From 7eee4a057e0d3c4e0ee89df5c8d5004ce41a969d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 3 Mar 2021 19:33:45 +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/Server.php | 54 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/HttpServer/Server.php b/HttpServer/Server.php index ff799d98..0f82d0e5 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -4,6 +4,7 @@ namespace HttpServer; use Annotation\Attribute; +use DirectoryIterator; use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\HttpService; use HttpServer\Events\OnClose; @@ -464,19 +465,7 @@ class Server extends HttpService $router = Snowflake::app()->getRouter(); $router->loadRouterSetting(); - $attributes = Snowflake::app()->getAttributes(); - - recursive_directory(CONTROLLER_PATH, function (\DirectoryIterator $file) use ($attributes) { - $annotations = $attributes->getFilename($file->getRealPath()); - - /** @var Attribute $attribute */ - foreach ($annotations['methods'] as $name => $attribute) { - if (!($attribute instanceof Attribute)) { - continue; - } - $attribute->execute([$annotations['handler'], $name]); - } - }); + recursive_directory(SOCKET_PATH, [$this, 'recursive_callback']); }); } @@ -488,23 +477,36 @@ class Server extends HttpService { $event = Snowflake::app()->getEvent(); $event->on(Event::SERVER_WORKER_START, function () { - $attributes = Snowflake::app()->getAttributes(); - - recursive_directory(SOCKET_PATH, function (\DirectoryIterator $file) use ($attributes) { - $annotations = $attributes->getFilename($file->getRealPath()); - - /** @var Attribute $attribute */ - foreach ($annotations['methods'] as $name => $attribute) { - if (!($attribute instanceof Attribute)) { - continue; - } - $attribute->execute([$annotations['handler'], $name]); - } - }); + recursive_directory(SOCKET_PATH, [$this, 'recursive_callback']); }); } + /** + * @param DirectoryIterator $file + * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException + */ + public function recursive_callback(DirectoryIterator $file) + { + $attributes = Snowflake::app()->getAttributes(); + + $annotations = $attributes->getFilename($file->getRealPath()); + if (empty($annotations)) { + return; + } + + /** @var Attribute $attribute */ + foreach ($annotations['methods'] as $name => $attribute) { + if (!($attribute instanceof Attribute)) { + continue; + } + $attribute->execute([$annotations['handler'], $name]); + } + } + + /** * @param $type * @return string