This commit is contained in:
2021-03-03 19:33:45 +08:00
parent 6ee5fdae3c
commit 7eee4a057e
+28 -26
View File
@@ -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