diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 4545ff03..2844f3e7 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -4,11 +4,9 @@ namespace Annotation; -use Database\InjectProperty; use DirectoryIterator; use Exception; use Snowflake\Abstracts\Component; -use Snowflake\Exception\ComponentException; /** * Class Annotation diff --git a/Annotation/Loader.php b/Annotation/Loader.php index c77fc103..cd65d2f2 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -30,8 +30,6 @@ class Loader extends BaseObject private array $_fileMap = []; - private array $_directoryMap = []; - private FileTree $files; @@ -135,74 +133,75 @@ class Loader extends BaseObject public function _scanDir(DirectoryIterator $paths, $namespace) { foreach ($paths as $path) { - if ($path->isDot()) continue; - - if (str_starts_with($path->getFilename(), '.')) { + if ($path->isDot() || str_starts_with($path->getFilename(), '.')) { continue; } if ($path->isDir()) { - $this->_scanDir(new DirectoryIterator($path->getRealPath()), $namespace); - continue; + $iterator = new DirectoryIterator($path->getRealPath()); + $this->_scanDir($iterator, $namespace); + } else { + $this->readFile($path, $namespace); } + } + } + + /** + * @param DirectoryIterator $path + * @param $namespace + * @throws Exception + */ + private function readFile(DirectoryIterator $path, $namespace) + { + try { if ($path->getExtension() !== 'php') { - continue; + return; } - try { - $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); - if (empty($replace) || !$replace->isInstantiable()) { + $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); + if (!$replace->getAttributes(Target::class)) { + return; + } + $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); + + $_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []]; + foreach ($replace->getAttributes() as $attribute) { + if ($attribute->getName() == Attribute::class) { continue; } + $_array['target'][] = $attribute->newInstance(); + } - if (!$replace->getAttributes(Target::class)) { - continue; - } - $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - - $_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []]; - foreach ($replace->getAttributes() as $attribute) { - if ($attribute->getName() == Attribute::class) { + $methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC); + foreach ($methods as $method) { + $_method = []; + foreach ($method->getAttributes() as $attribute) { + if (!class_exists($attribute->getName())) { continue; } - $_array['target'][] = $attribute->newInstance(); + $_method[] = $attribute->newInstance(); } - - $methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC); - foreach ($methods as $method) { - $_method = []; - foreach ($method->getAttributes() as $attribute) { - if (!class_exists($attribute->getName())) { - continue; - } - $_method[] = $attribute->newInstance(); - } - $_array['methods'][$method->getName()] = $_method; - } - - $methods = $replace->getProperties(); - foreach ($methods as $method) { - $_property = []; - if ($method->isStatic()) continue; - foreach ($method->getAttributes() as $attribute) { - if (!class_exists($attribute->getName())) { - continue; - } -// $property = $attribute->newInstance(); -// if ($property instanceof Inject) { -// $property->execute([$_array['handler'], $method]); -// } - $_property[] = $attribute->newInstance(); - } - $_array['property'][$method->getName()] = $_property; - } - - $this->_fileMap[$replace->getFileName()] = $replace->getName(); - - $this->_classes[$replace->getName()] = $_array; - } catch (Throwable $throwable) { - $this->addError($throwable, 'throwable'); + $_array['methods'][$method->getName()] = $_method; } + + $methods = $replace->getProperties(); + foreach ($methods as $method) { + $_property = []; + if ($method->isStatic()) continue; + foreach ($method->getAttributes() as $attribute) { + if (!class_exists($attribute->getName())) { + continue; + } + $_property[] = $attribute->newInstance(); + } + $_array['property'][$method->getName()] = $_property; + } + + $this->_fileMap[$replace->getFileName()] = $replace->getName(); + + $this->_classes[$replace->getName()] = $_array; + } catch (Throwable $throwable) { + $this->addError($throwable, 'throwable'); } } @@ -214,7 +213,7 @@ class Loader extends BaseObject public function loadByDirectory(string $path, ?string $outPath = null) { try { - $this->each($path); + $this->each($path, $outPath); } catch (Throwable $exception) { $this->addError($exception, 'throwable'); } @@ -245,14 +244,11 @@ class Loader extends BaseObject */ public function appendFileToDirectory(string $filePath, string $className) { - $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); + $directory = $this->splitDirectory($filePath); array_pop($DIRECTORY); $tree = null; - foreach ($DIRECTORY as $value) { - if (empty($value)) { - continue; - } + foreach ($directory as $value) { $tree = $this->getTree($tree, $value); } if ($tree instanceof FileTree) { @@ -265,13 +261,17 @@ class Loader extends BaseObject * @param string $filePath * @return $this */ - private function each(string $filePath): static + private function each(string $filePath, string $output): static { - $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); - $tree = null; - foreach ($DIRECTORY as $value) { - if (empty($value)) { + $directory = $this->splitDirectory($filePath); + + $output = DIRECTORY_SEPARATOR . rtrim($output, '/'); + + $out_path = ''; + foreach ($directory as $key => $value) { + $out_path .= DIRECTORY_SEPARATOR . $value; + if ($out_path === $output) { continue; } $tree = $this->getTree($tree, $value); @@ -284,6 +284,19 @@ class Loader extends BaseObject } + /** + * @param string $filePath + * @return false|string[] + */ + private function splitDirectory(string $filePath) + { + $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); + return array_filter($DIRECTORY, function ($value) { + return !empty($value); + }); + } + + /** * @param $tree * @param $value diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 3ef1a56c..fd8ee476 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -50,12 +50,13 @@ class OnWorkerStart extends Callback $this->onTask($server, $worker_id); } else { - $start = microtime(true); - $annotation->instanceDirectoryFiles(APP_PATH); - + $annotation->instanceDirectoryFiles(CONTROLLER_PATH); $this->error('use time ' . (microtime(true) - $start)); + Coroutine\go(function () use ($annotation) { + $annotation->instanceDirectoryFiles(APP_PATH, CONTROLLER_PATH); + }); $this->onWorker($server, $worker_id); }