From fce833dddedc5b166d84e9f5ac6988e6f8f9c6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 24 Sep 2021 18:53:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-handler/Abstracts/MiddlewareManager.php | 31 ++-- http-server/ServerCommand.php | 168 ++++++++++--------- 2 files changed, 112 insertions(+), 87 deletions(-) diff --git a/http-handler/Abstracts/MiddlewareManager.php b/http-handler/Abstracts/MiddlewareManager.php index 327e2669..afe136eb 100644 --- a/http-handler/Abstracts/MiddlewareManager.php +++ b/http-handler/Abstracts/MiddlewareManager.php @@ -26,19 +26,14 @@ class MiddlewareManager extends BaseObject /** * @param $class * @param $method - * @param array|string $middlewares + * @param array|string|null $middlewares * @return bool */ - public static function add($class, $method, array|string $middlewares): bool + public static function add($class, $method, array|string|null $middlewares): bool { - if (is_object($class)) { - $class = $class::class; - } - if (!isset(static::$_middlewares[$class])) { - static::$_middlewares[$class] = []; - } - if (!isset(static::$_middlewares[$class][$method])) { - static::$_middlewares[$class][$method] = new Iterator(); + [$class, $method] = static::setDefault($class, $method); + if (empty($middlewares)) { + return false; } if (is_string($middlewares)) { $middlewares = [$middlewares]; @@ -54,6 +49,22 @@ class MiddlewareManager extends BaseObject } + private static function setDefault($class, $method): array + { + if (is_object($class)) { + $class = $class::class; + } + + if (!isset(static::$_middlewares[$class])) { + static::$_middlewares[$class] = []; + } + if (!isset(static::$_middlewares[$class][$method])) { + static::$_middlewares[$class][$method] = new Iterator(); + } + return [$class, $method]; + } + + /** * @param $handler * @return Iterator|null diff --git a/http-server/ServerCommand.php b/http-server/ServerCommand.php index 38535df4..bbe0534d 100644 --- a/http-server/ServerCommand.php +++ b/http-server/ServerCommand.php @@ -6,17 +6,21 @@ namespace Server; use Annotation\Inject; use Exception; +use Http\Handler\Abstracts\HandlerManager; +use Http\Handler\Abstracts\MiddlewareManager; +use Http\Handler\Handler; use Kiri\Abstracts\Config; use Kiri\Events\EventProvider; +use Kiri\Exception\ConfigException; use Kiri\Kiri; +use Server\Abstracts\OnTaskerStart as TaskerDispatch; +use Server\Abstracts\OnWorkerStart as WorkerDispatch; +use Server\Events\OnAfterWorkerStart; use Server\Events\OnBeforeWorkerStart; use Server\Events\OnTaskerStart; use Server\Events\OnWorkerStart; use Server\Worker\OnServerWorker; -use Server\Abstracts\OnWorkerStart as WorkerDispatch; -use Server\Abstracts\OnTaskerStart as TaskerDispatch; use Swoole\Coroutine; -use Swoole\Runtime; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -30,92 +34,102 @@ class ServerCommand extends Command { - const ACTIONS = ['start', 'stop', 'restart']; + const ACTIONS = ['start', 'stop', 'restart']; - /** - * @var EventProvider - */ - #[Inject(EventProvider::class)] - public EventProvider $eventProvider; + /** + * @var EventProvider + */ + #[Inject(EventProvider::class)] + public EventProvider $eventProvider; - /** - * - */ - protected function configure() - { - $this->setName('sw:server') - ->setDescription('server start|stop|reload|restart') - ->addArgument('action', InputArgument::REQUIRED) - ->addArgument('daemon', InputArgument::OPTIONAL, '', 0); - } + /** + * + */ + protected function configure() + { + $this->setName('sw:server') + ->setDescription('server start|stop|reload|restart') + ->addArgument('action', InputArgument::REQUIRED) + ->addArgument('daemon', InputArgument::OPTIONAL, '', 0); + } - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int - * @throws Exception - */ - public function execute(InputInterface $input, OutputInterface $output): int - { - try { - $manager = Kiri::app()->getServer(); - $manager->setDaemon($input->getArgument('daemon')); - if (!in_array($input->getArgument('action'), self::ACTIONS)) { - throw new Exception('I don\'t know what I want to do.'); - } - if ($manager->isRunner() && $input->getArgument('action') == 'start') { - throw new Exception('Service is running. Please use restart.'); - } - $manager->shutdown(); - if ($input->getArgument('action') == 'stop') { - throw new Exception('shutdown success'); - } - $this->generate_runtime_builder($manager); - } catch (\Throwable $throwable) { - $output->write($throwable->getMessage()); - } finally { - return 1; - } - } + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + * @throws Exception + */ + public function execute(InputInterface $input, OutputInterface $output): int + { + try { + $manager = Kiri::app()->getServer(); + $manager->setDaemon($input->getArgument('daemon')); + if (!in_array($input->getArgument('action'), self::ACTIONS)) { + throw new Exception('I don\'t know what I want to do.'); + } + if ($manager->isRunner() && $input->getArgument('action') == 'start') { + throw new Exception('Service is running. Please use restart.'); + } + $manager->shutdown(); + if ($input->getArgument('action') == 'stop') { + throw new Exception('shutdown success'); + } + $this->generate_runtime_builder($manager); + } catch (\Throwable $throwable) { + $output->write($throwable->getMessage()); + } finally { + return 1; + } + } - /** - * @throws \Kiri\Exception\ConfigException - */ - private function configure_set() - { - $enable_coroutine = Config::get('servers.settings.enable_coroutine', false); - if ($enable_coroutine != true) { - return; - } - Coroutine::set([ - 'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION, - 'enable_deadlock_check' => FALSE, - 'exit_condition' => function () { - return Coroutine::stats()['coroutine_num'] === 0; - } - ]); - } + /** + * @throws ConfigException + */ + private function configure_set() + { + $enable_coroutine = Config::get('servers.settings.enable_coroutine', false); + if ($enable_coroutine != true) { + return; + } + Coroutine::set([ + 'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION, + 'enable_deadlock_check' => FALSE, + 'exit_condition' => function () { + return Coroutine::stats()['coroutine_num'] === 0; + } + ]); + } - /** - * @param $manager - * @throws \Kiri\Exception\ConfigException - */ - private function generate_runtime_builder($manager): void - { - $this->configure_set(); + /** + * @param $manager + * @throws ConfigException + */ + private function generate_runtime_builder($manager): void + { + $this->configure_set(); - exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder'); + exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder'); - $this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']); - $this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']); - $this->eventProvider->on(OnTaskerStart::class, [di(TaskerDispatch::class), 'dispatch']); + $this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']); + $this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']); + $this->eventProvider->on(OnTaskerStart::class, [di(TaskerDispatch::class), 'dispatch']); - $manager->start(); - } + $this->eventProvider->on(OnAfterWorkerStart::class, function () { + $lists = HandlerManager::dump(); + foreach ($lists as $list) { + if ($list instanceof \Closure) { + continue; + } + /** @var Handler|Closure $list */ + MiddlewareManager::add($list->callback[0], $list->callback[1], null); + } + }); + $manager->start(); + } }