From 6180cc31257872eb680ea57147c9d79e0a9a3dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 4 Mar 2021 10:19: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 --- Annotation/Annotation.php | 11 +++++++++++ Annotation/Loader.php | 25 +++++++++++++++++++++++++ HttpServer/Server.php | 4 ++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 63ec495f..0eb9e1c9 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -6,6 +6,7 @@ namespace Annotation; use DirectoryIterator; use Snowflake\Abstracts\Component; +use Snowflake\Exception\ComponentException; /** * Class Annotation @@ -50,6 +51,7 @@ class Annotation extends Component * @param string $namespace * @param string $alias * @return void + * @throws ComponentException */ public function read(string $path, string $namespace, string $alias = 'root'): void { @@ -58,6 +60,15 @@ class Annotation extends Component } + /** + * @param string $dir + */ + public function instanceDirectoryFiles(string $dir) + { + $this->_loader->loadByDirectory($dir); + } + + /** * @param string $filename * @return mixed diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 67ed65d4..41166e4f 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -10,6 +10,7 @@ use ReflectionClass; use ReflectionMethod; use ReflectionProperty; use Snowflake\Abstracts\BaseObject; +use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; use Throwable; @@ -119,6 +120,7 @@ class Loader extends BaseObject /** * @param DirectoryIterator $paths * @param $namespace + * @throws ComponentException */ public function _scanDir(DirectoryIterator $paths, $namespace) { @@ -199,6 +201,29 @@ class Loader extends BaseObject } + /** + * @param string $path + */ + public function loadByDirectory(string $path) + { + foreach ($this->_fileMap as $value) { + if (!str_starts_with($value, $path)) { + continue; + } + $annotations = $this->getClassByFilepath($path); + + foreach ($annotations['methods'] as $name => $attribute) { + foreach ($attribute as $value) { + if (!($value instanceof \Annotation\Attribute)) { + continue; + } + $value->execute([$annotations['handler'], $name]); + } + } + } + } + + /** * @param DirectoryIterator $path * @param string $namespace diff --git a/HttpServer/Server.php b/HttpServer/Server.php index c78dba41..e88853fc 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -465,7 +465,7 @@ class Server extends HttpService $router = Snowflake::app()->getRouter(); $router->loadRouterSetting(); - recursive_directory(CONTROLLER_PATH); + annotation()->instanceDirectoryFiles(CONTROLLER_PATH); }); } @@ -477,7 +477,7 @@ class Server extends HttpService { $event = Snowflake::app()->getEvent(); $event->on(Event::SERVER_WORKER_START, function () { - recursive_directory(SOCKET_PATH); + annotation()->instanceDirectoryFiles(SOCKET_PATH); }); }