diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 32a355d8..8d31a547 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -56,4 +56,14 @@ class Annotation extends Component $this->_loader->_scanDir(new DirectoryIterator($path), $namespace); } + + /** + * @param string $filename + * @return mixed + */ + public function getFilename(string $filename): mixed + { + return $this->_loader->getClassByFilepath($filename); + } + } diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 8d70f3d0..fae6045c 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -24,6 +24,9 @@ class Loader extends BaseObject private array $_classes = []; + private array $_fileMap = []; + + /** * @param $path * @param $namespace @@ -143,7 +146,7 @@ class Loader extends BaseObject continue; } - $_array = ['target' => [], 'methods' => [], 'property' => []]; + $_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []]; foreach ($replace->getAttributes() as $attribute) { if ($attribute->getName() == Attribute::class) { continue; @@ -175,10 +178,26 @@ class Loader extends BaseObject $_array['property'][$method->getName()] = $_property; } + $this->_fileMap[$replace->getFileName()] = $replace->getName(); + + $this->_classes[$replace->getName()] = $_array; } catch (Throwable $throwable) { echo $throwable->getMessage() . PHP_EOL; } } } + + + /** + * @param string $filename + * @return mixed + */ + public function getClassByFilepath(string $filename): mixed + { + if (!isset($this->_fileMap[$filename])) { + return null; + } + return $this->_classes[$this->_fileMap[$filename]]; + } } diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 3564fde6..ff799d98 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -3,6 +3,7 @@ namespace HttpServer; +use Annotation\Attribute; use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\HttpService; use HttpServer\Events\OnClose; @@ -464,7 +465,18 @@ class Server extends HttpService $router->loadRouterSetting(); $attributes = Snowflake::app()->getAttributes(); - $attributes->read(CONTROLLER_PATH, 'App\Http\Controllers', 'controllers'); + + 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]); + } + }); }); } @@ -477,7 +489,18 @@ class Server extends HttpService $event = Snowflake::app()->getEvent(); $event->on(Event::SERVER_WORKER_START, function () { $attributes = Snowflake::app()->getAttributes(); - $attributes->read(SOCKET_PATH, 'App\Websocket', 'sockets'); + + 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]); + } + }); }); }