This commit is contained in:
2021-03-03 19:35:44 +08:00
parent 7eee4a057e
commit edf82a4bf1
2 changed files with 31 additions and 31 deletions
+2 -26
View File
@@ -465,7 +465,7 @@ class Server extends HttpService
$router = Snowflake::app()->getRouter(); $router = Snowflake::app()->getRouter();
$router->loadRouterSetting(); $router->loadRouterSetting();
recursive_directory(SOCKET_PATH, [$this, 'recursive_callback']); recursive_directory(SOCKET_PATH);
}); });
} }
@@ -477,35 +477,11 @@ class Server extends HttpService
{ {
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_START, function () { $event->on(Event::SERVER_WORKER_START, function () {
recursive_directory(SOCKET_PATH, [$this, 'recursive_callback']); recursive_directory(SOCKET_PATH);
}); });
} }
/**
* @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 * @param $type
+29 -5
View File
@@ -10,6 +10,7 @@ use HttpServer\Http\Response;
use HttpServer\Route\Router; use HttpServer\Route\Router;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use Snowflake\Core\ArrayAccess; use Snowflake\Core\ArrayAccess;
@@ -69,10 +70,33 @@ if (!function_exists('recursive_directory')) {
/** /**
* @param string $path * @param DirectoryIterator $file
* @param array|Closure $callback * @throws ComponentException
* @throws ReflectionException
* @throws NotFindClassException
*/ */
function recursive_directory(string $path, array|Closure $callback) 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 string $path
*/
function recursive_directory(string $path)
{ {
$directoryIterators = new \DirectoryIterator($path); $directoryIterators = new \DirectoryIterator($path);
foreach ($directoryIterators as $directoryIterator) { foreach ($directoryIterators as $directoryIterator) {
@@ -80,9 +104,9 @@ if (!function_exists('recursive_directory')) {
continue; continue;
} }
if ($directoryIterator->isDir()) { if ($directoryIterator->isDir()) {
Recursive_directory($directoryIterator->getRealPath(), $callback); Recursive_directory($directoryIterator->getRealPath());
} else { } else {
call_user_func($callback, $directoryIterator); call_user_func('recursive_callback', $directoryIterator);
} }
} }
unset($directoryIterators); unset($directoryIterators);