From 3d77ed9d1061b760bba05121c1ac01b5bdde434a Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Thu, 8 Apr 2021 01:07:48 +0800 Subject: [PATCH] modify --- Annotation/Annotation.php | 8 +-- Annotation/FileTree.php | 66 ++++++++++++++++++ Annotation/Loader.php | 143 ++++++++++++++++++-------------------- 3 files changed, 136 insertions(+), 81 deletions(-) create mode 100644 Annotation/FileTree.php diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 468ac9e2..4545ff03 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -95,10 +95,10 @@ class Annotation extends Component * @param string $dir * @throws Exception */ - public function runtime(string $dir, ?string $outPath = null) - { - $this->_loader->directoryRuntime($dir, $outPath); - } +// public function runtime(string $dir, ?string $outPath = null) +// { +// $this->_loader->directoryRuntime($dir, $outPath); +// } /** diff --git a/Annotation/FileTree.php b/Annotation/FileTree.php new file mode 100644 index 00000000..36cb1332 --- /dev/null +++ b/Annotation/FileTree.php @@ -0,0 +1,66 @@ +childes[$path])) { + $this->addChild($path, new FileTree()); + } + return $this->childes[$path]; + } + + + /** + * @param string $path + * @param FileTree $fileTree + */ + public function addChild(string $path, FileTree $fileTree) + { + $this->childes[$path] = $fileTree; + } + + + /** + * @param string $className + */ + public function addFile(string $className) + { + $this->files[] = $className; + } + + + /** + * @return array + */ + public function getFiles(): array + { + return $this->files; + } + + + /** + * @return array + */ + public function getChildes(): array + { + return $this->childes; + } + + +} diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 1cb2203b..73a67d50 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -32,6 +32,14 @@ class Loader extends BaseObject private array $_directoryMap = []; + private FileTree $files; + + + public function init() + { + $this->files = new FileTree(); + } + /** * @param $path @@ -126,8 +134,6 @@ class Loader extends BaseObject */ public function _scanDir(DirectoryIterator $paths, $namespace) { - /** @var DirectoryIterator $path */ - $DIRECTORY = $this->createDirectoryMap($paths); foreach ($paths as $path) { if ($path->isDot()) continue; @@ -208,30 +214,33 @@ class Loader extends BaseObject public function loadByDirectory(string $path, ?string $outPath = null) { try { - foreach ($this->_fileMap as $fileName => $className) { - if (!str_starts_with($fileName, $path)) { - continue; - } - if (!isset($this->_classes[$className])) { - continue; - } - $annotations = $this->_classes[$className]; - if (isset($annotations['target']) && !empty($annotations['target'])) { - foreach ($annotations['target'] as $value) { - $value->execute([$annotations['handler']]); - } - } + return $this->each($path); - foreach ($annotations['methods'] as $name => $attribute) { - foreach ($attribute as $value) { - if (!($value instanceof \Annotation\Attribute)) { - continue; - } - $value->execute([$annotations['handler'], $name]); - } - } - } +// foreach ($this->_fileMap as $fileName => $className) { +// if (!str_starts_with($fileName, $path)) { +// continue; +// } +// if (!isset($this->_classes[$className])) { +// continue; +// } +// +// $annotations = $this->_classes[$className]; +// if (isset($annotations['target']) && !empty($annotations['target'])) { +// foreach ($annotations['target'] as $value) { +// $value->execute([$annotations['handler']]); +// } +// } +// +// foreach ($annotations['methods'] as $name => $attribute) { +// foreach ($attribute as $value) { +// if (!($value instanceof \Annotation\Attribute)) { +// continue; +// } +// $value->execute([$annotations['handler'], $name]); +// } +// } +// } } catch (Throwable $exception) { $this->addError($exception, 'throwable'); } @@ -256,38 +265,6 @@ class Loader extends BaseObject } - /** - * @param DirectoryIterator $directoryIterator - * @return string - */ - public function createDirectoryMap(DirectoryIterator $directoryIterator): string - { - $DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath()); - array_pop($DIRECTORY); - - $path = DIRECTORY_SEPARATOR; - foreach ($DIRECTORY as $value) { - $path = $this->makeMoneyDirectoryArray($path, $value); - } - return $DIRECTORY; - } - - - /** - * @param $path - * @param $value - * @return string - */ - private function makeMoneyDirectoryArray($path, $value) - { - $path .= $value . DIRECTORY_SEPARATOR; - if (!isset($this->_directoryMap[$path])) { - $this->_directoryMap[$path] = []; - } - return $path; - } - - /** * @param string $filePath * @param string $className @@ -297,38 +274,52 @@ class Loader extends BaseObject $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); array_pop($DIRECTORY); - $path = DIRECTORY_SEPARATOR; + $tree = $this->files; foreach ($DIRECTORY as $value) { - $path = $this->makeMoneyDirectoryArray($path, $value); + $tree = $tree->getChild($value); - $this->_directoryMap[$path][] = $className; + $tree->addFile($className); } } /** - * @param string $Directory - * @return array + * @param string $filePath + * @return $this */ - public function getDirectoryFiles(string $Directory): array + private function each(string $filePath): static { - if (!isset($this->_directoryMap[$Directory])) { - return []; + $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); + + $tree = null; + foreach ($DIRECTORY as $value) { + if ($tree === null) { + $tree = $this->files->getChild($value); + } else { + $tree = $tree->getChild($value); + } } - return $this->_directoryMap[$Directory]; + if ($tree instanceof FileTree) { + $this->eachNode($tree->getChildes()); + $this->execute($tree->getFiles()); + } + + return $this; } /** - * @param string $Directory - * @param string|null $output + * @param FileTree[] $nodes */ - public function directoryRuntime(string $Directory, ?string $output) + private function eachNode(array $nodes) { - if (!empty($output) && isset($this->_directoryMap[$output])) { - unset($this->_directoryMap[$output]); + foreach ($nodes as $node) { + $childes = $node->getChildes(); + if (!empty($childes)) { + $this->eachNode($childes); + } + $this->execute($node->getFiles()); } - $this->execute($Directory); } @@ -346,16 +337,14 @@ class Loader extends BaseObject /** - * @param $directory + * @param array $classes */ - private function execute(string $directory) + private function execute(array $classes) { - if (!isset($this->_directoryMap[$directory])) { + if (empty($classes)) { return; } - - $directories = $this->_directoryMap[$directory]; - foreach ($directories as $className) { + foreach ($classes as $className) { if (!isset($this->_classes[$className])) { continue; }