From 2c21e5c2fe00960798009fa2ddfd5e2cb337de80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 11 Aug 2023 00:12:33 +0800 Subject: [PATCH] qqq --- function.php | 16 +++ kiri-engine/Abstracts/BaseApplication.php | 65 ++++------- kiri-engine/Application.php | 105 ++++++++++-------- kiri-engine/Events/OnAfterCommandExecute.php | 4 +- kiri-engine/Events/OnBeforeCommandExecute.php | 10 ++ 5 files changed, 109 insertions(+), 91 deletions(-) diff --git a/function.php b/function.php index 9afbf08d..e3b285e5 100644 --- a/function.php +++ b/function.php @@ -662,6 +662,22 @@ if (!function_exists('on')) { } +if (!function_exists('off')) { + + + /** + * @param $name + * @param $callback + * @throws + */ + function off($name, $callback): void + { + $pro = di(EventProvider::class); + $pro->off($name, $callback); + } + +} + if (!function_exists('process_name_set')) { diff --git a/kiri-engine/Abstracts/BaseApplication.php b/kiri-engine/Abstracts/BaseApplication.php index 1214671d..9a16749f 100644 --- a/kiri-engine/Abstracts/BaseApplication.php +++ b/kiri-engine/Abstracts/BaseApplication.php @@ -13,13 +13,17 @@ namespace Kiri\Abstracts; use Database\DatabasesProviders; use Exception; use Kiri; +use Kiri\Events\EventInterface; use Kiri\Di\LocalService; use Kiri\Config\ConfigProvider; use Kiri\Error\StdoutLogger; use Kiri\Exception\{InitException}; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; use Kiri\Events\EventProvider; +use ReflectionException; /** * Class BaseApplication @@ -99,16 +103,15 @@ abstract class BaseApplication extends Component */ public function parseStorage(ConfigProvider $config): void { - if ($storage = $config->get('storage', 'storage')) { - if (!str_contains($storage, APP_PATH)) { - $storage = APP_PATH . $storage . '/'; - } - if (!is_dir($storage)) { - mkdir($storage, 0777, true); - } - if (!is_dir($storage) || !is_writeable($storage)) { - throw new InitException("Directory {$storage} does not have write permission"); - } + $storage = $config->get('storage', 'storage'); + if (!str_contains($storage, APP_PATH)) { + $storage = APP_PATH . $storage . '/'; + } + if (!is_dir($storage)) { + mkdir($storage, 0777, true); + } + if (!is_dir($storage) || !is_writeable($storage)) { + throw new InitException("Directory {$storage} does not have write permission"); } } @@ -116,6 +119,9 @@ abstract class BaseApplication extends Component /** * @param ConfigProvider $config * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException * @throws Exception */ public function parseEvents(ConfigProvider $config): void @@ -123,42 +129,13 @@ abstract class BaseApplication extends Component $events = $config->get('events', []); foreach ($events as $key => $value) { if (is_string($value)) { - $value = Kiri::createObject($value); + $value = $this->container->get($value); + if (!($value instanceof EventInterface)) { + throw new Exception("Event listen must implement " . EventInterface::class); + } + $value = [$value, 'process']; } - $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); } } diff --git a/kiri-engine/Application.php b/kiri-engine/Application.php index 92ac3fd4..e32d4e25 100644 --- a/kiri-engine/Application.php +++ b/kiri-engine/Application.php @@ -19,11 +19,7 @@ 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 -}; +use Symfony\Component\Console\{Application as ConsoleApplication, Input\ArgvInput, Output\ConsoleOutput, Output\OutputInterface}; use Kiri\Server\Events\OnWorkerStart; /** @@ -54,6 +50,10 @@ class Application extends BaseApplication /** * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException + * @throws Exception */ public function init(): void { @@ -61,8 +61,44 @@ class Application extends BaseApplication $this->errorHandler->registerExceptionHandler(\config('error.exception', [])); $this->errorHandler->registerErrorHandler(\config('error.error', [])); $this->id = \config('id', uniqid('id.')); + + $event = $this->container->get(Kiri\Events\EventProvider::class); + $event->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']); } + + /** + * @param OnBeforeCommandExecute $beforeCommandExecute + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException + */ + public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void + { + if (!($beforeCommandExecute->command instanceof Kiri\Server\ServerCommand)) { + $scanner = $this->container->get(Scanner::class); + $scanner->read(APP_PATH . 'app/'); + } else if (\config('reload.hot', false) === false) { + $scanner = $this->container->get(Scanner::class); + $scanner->read(APP_PATH . 'app/'); + } else { + on(OnWorkerStart::class, [$this, 'scanner']); + } + } + + + /** + * @return void + * @throws ReflectionException + */ + public function scanner(): void + { + $scanner = di(Scanner::class); + $scanner->read(APP_PATH . 'app/'); + } + + /** * @param string $service * @return $this @@ -73,7 +109,7 @@ class Application extends BaseApplication if (!class_exists($service)) { return $this; } - $class = Kiri::getDi()->get($service); + $class = $this->container->get($service); if (method_exists($class, 'onImport')) { $class->onImport($this->localService); } @@ -84,6 +120,8 @@ class Application extends BaseApplication /** * @param Kernel $kernel * @return $this + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws ReflectionException */ public function commands(Kernel $kernel): static @@ -98,13 +136,14 @@ class Application extends BaseApplication /** * @param string $command * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws ReflectionException */ public function command(string $command): void { - $container = Kiri::getDi(); - $console = $container->get(ConsoleApplication::class); - $console->add($container->get($command)); + $console = $this->container->get(ConsoleApplication::class); + $console->add($this->container->get($command)); } @@ -118,46 +157,20 @@ class Application extends BaseApplication */ public function execute(array $argv): void { - $container = Kiri::getDi(); - - [$input, $output] = $this->argument($argv); - $console = $container->get(ConsoleApplication::class); - $command = $console->find($input->getFirstArgument()); - - if (!($command instanceof Kiri\Server\ServerCommand)) { - $scanner = $container->get(Scanner::class); - $scanner->read(APP_PATH . 'app/'); - } else if (\config('reload.hot', false) === false) { - $scanner = $container->get(Scanner::class); - $scanner->read(APP_PATH . 'app/'); - } else { - on(OnWorkerStart::class, function () { - $scanner = di(Scanner::class); - $scanner->read(APP_PATH . 'app/'); - }); - } - - fire(new OnBeforeCommandExecute()); - - $command->run($input, $output); - fire(new OnAfterCommandExecute()); - $output->writeln('ok' . PHP_EOL); - } - - - /** - * @param $argv - * @return array - */ - private function argument($argv): array - { - $container = Kiri::getDi(); $input = new ArgvInput($argv); - $container->bind(ArgvInput::class, $input); + $this->container->bind(ArgvInput::class, $input); $output = new ConsoleOutput(); - $container->bind(OutputInterface::class, $output); + $this->container->bind(OutputInterface::class, $output); - return [$input, $output]; + $console = $this->container->get(ConsoleApplication::class); + $command = $console->find($input->getFirstArgument()); + + fire(new OnBeforeCommandExecute($command)); + + $command->run($input, $output); + fire(new OnAfterCommandExecute($command)); + $output->writeln('execute complete.' . PHP_EOL); } + } diff --git a/kiri-engine/Events/OnAfterCommandExecute.php b/kiri-engine/Events/OnAfterCommandExecute.php index cda74145..17fa3b79 100644 --- a/kiri-engine/Events/OnAfterCommandExecute.php +++ b/kiri-engine/Events/OnAfterCommandExecute.php @@ -2,6 +2,8 @@ namespace Kiri\Events; +use Symfony\Component\Console\Command\Command; + class OnAfterCommandExecute { @@ -9,7 +11,7 @@ class OnAfterCommandExecute /** * */ - public function __construct() + public function __construct(public Command $command) { } diff --git a/kiri-engine/Events/OnBeforeCommandExecute.php b/kiri-engine/Events/OnBeforeCommandExecute.php index 885feca4..8c2164dd 100644 --- a/kiri-engine/Events/OnBeforeCommandExecute.php +++ b/kiri-engine/Events/OnBeforeCommandExecute.php @@ -2,7 +2,17 @@ namespace Kiri\Events; +use Symfony\Component\Console\Command\Command; + class OnBeforeCommandExecute { + + /** + * @param Command $command + */ + public function __construct(public Command $command) + { + } + }