modify plugin name

This commit is contained in:
2022-06-22 16:29:42 +08:00
parent f139f32c85
commit 38f00206eb
16 changed files with 597 additions and 815 deletions
@@ -1,311 +1,277 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/10/7 0007
* Time: 2:13
*/
declare(strict_types=1);
namespace Kiri\Abstracts;
use Exception;
use Kiri;
use Kiri\Di\LocalService;
use Kiri\Error\{ErrorHandler, StdoutLogger, StdoutLoggerInterface};
use Kiri\Exception\{InitException};
use Kiri\Di\ContainerInterface;
use Kiri\Server\{Server};
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider;
/**
* Class BaseApplication
* @package Kiri\Base
*/
abstract class BaseApplication extends Component
{
/**
* @var string
*/
public string $storage = APP_PATH . 'storage';
public string $envPath = APP_PATH . '.env';
/**
* Init constructor.
*
*
* @throws
*/
public function __construct(public ContainerInterface $container, public EventProvider $eventProvider)
{
Kiri::init($this);
$config = sweep(APP_PATH . '/config');
$this->mapping($config['mapping'] ?? []);
$this->parseInt($config);
$this->parseEvents($config);
$this->initErrorHandler();
$this->enableEnvConfig();
parent::__construct();
}
/**
* @param array $mapping
*/
public function mapping(array $mapping)
{
$di = Kiri::getDi();
$di->mapping(StdoutLoggerInterface::class, StdoutLogger::class);
$di->mapping(LoggerInterface::class, Logger::class);
foreach ($mapping as $interface => $class) {
$di->mapping($interface, $class);
}
}
/**
* @return array
*/
public function enableEnvConfig(): array
{
if (!file_exists($this->envPath)) {
return [];
}
$lines = $this->readLinesFromFile($this->envPath);
foreach ($lines as $line) {
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
[$key, $value] = explode('=', $line);
putenv(trim($key) . '=' . trim($value));
}
}
return $lines;
}
/**
* Read lines from the file, auto detecting line endings.
*
* @param string $filePath
*
* @return array
*/
protected function readLinesFromFile(string $filePath): array
{
// Read file into an array of lines with auto-detected line endings
// $autodetect = ini_get('auto_detect_line_endings');
// ini_set('auto_detect_line_endings', '1');
// ini_set('auto_detect_line_endings', $autodetect);
return file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
/**
* Determine if the line in the file is a comment, e.g. begins with a #.
*
* @param string $line
*
* @return bool
*/
protected function isComment(string $line): bool
{
$line = ltrim($line);
return isset($line[0]) && $line[0] === '#';
}
/**
* Determine if the given line looks like it's setting a variable.
*
* @param string $line
*
* @return bool
*/
protected function looksLikeSetter(string $line): bool
{
return str_contains($line, '=');
}
/**
* @param $config
*
* @throws
*/
public function parseInt($config)
{
Config::sets($config);
if ($storage = Config::get('storage', 'storage')) {
if (!str_contains($storage, APP_PATH)) {
$storage = APP_PATH . $storage . '/';
}
if (!is_dir($storage)) {
mkdir($storage);
}
if (!is_dir($storage) || !is_writeable($storage)) {
throw new InitException("Directory {$storage} does not have write permission");
}
}
}
/**
* @param $config
*
* @throws
*/
public function parseEvents($config)
{
if (!isset($config['events']) || !is_array($config['events'])) {
return;
}
foreach ($config['events'] as $key => $value) {
if (is_string($value)) {
$value = Kiri::createObject($value);
}
$this->addEvent($key, $value);
}
}
/**
* @param $key
* @param $value
* @return void
* @throws InitException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
private function addEvent($key, $value): void
{
if ($value instanceof \Closure || is_object($value)) {
$this->eventProvider->on($key, $value, 0);
return;
}
if (is_array($value)) {
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$this->eventProvider->on($key, $value, 0);
return;
}
if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$this->eventProvider->on($key, $value, 0);
return;
}
foreach ($value as $item) {
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$this->eventProvider->on($key, $item, 0);
}
}
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function clone($name): mixed
{
return clone $this->get($name);
}
/**
*
* @throws Exception
*/
public function initErrorHandler()
{
$error = Kiri::getDi()->get(ErrorHandler::class);
$error->register();
}
/**
* @param $name
* @return mixed
* @throws
*/
public function get($name): mixed
{
return Kiri::getDi()->get(LocalService::class)->get($name);
}
/**
* @return mixed
*/
public function getLocalIps(): mixed
{
return swoole_get_local_ip();
}
/**
* @return mixed
*/
public function getFirstLocal(): mixed
{
return current($this->getLocalIps());
}
/**
*
* @return Server
* @throws
*/
public function getServer(): Server
{
return Kiri::getDi()->get(Server::class);
}
/**
* @param string $name
* @return mixed|null
* @throws Exception
*/
public function __get(string $name)
{
$localService = Kiri::getDi()->get(LocalService::class);
if ($localService->has($name)) {
return $localService->get($name);
}
return parent::__get($name); // TODO: Change the autogenerated stub
}
/**
* @param $id
* @param $definition
*/
public function set($id, $definition): void
{
Kiri::getDi()->get(LocalService::class)->set($id, $definition);
}
/**
* @param $id
* @return bool
*/
public function has($id): bool
{
return Kiri::getDi()->get(LocalService::class)->has($id);
}
}
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/10/7 0007
* Time: 2:13
*/
declare(strict_types=1);
namespace Kiri\Abstracts;
use Exception;
use Kiri;
use Kiri\Di\LocalService;
use Kiri\Error\{ErrorHandler, StdoutLogger, StdoutLoggerInterface};
use Kiri\Exception\{InitException};
use Kiri\Di\ContainerInterface;
use Kiri\Message\Constrict\Request;
use Kiri\Message\Constrict\RequestInterface;
use Kiri\Message\Constrict\Response;
use Kiri\Message\Constrict\ResponseInterface;
use Kiri\Message\Emitter;
use Kiri\Message\ResponseEmitter;
use Kiri\Server\{Server};
use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider;
/**
* Class BaseApplication
* @package Kiri\Base
*/
abstract class BaseMain extends Component
{
/**
* @var string
*/
public string $storage = APP_PATH . 'storage';
public string $envPath = APP_PATH . '.env';
/**
* Init constructor.
*
*
* @throws
*/
public function __construct(public ContainerInterface $container, public EventProvider $provider)
{
$config = sweep(APP_PATH . '/config');
$this->mapping($config['mapping'] ?? []);
$this->parseInt($config);
$this->parseEvents($config);
$this->initErrorHandler();
$this->enableEnvConfig();
parent::__construct();
}
/**
* @param array $mapping
*/
public function mapping(array $mapping)
{
$di = Kiri::getDi();
$di->mapping(StdoutLoggerInterface::class, StdoutLogger::class);
$di->mapping(LoggerInterface::class, Logger::class);
$di->mapping(Emitter::class, ResponseEmitter::class);
$di->mapping(ResponseInterface::class, Response::class);
$di->mapping(RequestInterface::class, Request::class);
foreach ($mapping as $interface => $class) {
$di->mapping($interface, $class);
}
}
/**
* @return array
*/
public function enableEnvConfig(): array
{
if (!file_exists($this->envPath)) {
return [];
}
$lines = $this->readLinesFromFile($this->envPath);
foreach ($lines as $line) {
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
[$key, $value] = explode('=', $line);
putenv(trim($key) . '=' . trim($value));
}
}
return $lines;
}
/**
* Read lines from the file, auto detecting line endings.
*
* @param string $filePath
*
* @return array
*/
protected function readLinesFromFile(string $filePath): array
{
// Read file into an array of lines with auto-detected line endings
// $autodetect = ini_get('auto_detect_line_endings');
// ini_set('auto_detect_line_endings', '1');
// ini_set('auto_detect_line_endings', $autodetect);
return file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
/**
* Determine if the line in the file is a comment, e.g. begins with a #.
*
* @param string $line
*
* @return bool
*/
protected function isComment(string $line): bool
{
$line = ltrim($line);
return isset($line[0]) && $line[0] === '#';
}
/**
* Determine if the given line looks like it's setting a variable.
*
* @param string $line
*
* @return bool
*/
protected function looksLikeSetter(string $line): bool
{
return str_contains($line, '=');
}
/**
* @param $config
*
* @throws
*/
public function parseInt($config)
{
Config::sets($config);
if ($storage = Config::get('storage', 'storage')) {
if (!str_contains($storage, APP_PATH)) {
$storage = APP_PATH . $storage . '/';
}
if (!is_dir($storage)) {
mkdir($storage);
}
if (!is_dir($storage) || !is_writeable($storage)) {
throw new InitException("Directory {$storage} does not have write permission");
}
}
}
/**
* @param $config
*
* @throws
*/
public function parseEvents($config)
{
if (!isset($config['events']) || !is_array($config['events'])) {
return;
}
foreach ($config['events'] as $key => $value) {
if (is_string($value)) {
$value = Kiri::createObject($value);
}
$this->addEvent($key, $value);
}
}
/**
* @param $key
* @param $value
* @return void
* @throws InitException
* @throws Exception
*/
private function addEvent($key, $value): void
{
if ($value instanceof \Closure || is_object($value)) {
$this->provider->on($key, $value, 0);
return;
}
if (!is_array($value)) {
return;
}
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$this->provider->on($key, $value, 0);
return;
} else if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$this->provider->on($key, $value, 0);
return;
}
foreach ($value as $item) {
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$this->provider->on($key, $item, 0);
}
}
/**
* @return mixed
*/
public function getLocalIps(): mixed
{
return swoole_get_local_ip();
}
/**
* @return mixed
*/
public function getFirstLocal(): mixed
{
return current($this->getLocalIps());
}
/**
*
* @return Server
* @throws
*/
public function getServer(): Server
{
return Kiri::getDi()->get(Server::class);
}
/**
* @param string $name
* @return mixed|null
* @throws Exception
*/
public function __get(string $name)
{
$localService = Kiri::getDi()->get(LocalService::class);
if ($localService->has($name)) {
return $localService->get($name);
}
return parent::__get($name); // TODO: Change the autogenerated stub
}
/**
* @param $id
* @param $definition
*/
public function set($id, $definition): void
{
Kiri::getDi()->get(LocalService::class)->set($id, $definition);
}
/**
* @param $id
* @return bool
*/
public function has($id): bool
{
return Kiri::getDi()->get(LocalService::class)->has($id);
}
}
+9 -10
View File
@@ -137,6 +137,7 @@ class Logger implements LoggerInterface
* @param string $message
* @param array $context
* @throws ConfigException
* @throws Exception
*/
public function log($level, $message, array $context = [])
{
@@ -146,8 +147,10 @@ class Logger implements LoggerInterface
return;
}
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
$_string = '[' . now() . '] production.' . $level . ': ' . $message;
if (!empty($context)) {
$_string .= PHP_EOL . $this->_string($context);
}
file_put_contents('php://output', $_string);
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
@@ -188,21 +191,17 @@ class Logger implements LoggerInterface
/**
* @param $message
* @param $context
* @return string
*/
private function _string($message, $context): string
private function _string($context): string
{
if ($context instanceof \Throwable) {
$context = ['file' => $context->getFile(), 'line' => $context->getLine()];
}
if (!empty($context)) {
if (is_array($context) && $context[0] instanceof \Throwable) {
$context = ['file' => $context[0]->getFile(), 'line' => $context[0]->getLine()];
}
return $message . ' ' . PHP_EOL . print_r($context, true) . PHP_EOL;
if (is_array($context) && $context[0] instanceof \Throwable) {
$context = ['file' => $context[0]->getFile(), 'line' => $context[0]->getLine()];
}
return $message . PHP_EOL;
return print_r($context, true) . PHP_EOL;
}
}
+2 -2
View File
@@ -4,11 +4,11 @@ declare(strict_types=1);
namespace Kiri\Abstracts;
use Kiri\Application;
use Kiri\Di\LocalService;
interface Provider
{
public function onImport(Application $application);
public function onImport(LocalService $application);
}
+13
View File
@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Kiri\Abstracts;
use Exception;
use Kiri\Di\ContainerInterface;
/**
* Class Providers
* @package Kiri\Abstracts
@@ -12,4 +15,14 @@ abstract class Providers extends Component implements Provider
{
/**
* @param ContainerInterface $container
* @param array $config
* @throws Exception
*/
public function __construct(public ContainerInterface $container, array $config = [])
{
parent::__construct($config);
}
}
-249
View File
@@ -1,249 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/25 0025
* Time: 18:38
*/
declare(strict_types=1);
namespace Kiri;
use Closure;
use Database\DatabasesProviders;
use Exception;
use Kiri;
use Kiri\Abstracts\{BaseApplication, Config, Kernel};
use Kiri\Crontab\CrontabProviders;
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
use Kiri\Server\ServerProviders;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use stdClass;
use Swoole\Process;
use Swoole\Timer;
use Symfony\Component\Console\{Application as ConsoleApplication,
Command\Command,
Input\ArgvInput,
Output\ConsoleOutput,
Output\OutputInterface
};
/**
* Class Init
*
* @package Kiri
*
* @property-read Config $config
*/
class Application extends BaseApplication
{
/**
* @var string
*/
public string $id = 'uniqueId';
public string $state = '';
/** @var array<array<Process>> */
private array $_process = [];
/**
*/
public function init()
{
$this->import(ServerProviders::class);
}
/**
* @throws
*/
public function withDatabase()
{
$this->import(DatabasesProviders::class);
}
/**
* @throws
*/
public function withCrontab()
{
$this->import(CrontabProviders::class);
}
/**
* @param string $class
* @param Process $process
*/
public function addProcess(string $class, Process $process)
{
}
/**
* @return Process[]
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @param string $class
* @return Process|null
*/
public function getProcessName(string $class): ?Process
{
return $this->_process[$class] ?? null;
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
return $this;
}
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
return $this;
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
/**
* @param string $command
* @throws
*/
public function register(string $command)
{
di(ConsoleApplication::class)->add(di($command));
}
/**
* @param array $argv
* @return void
*/
public function execute(array $argv): void
{
ini_set('swoole.enable_preemptive_scheduler', 'On');
ini_set('swoole.enable_library', 'On');
[$input, $output] = $this->argument($argv);
try {
$console = di(ConsoleApplication::class);
$command = $input->getFirstArgument();
if (empty($command)) {
$command = 'sw:server';
}
$command = $console->find($command);
if ($command instanceof Command) {
$this->enableFileChange($command, $input, $output);
}
} catch (\Throwable $exception) {
$output->writeln(jTraceEx($exception));
} finally {
Timer::clearAll();
}
}
/**
* @param $argv
* @return array
*/
private function argument($argv): array
{
return [new ArgvInput($argv), new ConsoleOutput()];
}
/**
* @param Command $class
* @param $input
* @param $output
* @return void
* @throws ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
private function enableFileChange(Command $class, $input, $output): void
{
fire(new OnBeforeCommandExecute());
$this->container->setBindings(OutputInterface::class, $output);
if (!($class instanceof Kiri\Server\ServerCommand)) {
scan_directory(MODEL_PATH,'app\Model');
}
$class->run($input, $output);
fire(new OnAfterCommandExecute());
$output->writeln('ok' . PHP_EOL);
}
/**
* @param $className
* @param null $abstracts
* @return stdClass
* @throws Exception
*/
public function make($className, $abstracts = null): stdClass
{
return make($className, $abstracts);
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
namespace Kiri\Core;
use JetBrains\PhpStorm\Pure;
use ReturnTypeWillChange;
use Kiri\ReturnTypeWillChange;
use Traversable;
class HashMap implements \ArrayAccess, \IteratorAggregate
+2 -2
View File
@@ -131,7 +131,7 @@ class ErrorHandler extends Component implements ErrorInterface
* @return false|string
* @throws Exception
*/
public function sendError($message, $file, $line, $code = 500): bool|string
public function sendError($message, $file, $line, int $code = 500): bool|string
{
$path = ['file' => $file, 'line' => $line];
@@ -168,6 +168,6 @@ class ErrorHandler extends Component implements ErrorInterface
*/
public function writer($message, string $category = 'app')
{
Kiri::app()->debug($message, $category);
Kiri::getLogger()->debug($category, [$message]);
}
}
+1 -1
View File
@@ -23,6 +23,6 @@ interface ErrorInterface
* @param int $code
* @return mixed
*/
public function sendError($message, $file, $line, $code = 500): mixed;
public function sendError($message, $file, $line, int $code = 500): mixed;
}
+127
View File
@@ -0,0 +1,127 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/25 0025
* Time: 18:38
*/
declare(strict_types=1);
namespace Kiri;
use Exception;
use Kiri;
use Kiri\Abstracts\{BaseMain, Config, Kernel};
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Symfony\Component\Console\{Application as ConsoleApplication,
Input\ArgvInput,
Output\ConsoleOutput,
Output\OutputInterface
};
/**
* Class Init
*
* @package Kiri
*
* @property-read Config $config
*/
class Main extends BaseMain
{
/**
* @var string
*/
public string $id = 'uniqueId';
public string $state = '';
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
return $this;
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->command($command);
}
return $this;
}
/**
* @param string $command
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function command(string $command): void
{
$console = $this->container->get(ConsoleApplication::class);
$console->add($this->container->get($command));
}
/**
* @param array $argv
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception
*/
public function execute(array $argv): void
{
[$input, $output] = $this->argument($argv);
$console = $this->container->get(ConsoleApplication::class);
$command = $console->find($input->getFirstArgument());
fire(new OnBeforeCommandExecute());
$command->run($input, $output);
fire(new OnAfterCommandExecute());
$output->writeln('ok' . PHP_EOL);
}
/**
* @param $argv
* @return array
*/
private function argument($argv): array
{
$input = new ArgvInput($argv);
$this->container->setBindings(ArgvInput::class, $input);
$output = new ConsoleOutput();
$this->container->setBindings(OutputInterface::class, $output);
return [$input, $output];
}
}