From d46762abbb3ed49189eff6a703fcdce323c995be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 2 Apr 2021 18:57:56 +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 --- HttpServer/Route/Router.php | 13 ++- System/Application.php | 195 ++++++++++++++++++++---------------- 2 files changed, 116 insertions(+), 92 deletions(-) diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index f2c41f79..8376d4aa 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -43,7 +43,7 @@ class Router extends HttpService implements RouterInterface public ?Closure $middleware = null; - public bool $useTree = false; + public int $useTree = ROUTER_TREE; /** @@ -64,12 +64,13 @@ class Router extends HttpService implements RouterInterface $this->dir = Config::get('http.namespace', false, $this->dir); } + /** * @param bool $useTree */ public function setUseTree(bool $useTree): void { - $this->useTree = $useTree; + $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; } @@ -101,7 +102,6 @@ class Router extends HttpService implements RouterInterface * @param $handler * @param string $method * @return ?Node - * @throws ConfigException */ public function addRoute($path, $handler, $method = 'any'): ?Node { @@ -110,8 +110,7 @@ class Router extends HttpService implements RouterInterface $this->nodes[$method] = []; } - $useTree = Config::get('router', false, ROUTER_TREE); - if ($useTree == ROUTER_TREE) { + if ($this->useTree == ROUTER_TREE) { return $this->tree($path, $handler, $method); } else { return $this->hash($path, $handler, $method); @@ -515,7 +514,7 @@ class Router extends HttpService implements RouterInterface } return $node->afterDispatch($response); } catch (\Throwable $exception) { - $this->addError($exception,'throwable'); + $this->addError($exception, 'throwable'); $Code = $exception->getCode() == 0 ? 500 : $exception->getCode(); @@ -658,7 +657,7 @@ class Router extends HttpService implements RouterInterface $router = $this; include_once "{$files}"; } catch (\Throwable $exception) { - $this->addError($exception,'throwable'); + $this->addError($exception, 'throwable'); } finally { if (isset($exception)) { unset($exception); diff --git a/System/Application.php b/System/Application.php index 3c2a5e09..648461dd 100644 --- a/System/Application.php +++ b/System/Application.php @@ -11,6 +11,7 @@ namespace Snowflake; use Annotation\Aspect; +use Closure; use Console\Console; use Console\ConsoleProviders; use Database\DatabasesProviders; @@ -36,104 +37,128 @@ use Swoole\Timer; class Application extends BaseApplication { - /** - * @var string - */ - public string $id = 'uniqueId'; + /** + * @var string + */ + public string $id = 'uniqueId'; - public string $state = ''; + public string $state = ''; - /** - * @throws NotFindClassException - */ - public function init() - { - $this->import(ConsoleProviders::class); - $this->import(DatabasesProviders::class); - $this->import(ServerProviders::class); + /** + * @throws NotFindClassException + */ + public function init() + { + $this->import(ConsoleProviders::class); + $this->import(DatabasesProviders::class); + $this->import(ServerProviders::class); - $this->import(CrontabProviders::class); - } + $this->import(CrontabProviders::class); + } - /** - * @param string $service - * @return $this - * @throws - */ - public function import(string $service): static - { - if (!class_exists($service)) { - throw new NotFindClassException($service); - } - $class = Snowflake::createObject($service); - if (method_exists($class, 'onImport')) { - $class->onImport($this); - } - return $this; - } + /** + * @param Closure|array $closure + * @return $this + * @throws Exception + */ + public function middleware(Closure|array $closure): static + { + $this->getRouter()->setMiddleware($closure); + return $this; + } - /** - * @param $kernel - * @return $this - */ - public function commands(Kernel $kernel): static - { - foreach ($kernel->getCommands() as $command) { - $this->register($command); - } - return $this; - } + /** + * @param bool $useTree + * @return $this + * @throws Exception + */ + public function setUseTree(bool $useTree): static + { + $this->getRouter()->setUseTree($useTree); + return $this; + } - /** - * @param string $command - * @throws - */ - public function register(string $command) - { - /** @var Console $abstracts */ - $abstracts = $this->get('console'); - $abstracts->register($command); - } + /** + * @param string $service + * @return $this + * @throws + */ + public function import(string $service): static + { + if (!class_exists($service)) { + throw new NotFindClassException($service); + } + $class = Snowflake::createObject($service); + if (method_exists($class, 'onImport')) { + $class->onImport($this); + } + return $this; + } - /** - * @param Input $argv - * @return void - * @throws Exception - */ - public function start(Input $argv): void - { - try { - fire(Event::SERVER_BEFORE_START); + /** + * @param $kernel + * @return $this + */ + public function commands(Kernel $kernel): static + { + foreach ($kernel->getCommands() as $command) { + $this->register($command); + } + return $this; + } - $manager = Snowflake::app()->get('console'); - $manager->setParameters($argv); - $class = $manager->search(); - response()->send($manager->execCommand($class)); - } catch (\Throwable $exception) { - response()->send(implode("\n", [ - 'Msg: ' . $exception->getMessage(), - 'Line: ' . $exception->getLine(), - 'File: ' . $exception->getFile() - ])); - } finally { - Timer::clearAll(); - } - } - /** - * @param $className - * @param null $abstracts - * @return stdClass - * @throws Exception - */ - public function make($className, $abstracts = null): stdClass - { - return make($className, $abstracts); - } + /** + * @param string $command + * @throws + */ + public function register(string $command) + { + /** @var Console $abstracts */ + $abstracts = $this->get('console'); + $abstracts->register($command); + } + + + /** + * @param Input $argv + * @return void + * @throws Exception + */ + public function start(Input $argv): void + { + try { + fire(Event::SERVER_BEFORE_START); + + $manager = Snowflake::app()->get('console'); + $manager->setParameters($argv); + $class = $manager->search(); + response()->send($manager->execCommand($class)); + } catch (\Throwable $exception) { + response()->send(implode("\n", [ + 'Msg: ' . $exception->getMessage(), + 'Line: ' . $exception->getLine(), + 'File: ' . $exception->getFile() + ])); + } finally { + Timer::clearAll(); + } + } + + /** + * @param $className + * @param null $abstracts + * @return stdClass + * @throws Exception + */ + public function make($className, $abstracts = null): stdClass + { + return make($className, $abstracts); + } }