diff --git a/Kiri.php b/Kiri.php index 38478098..9ff4c39e 100644 --- a/Kiri.php +++ b/Kiri.php @@ -145,7 +145,7 @@ class Kiri */ public static function getAnnotation(): Annotation { - return static::app()->getAnnotation(); + return static::getDi()->get(Annotation::class); } @@ -311,15 +311,6 @@ class Kiri } - /** - * @return mixed - * @throws Exception - */ - public static function reload(): mixed - { - return Kiri::app()->getSwoole()->reload(); - } - const PROCESS = 'process'; const TASK = 'task'; diff --git a/function.php b/function.php index 727f36a0..6605ecfa 100644 --- a/function.php +++ b/function.php @@ -9,8 +9,8 @@ use Kiri\Annotation\Annotation; use Kiri\Annotation\Route\Route; use Kiri\Application; use Kiri\Core\ArrayAccess; -use Kiri\Di\NoteManager; -use Kiri\Error\Logger; +use Kiri\Di\TargetManager; +use Kiri\Error\StdoutLoggerInterface; use Kiri\Events\EventDispatch; use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; @@ -175,21 +175,6 @@ if (!function_exists('now')) { } -if (!function_exists('workerName')) { - - - /** - * @param $worker_id - * @return string - */ - function workerName($worker_id) - { - return $worker_id >= Kiri::app()->getSwoole()->setting['worker_num'] ? 'Task' : 'Worker'; - } - -} - - if (!function_exists('Annotation')) { @@ -218,7 +203,7 @@ if (!function_exists('scan_directory')) { */ function scan_directory($dir, $namespace, array $exclude = []) { - $annotation = Kiri::app()->getAnnotation(); + $annotation = Kiri::getDi()->get(Annotation::class); $annotation->read($dir, $namespace, $exclude); injectRuntime($dir, $exclude); @@ -259,18 +244,22 @@ if (!function_exists('injectRuntime')) { $router = []; foreach ($fileLists as $class) { - foreach (NoteManager::getTargetAnnotation($class) as $value) { + $targetAttributes = TargetManager::get($class)->getAttributes(); + foreach ($targetAttributes as $value) { + $value = $value->newInstance(); if (!method_exists($value, 'execute')) { continue; } $value->execute($class); } - $methods = $di->getMethodAttribute($class); + + $methods = TargetManager::get($class)->getMethodsAttribute(); foreach ($methods as $method => $attribute) { if (empty($attribute)) { continue; } foreach ($attribute as $item) { + $item = $item->newInstance(); if ($item instanceof Route) { $router[] = [$item, $class, $method]; } else { @@ -448,22 +437,6 @@ if (!function_exists('loadByDir')) { } -if (!function_exists('write')) { - - - /** - * @param string $messages - * @param string $category - * @throws Exception - */ - function write(string $messages, string $category = 'app') - { - $logger = Kiri::app()->getLogger(); - $logger->write($messages, $category); - } -} - - if (!function_exists('redis')) { @@ -536,12 +509,11 @@ if (!function_exists('logger')) { /** - * @return Logger * @throws Exception */ - function logger(): Logger + function logger(): StdoutLoggerInterface { - return Kiri::app()->getLogger(); + return Kiri::getDi()->get(StdoutLoggerInterface::class); } } diff --git a/kiri-annotation/Attribute.php b/kiri-annotation/AbstractAttribute.php similarity index 83% rename from kiri-annotation/Attribute.php rename to kiri-annotation/AbstractAttribute.php index 067aacf0..22b3e32c 100644 --- a/kiri-annotation/Attribute.php +++ b/kiri-annotation/AbstractAttribute.php @@ -1,27 +1,27 @@ -get(EventProvider::class); - if (is_string($class)) { - $class = Kiri::getDi()->get($class); - } - $pro->on($this->name, [$class, $method]); - return true; - } + public function __serialize() + { + // TODO: Implement __serialize() method. + } + + + public function __unserialize(array $data) + { + // TODO: Implement __unserialize() method. + } + + + public function serialize(): array + { + // TODO: Implement __serialize() method. + } + + + public function unserialize(array|string $data): void + { + } + + + /** + * @param mixed $class + * @param mixed|null $method + * @return bool + * @throws Exception + */ + public function execute(mixed $class, mixed $method = null): bool + { + $pro = Kiri::getDi()->get(EventProvider::class); + if (is_string($class)) { + $class = Kiri::getDi()->get($class); + } + $pro->on($this->name, [$class, $method]); + return true; + } } diff --git a/kiri-annotation/Inject.php b/kiri-annotation/Inject.php index 53501560..858dd5d0 100644 --- a/kiri-annotation/Inject.php +++ b/kiri-annotation/Inject.php @@ -14,7 +14,7 @@ use ReflectionProperty; * Class Inject * @package Annotation */ -#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject extends Attribute +#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject extends AbstractAttribute { diff --git a/kiri-annotation/Loader.php b/kiri-annotation/Loader.php index ae26f2f5..c71208fc 100644 --- a/kiri-annotation/Loader.php +++ b/kiri-annotation/Loader.php @@ -124,7 +124,7 @@ class Loader extends Component } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); } catch (Throwable $throwable) { - $this->error(jTraceEx($throwable), 'throwable'); + $this->logger->error(jTraceEx($throwable)); } } @@ -167,7 +167,7 @@ class Loader extends Component } return $paths; } catch (Throwable $exception) { - $this->addError($exception, 'throwable'); + $this->logger->addError($exception, 'throwable'); return []; } } diff --git a/kiri-annotation/Mapping.php b/kiri-annotation/Mapping.php index 37cdf1a6..bc6d501c 100644 --- a/kiri-annotation/Mapping.php +++ b/kiri-annotation/Mapping.php @@ -4,7 +4,7 @@ namespace Kiri\Annotation; use Kiri; -#[\Attribute(\Attribute::TARGET_CLASS)] class Mapping extends Attribute +#[\Attribute(\Attribute::TARGET_CLASS)] class Mapping extends AbstractAttribute { diff --git a/kiri-annotation/Route/Document.php b/kiri-annotation/Route/Document.php index 2de1ef26..fea578a8 100644 --- a/kiri-annotation/Route/Document.php +++ b/kiri-annotation/Route/Document.php @@ -4,13 +4,13 @@ namespace Kiri\Annotation\Route; -use Kiri\Annotation\Attribute; +use Kiri\Annotation\AbstractAttribute; /** * Class Document * @package Annotation\Route */ -#[\Attribute(\Attribute::TARGET_METHOD)] class Document extends Attribute +#[\Attribute(\Attribute::TARGET_METHOD)] class Document extends AbstractAttribute { const INTEGER = 'int'; diff --git a/kiri-annotation/Route/Method.php b/kiri-annotation/Route/Method.php new file mode 100644 index 00000000..09b6e950 --- /dev/null +++ b/kiri-annotation/Route/Method.php @@ -0,0 +1,16 @@ +mapping($config['mapping'] ?? []); + $config = sweep(APP_PATH . '/config'); $this->moreComponents(); @@ -60,7 +64,6 @@ abstract class BaseApplication extends Component $this->parseEvents($config); $this->initErrorHandler(); $this->enableEnvConfig(); - $this->mapping($config['mapping'] ?? []); parent::__construct(); } @@ -72,6 +75,8 @@ abstract class BaseApplication extends Component 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); } @@ -109,10 +114,9 @@ abstract class BaseApplication extends Component // 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'); - $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); -// ini_set('auto_detect_line_endings', $autodetect); + // ini_set('auto_detect_line_endings', $autodetect); - return $lines; + return file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); } /** @@ -199,39 +203,34 @@ abstract class BaseApplication extends Component } - /** - * @param OnTaskInterface $execute - * @throws ReflectionException|Exception - */ - public function task(OnTaskInterface $execute): void - { - di(AsyncTaskExecute::class)->execute($execute); - } - /** * @param $key * @param $value + * @return void * @throws InitException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception */ private function addEvent($key, $value): void { + $provider = $this->getEventProvider(); if ($value instanceof \Closure || is_object($value)) { - $this->getEventProvider()->on($key, $value, 0); + $provider->on($key, $value, 0); return; } if (is_array($value)) { if (is_object($value[0]) && !($value[0] instanceof \Closure)) { - $this->getEventProvider()->on($key, $value, 0); + $provider->on($key, $value, 0); return; } if (is_string($value[0])) { $value[0] = Kiri::createObject($value[0]); - $this->getEventProvider()->on($key, $value, 0); + $provider->on($key, $value, 0); return; } @@ -240,7 +239,7 @@ abstract class BaseApplication extends Component if (!is_callable($item, true)) { throw new InitException("Class does not hav callback."); } - $this->getEventProvider()->on($key, $item, 0); + $provider->on($key, $item, 0); } } @@ -263,7 +262,8 @@ abstract class BaseApplication extends Component */ public function initErrorHandler() { - $this->get('error')->register(); + $error = $this->container->get(ErrorHandler::class); + $error->register(); } @@ -305,65 +305,6 @@ abstract class BaseApplication extends Component } - /** - * @return \Redis|Redis - * @throws - */ - public function getRedis(): Redis|\Redis - { - return Kiri::getDi()->get(Redis::class); - } - - /** - * @param $ip - * @return bool - */ - public function isLocal($ip): bool - { - return $this->getFirstLocal() == $ip; - } - - - /** - * @return ErrorHandler - * @throws - */ - public function getError(): ErrorHandler - { - return $this->get('error'); - } - - - /** - * @param $name - * @return Table - * @throws - */ - public function getTable($name): Table - { - return $this->get($name); - } - - - /** - * @return Config - * @throws - */ - public function getConfig(): Config - { - return $this->get('config'); - } - - - /** - * @return Router - * @throws - */ - public function getRouter(): Router - { - return Kiri::getDi()->get(Router::class); - } - /** * @return Server @@ -375,34 +316,6 @@ abstract class BaseApplication extends Component } - /** - * @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null - * @throws - */ - public function getSwoole(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null - { - return di(ServerManager::class)->getServer(); - } - - - /** - * @return SAnnotation - * @throws - */ - public function getAnnotation(): SAnnotation - { - return $this->get('Annotation'); - } - - - /** - * @param $array - */ - private function setComponents($array): void - { - di(LocalService::class)->setComponents($array); - } - /** * @param $id @@ -422,20 +335,4 @@ abstract class BaseApplication extends Component { return di(LocalService::class)->has($id); } - - - /** - * @throws Exception - */ - protected function moreComponents(): void - { - $this->setComponents([ - 'error' => ['class' => ErrorHandler::class], - 'config' => ['class' => Config::class], - 'logger' => ['class' => Logger::class], - 'Annotation' => ['class' => SAnnotation::class], - 'databases' => ['class' => Connection::class], - 'kafka-container' => ['class' => KafkaProvider::class], - ]); - } } diff --git a/kiri-engine/Abstracts/Component.php b/kiri-engine/Abstracts/Component.php index b38f5a04..1f26a286 100644 --- a/kiri-engine/Abstracts/Component.php +++ b/kiri-engine/Abstracts/Component.php @@ -12,10 +12,11 @@ namespace Kiri\Abstracts; use Exception; use JetBrains\PhpStorm\Pure; +use Kiri; use Kiri\Di\Container; +use Kiri\Error\StdoutLogger; use Kiri\Events\EventDispatch; use Kiri\Events\EventProvider; -use Kiri; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; @@ -23,11 +24,17 @@ use Psr\Container\NotFoundExceptionInterface; /** * Class Component * @package Kiri\Base + * @property EventDispatch $eventDispatch + * @property EventProvider $eventProvider + * @property Container $container */ class Component implements Configure { + protected ?StdoutLogger $logger = null; + + /** * BaseAbstract constructor. * @@ -36,11 +43,30 @@ class Component implements Configure */ public function __construct(array $config = []) { + if (is_null($this->logger)) { + $this->logger = Kiri::getDi()->get(StdoutLogger::class); + } if (!empty($config) && is_array($config)) { Kiri::configure($this, $config); } } + /** + * @throws Exception + */ + public function init() + { + } + + + /** + * @return string + */ + #[Pure] public static function className(): string + { + return static::class; + } + /** * @return Container|ContainerInterface @@ -72,156 +98,42 @@ class Component implements Configure return $this->getContainer()->get(EventDispatch::class); } + /** + * @param string $name + * @return mixed * @throws Exception */ - public function init() + public function __get(string $name) { - } - - - /** - * @return string - */ - #[Pure] public static function className(): string - { - return static::class; - } - - - /** - * @param $message - * @param string $model - * @return bool - * @throws Exception - */ - public function addError($message, string $model = 'app'): bool - { - if ($message instanceof \Throwable) { - $this->error($message = jTraceEx($message)); + $method = 'get' . ucfirst($name); + if (method_exists($this, $method)) { + return $this->{$method}(); + } else if (method_exists($this, $name)) { + return $this->{$name}; } else { - if (!is_string($message)) { - $message = json_encode($message, JSON_UNESCAPED_UNICODE); - } - $this->error($message); + throw new Exception('Unable getting property ' . get_called_class() . '::' . $name); } - Kiri::app()->getLogger()->fail($message, $model); - return FALSE; } /** - * @return Logger + * @param string $name + * @param $value + * @return void * @throws Exception */ - protected function logger(): Logger + public function __set(string $name, $value): void { - return Kiri::getDi()->get(Logger::class); - } - - - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function debug(mixed $message, string $method = '', string $file = '') - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $context = []; - if (!empty($method)) $context['method'] = $method; - if (!empty($file)) $context['file'] = $file; - - $this->logger()->debug($message, $context); - } - - - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function info(mixed $message, string $method = '', string $file = '') - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $context = []; - if (!empty($method)) $context['method'] = $method; - if (!empty($file)) $context['file'] = $file; - - $this->logger()->info($message, $context); - } - - - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function success(mixed $message, string $method = '', string $file = '') - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $context = []; - if (!empty($method)) $context['method'] = $method; - if (!empty($file)) $context['file'] = $file; - - $this->logger()->notice($message, $context); - } - - - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function warning(mixed $message, string $method = '', string $file = '') - { - if (!is_string($message)) { - $message = print_r($message, true); - } - - $context = []; - if (!empty($method)) $context['method'] = $method; - if (!empty($file)) $context['file'] = $file; - - $this->logger()->critical($message, $context); - } - - - /** - * @param mixed $message - * @param null $method - * @param null $file - * @throws Exception - */ - public function error(mixed $message, $method = null, $file = null) - { - if ($message instanceof \Throwable) { - $message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile(); - } - - $context = []; - if (is_string($method)) { - $message = (empty($method) ? '' : $method . ': ') . $message; + $method = 'set' . ucfirst($name); + if (method_exists($this, $method)) { + $this->{$method}($value); + } else if (method_exists($this, $name)) { + $this->{$name} = $value; } else { - if (is_null($method)) { - $method = []; - } - $context = $method; + throw new Exception('Unable setting property ' . get_called_class() . '::' . $name); } - if (!empty($method)) $context['method'] = $method; - if (!empty($file)) $context['file'] = $file; - - $this->logger()->error($message, $context); } + } diff --git a/kiri-engine/Abstracts/Logger.php b/kiri-engine/Abstracts/Logger.php index 825faf1d..5d361ae0 100644 --- a/kiri-engine/Abstracts/Logger.php +++ b/kiri-engine/Abstracts/Logger.php @@ -36,7 +36,6 @@ class Logger implements LoggerInterface /** * @return void - * @throws ReflectionException */ public function init() { diff --git a/kiri-engine/Abstracts/TraitApplication.php b/kiri-engine/Abstracts/TraitApplication.php index 254abcb9..3fb36ccf 100644 --- a/kiri-engine/Abstracts/TraitApplication.php +++ b/kiri-engine/Abstracts/TraitApplication.php @@ -9,7 +9,6 @@ use Database\Connection; use Database\DatabasesProviders; use Kiri\Message\Handler\Router; use Kiri\Server\Server; -use Kiri\Error\Logger; use Kiri\Jwt\JWTAuth; /** @@ -18,7 +17,6 @@ use Kiri\Jwt\JWTAuth; * @property Router $router * @property Server $server * @property DatabasesProviders $db - * @property Logger $logger * @property JWTAuth $jwt * @property SAnnotation $annotation * @property Connection $databases diff --git a/kiri-engine/Application.php b/kiri-engine/Application.php index 258163ab..581e664a 100644 --- a/kiri-engine/Application.php +++ b/kiri-engine/Application.php @@ -11,7 +11,6 @@ namespace Kiri; use Closure; -use Database\CreateConnectionPool; use Database\DatabasesProviders; use Exception; use Kiri; @@ -62,8 +61,6 @@ class Application extends BaseApplication public function init() { $this->import(ServerProviders::class); - - $this->register(Runtime::class); } diff --git a/kiri-engine/Async.php b/kiri-engine/Async.php deleted file mode 100644 index 55adc13f..00000000 --- a/kiri-engine/Async.php +++ /dev/null @@ -1,44 +0,0 @@ -execute(static::$_absences[$name], $params); - } - -} diff --git a/kiri-engine/Cache/Redis.php b/kiri-engine/Cache/Redis.php index 3d5254bd..048be94d 100644 --- a/kiri-engine/Cache/Redis.php +++ b/kiri-engine/Cache/Redis.php @@ -80,7 +80,7 @@ class Redis extends Component $data = $this->proxy($name, $arguments); } if (microtime(true) - $time >= 0.02) { - $this->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time)); + $this->logger->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time)); } return $data; } diff --git a/kiri-engine/Error/ErrorHandler.php b/kiri-engine/Error/ErrorHandler.php index 137989e0..baa60f2a 100644 --- a/kiri-engine/Error/ErrorHandler.php +++ b/kiri-engine/Error/ErrorHandler.php @@ -93,11 +93,10 @@ class ErrorHandler extends Component implements ErrorInterface $data = Json::to(500, $error[1], $path); - Kiri::app()->error($data, 'error'); + $this->logger->error('On error handler', [$data]); di(EventDispatch::class)->dispatch(new OnAfterRequest()); - throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); } diff --git a/kiri-engine/Error/Logger.php b/kiri-engine/Error/Logger.php deleted file mode 100644 index 23996093..00000000 --- a/kiri-engine/Error/Logger.php +++ /dev/null @@ -1,117 +0,0 @@ -logs[$application] ?? 'Unknown error.'; - } - - - /** - * @param $message - * @param $method - * @return void - */ - public function fail($message, $method) - { - $this->logs[$method] = $message; - } - - - /** - * @param string $messages - * @param string $method - * @throws Exception - */ - public function write(string $messages, string $method = 'app') - { - if (empty($messages)) { - return; - } - - $to_day = date('Y-m-d'); - - $fileName = storage('server-' . $to_day . '.log', $dirName = 'log/' . ($method ?? 'app')); - - file_put_contents($fileName, '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL); - } - - - /** - * @param Throwable $exception - * @return mixed - * @throws Exception - */ - public function exception(Throwable $exception): mixed - { - $code = $exception->getCode() == 0 ? 500 : $exception->getCode(); - - $logger = Kiri::app()->getLogger(); - $logger->write(jTraceEx($exception), 'exception'); - - return Json::to($code, $exception->getMessage(), [ - 'file' => $exception->getFile(), - 'line' => $exception->getLine() - ]); - } - - - /** - * @param string $name - * @param array $arguments - * @return mixed - */ - public function __call(string $name, array $arguments): mixed - { - if (!method_exists($this, $name)) { - return $this->logger->{$name}(...$arguments); - } else { - return $this->{$name}(...$arguments); - } - } - - -} diff --git a/kiri-engine/Error/StdoutLogger.php b/kiri-engine/Error/StdoutLogger.php new file mode 100644 index 00000000..4c7b1982 --- /dev/null +++ b/kiri-engine/Error/StdoutLogger.php @@ -0,0 +1,42 @@ +error($model, [$message]); + if ($message instanceof \Exception) { + $this->errors[$model] = $message->getMessage(); + } else { + $this->errors[$model] = $message; + } + return false; + } + + + /** + * @param string $model + * @return mixed + */ + public function getLastError(string $model = 'app'): mixed + { + return $this->errors[$model] ?? 'Unknown error.'; + } + +} diff --git a/kiri-engine/Error/StdoutLoggerInterface.php b/kiri-engine/Error/StdoutLoggerInterface.php new file mode 100644 index 00000000..067db663 --- /dev/null +++ b/kiri-engine/Error/StdoutLoggerInterface.php @@ -0,0 +1,26 @@ + $event) { - [$handler] = $event; - if ($handler !== $callback) { - continue; - } - unset(static::$_events[$name][$index]); - } - } - - - /** - * @param $name - */ - public static function offName($name): void - { - unset(static::$_events[$name]); - } - - - /** - * @param $name - * @param null $callback - * @return bool - */ - public static function exists($name, $callback): bool - { - if ($callback instanceof \Closure || !isset(static::$_events[$name])) { - return false; - } - foreach (static::$_events[$name] as $event) { - [$handler] = $event; - if ($handler === $callback) { - return true; - } - } - return false; - } - - - /** - * @param $name - * @param $handler - * @return mixed - */ - public static function get($name, $handler): mixed - { - if (!static::exists($name, $handler)) { - return null; - } - if (empty($handler)) { - return static::$_events[$name]; - } - foreach (static::$_events[$name] as $event) { - [$callback] = $event; - if ($callback === $handler) { - return [$event]; - } - } - return null; - } - - - public static function clean() - { - static::$_events = []; - } - - - /** - * @param $name - * @param array $params - * @return bool - * @throws Exception - */ - public function dispatch($name, array $params = []): bool - { - return static::trigger($name, $params); - } - - - /** - * @param $name - * @param null $parameter - * @param false $is_remove - * @return bool - * @throws Exception - */ - public static function trigger($name, $parameter = null, bool $is_remove = false): bool - { - foreach ((static::$_events[$name] ?? []) as $key => $event) { - static::execute($event, $parameter); - if ($event instanceof \Closure) { - unset(static::$_events[$name][$key]); - } - } - if ($is_remove) { - unset(static::$_events[$name]); - } - return true; - } - - - /** - * @param $event - * @param $parameter - * @return void - * @throws Exception - */ - private static function execute($event, $parameter): void - { - try { - call_user_func($event[0], ...$parameter); - } catch (\Throwable $throwable) { - logger()->addError($throwable, 'throwable'); - return; - } - } - - - -} diff --git a/kiri-engine/FileListen/HotReload.php b/kiri-engine/FileListen/HotReload.php index 0e8dd576..c92f541e 100644 --- a/kiri-engine/FileListen/HotReload.php +++ b/kiri-engine/FileListen/HotReload.php @@ -7,11 +7,11 @@ use Kiri; use Kiri\Abstracts\Config; use Kiri\Annotation\Inject; use Kiri\Core\Json; -use Kiri\Error\Logger; use Kiri\Exception\ConfigException; use Swoole\Coroutine; use Swoole\Process; use Swoole\Timer; +use Kiri\Error\StdoutLogger; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -39,8 +39,8 @@ class HotReload extends Command public Inotify|Scaner $driver; - #[Inject(Logger::class)] - public Logger $logger; + #[Inject(StdoutLogger::class)] + public StdoutLogger $logger; protected mixed $source = NULL; diff --git a/kiri-engine/FileListen/Inotify.php b/kiri-engine/FileListen/Inotify.php index 99b5acbc..b7d719f4 100644 --- a/kiri-engine/FileListen/Inotify.php +++ b/kiri-engine/FileListen/Inotify.php @@ -3,7 +3,7 @@ namespace Kiri\FileListen; use Exception; -use Kiri\Error\Logger; +use Kiri\Error\StdoutLogger; use Swoole\Event; use Swoole\Timer; @@ -103,7 +103,7 @@ class Inotify */ public function reload($path) { - \Kiri::getDi()->get(Logger::class)->warning('file change'); + \Kiri::getDi()->get(StdoutLogger::class)->warning('file change'); $this->process->trigger_reload($path); $this->process->int = -1; @@ -137,7 +137,8 @@ class Inotify { //目录不存在 if (!is_dir($dir)) { - return logger()->addError("[$dir] is not a directory."); + logger()->addError("[$dir] is not a directory."); + return false; } //避免重复监听 if (isset($this->watchFiles[$dir])) { diff --git a/kiri-engine/FileListen/Scaner.php b/kiri-engine/FileListen/Scaner.php index f9d2c7ba..208720a3 100644 --- a/kiri-engine/FileListen/Scaner.php +++ b/kiri-engine/FileListen/Scaner.php @@ -3,7 +3,7 @@ namespace Kiri\FileListen; use Exception; -use Kiri\Error\Logger; +use Kiri\Error\StdoutLogger; class Scaner { @@ -113,7 +113,7 @@ class Scaner { $this->isReloading = TRUE; - \Kiri::getDi()->get(Logger::class)->warning('file change'); + \Kiri::getDi()->get(StdoutLogger::class)->warning('file change'); $this->process->trigger_reload($path); diff --git a/kiri-engine/IProxy.php b/kiri-engine/IProxy.php deleted file mode 100644 index 75f55171..00000000 --- a/kiri-engine/IProxy.php +++ /dev/null @@ -1,13 +0,0 @@ -addError($exception, 'mysql'); + $result = $this->logger->addError($exception, 'mysql'); } finally { return $result; } diff --git a/kiri-engine/Proxy.php b/kiri-engine/Proxy.php deleted file mode 100644 index f592ab3c..00000000 --- a/kiri-engine/Proxy.php +++ /dev/null @@ -1,27 +0,0 @@ -IProxy->execute(); - } - -} diff --git a/kiri-engine/Runtime.php b/kiri-engine/Runtime.php deleted file mode 100644 index 3ed3f68a..00000000 --- a/kiri-engine/Runtime.php +++ /dev/null @@ -1,100 +0,0 @@ -setName('runtime:builder'); - } - - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int - * @throws Exception - */ - public function execute(InputInterface $input, OutputInterface $output): int - { - // TODO: Implement onHandler() method. - $annotation = Kiri::app()->getAnnotation(); - - $runtime = storage(static::CACHE_NAME); - $config = storage(static::CONFIG_NAME); - - Kiri::writeFile($config, $this->configEach()); - Kiri::writeFile($runtime, serialize($annotation->getLoader())); - - return 1; - } - - - /** - * @return string - * @throws Exception - */ - public function configEach(): string - { - $array = []; - foreach (Config::getData() as $key => $datum) { - if ($datum instanceof \Closure) { - continue; - } - if (is_array($datum)) { - $array[$key] = $this->arrayEach($datum); - } else { - $array[$key] = $datum; - } - } - return serialize($array); - } - - - /** - * @param array $value - * @return array - */ - private function arrayEach(array $value): array - { - $array = []; - foreach ($value as $key => $item) { - if ($item instanceof \Closure) { - continue; - } - if (is_array($item)) { - $array[$key] = $this->arrayEach($item); - } else { - $array[$key] = $item; - } - } - return $array; - } - - -} diff --git a/kiri-gii/GiiController.php b/kiri-gii/GiiController.php index 740a250d..8f6a949d 100644 --- a/kiri-gii/GiiController.php +++ b/kiri-gii/GiiController.php @@ -60,7 +60,8 @@ namespace {$namespace}; $import = $this->getImports($path['path'] . '/' . $managerName . 'Controller.php', $class); } catch (\Throwable $Exception) { - exit(logger()->addError($Exception, 'throwable')); + logger()->addError($Exception, 'throwable'); + exit(); } } else { $import = "use Kiri; diff --git a/kiri-task/Annotation/AsynchronousTask.php b/kiri-task/Annotation/AsynchronousTask.php index 34b7719e..ddc67e75 100644 --- a/kiri-task/Annotation/AsynchronousTask.php +++ b/kiri-task/Annotation/AsynchronousTask.php @@ -3,10 +3,10 @@ namespace Kiri\Task\Annotation; -use Kiri\Annotation\Attribute; +use Kiri\Annotation\AbstractAttribute; use Kiri\Task\TaskManager; -#[\Attribute(\Attribute::TARGET_CLASS)] class AsynchronousTask extends Attribute +#[\Attribute(\Attribute::TARGET_CLASS)] class AsynchronousTask extends AbstractAttribute {