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