From e4e11aaadd1d86a1b8aed81ddd65b97b2f0ed528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 9 Apr 2021 09:51:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/Loader.php | 598 +++++++++++++++++++++--------------------- System/Runtime.php | 7 +- 2 files changed, 305 insertions(+), 300 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 34939aea..e6e1b7e7 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -7,11 +7,8 @@ namespace Annotation; use Attribute; use DirectoryIterator; use Exception; -use ReflectionClass; use ReflectionMethod; -use ReflectionProperty; use Snowflake\Abstracts\BaseObject; -use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; use Throwable; @@ -24,350 +21,357 @@ class Loader extends BaseObject { - private array $_classes = []; + private array $_classes = []; - private array $_fileMap = []; + private array $_fileMap = []; - private FileTree $files; + private FileTree $files; - public function init() - { - $this->files = new FileTree(); - } + public function init() + { + $this->files = new FileTree(); + } - /** - * @param $path - * @param $namespace - * @throws Exception - */ - public function loader($path, $namespace) - { - $this->_scanDir(new DirectoryIterator($path), $namespace); - } + /** + * @param $path + * @param $namespace + * @throws Exception + */ + public function loader($path, $namespace) + { + $this->_scanDir(new DirectoryIterator($path), $namespace); + } - /** - * @return array - */ - public function getClasses(): array - { - return $this->_classes; - } + /** + * @return array + */ + public function getClasses(): array + { + return $this->_classes; + } - /** - * @param string $class - * @param string $property - * @return mixed - */ - public function getProperty(string $class, string $property = ''): mixed - { - if (!isset($this->_classes[$class])) { - return null; - } - $properties = $this->_classes[$class]['property']; - if (!empty($property) && isset($properties[$property])) { - return $properties[$property]; - } - return $properties; - } + /** + * @param string $class + * @param string $property + * @return mixed + */ + public function getProperty(string $class, string $property = ''): mixed + { + if (!isset($this->_classes[$class])) { + return null; + } + $properties = $this->_classes[$class]['property']; + if (!empty($property) && isset($properties[$property])) { + return $properties[$property]; + } + return $properties; + } - /** - * @param string $class - * @param mixed $handler - * @return Loader - */ - public function injectProperty(string $class, object $handler): static - { - $properties = $this->getProperty($class); - if (empty($properties)) { - return $this; - } - foreach ($properties as $property => $attributes) { - foreach ($attributes as $attribute) { - $attribute->execute([$handler, $property]); - } - } - return $this; - } + /** + * @param string $class + * @param mixed $handler + * @return Loader + */ + public function injectProperty(string $class, object $handler): static + { + $properties = $this->getProperty($class); + if (empty($properties)) { + return $this; + } + foreach ($properties as $property => $attributes) { + foreach ($attributes as $attribute) { + $attribute->execute([$handler, $property]); + } + } + return $this; + } - /** - * @param string $class - * @param string $method - * @return mixed - */ - public function getMethod(string $class, string $method = ''): array - { - if (!isset($this->_classes[$class])) { - return []; - } - $properties = $this->_classes[$class]['methods']; - if (!empty($method) && isset($properties[$method])) { - return $properties[$method]; - } - return $properties; - } + /** + * @param string $class + * @param string $method + * @return mixed + */ + public function getMethod(string $class, string $method = ''): array + { + if (!isset($this->_classes[$class])) { + return []; + } + $properties = $this->_classes[$class]['methods']; + if (!empty($method) && isset($properties[$method])) { + return $properties[$method]; + } + return $properties; + } - /** - * @param string $class - * @return array - */ - public function getTarget(string $class): array - { - return $this->_classes[$class] ?? []; - } + /** + * @param string $class + * @return array + */ + public function getTarget(string $class): array + { + return $this->_classes[$class] ?? []; + } - /** - * @param DirectoryIterator $paths - * @param $namespace - * @throws Exception - */ - public function _scanDir(DirectoryIterator $paths, $namespace) - { - foreach ($paths as $path) { - if ($path->isDot() || str_starts_with($path->getFilename(), '.')) { - continue; - } - if ($path->isDir()) { - $iterator = new DirectoryIterator($path->getRealPath()); - $this->_scanDir($iterator, $namespace); - } else { - $this->readFile($path, $namespace); - } - } - } + /** + * @param DirectoryIterator $paths + * @param $namespace + * @throws Exception + */ + public function _scanDir(DirectoryIterator $paths, $namespace) + { + foreach ($paths as $path) { + if ($path->isDot() || str_starts_with($path->getFilename(), '.')) { + continue; + } + if ($path->isDir()) { + $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') { - return; - } + /** + * @param DirectoryIterator $path + * @param $namespace + * @throws Exception + */ + private function readFile(DirectoryIterator $path, $namespace) + { + try { + if ($path->getExtension() !== 'php') { + return; + } - $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); - if (empty($replace) || !$replace->getAttributes(Target::class)) { - return; - } - $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); + $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); + if (empty($replace) || !$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(); - } + $_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []]; + foreach ($replace->getAttributes() as $attribute) { + if ($attribute->getName() == Attribute::class) { + continue; + } + $_array['target'][] = $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->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(); - } - $_array['property'][$method->getName()] = $_property; - } + $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->_fileMap[$replace->getFileName()] = $replace->getName(); - $this->_classes[$replace->getName()] = $_array; - } catch (Throwable $throwable) { - $this->addError($throwable, 'throwable'); - } - } + $this->_classes[$replace->getName()] = $_array; + } catch (Throwable $throwable) { + $this->addError($throwable, 'throwable'); + } + } - /** - * @param string $path - * @throws Exception - */ - public function loadByDirectory(string $path, ?string $outPath = null) - { - try { - $this->each($path, $outPath); - } catch (Throwable $exception) { - $this->addError($exception, 'throwable'); - } - } + /** + * @param string $path + * @param string|null $outPath + * @throws Exception + */ + public function loadByDirectory(string $path, ?string $outPath = null) + { + try { + $this->each($path, $outPath); + } catch (Throwable $exception) { + $this->addError($exception, 'throwable'); + } + } - /** - * @param DirectoryIterator $path - * @param string $namespace - * @return string - */ - private function explodeFileName(DirectoryIterator $path, string $namespace): string - { - $replace = str_replace(APP_PATH . 'app', '', $path->getRealPath()); + /** + * @param DirectoryIterator $path + * @param string $namespace + * @return string + */ + private function explodeFileName(DirectoryIterator $path, string $namespace): string + { + $replace = str_replace(APP_PATH . 'app', '', $path->getRealPath()); - $replace = str_replace('.php', '', $replace); - $replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace); - $explode = explode('\\', $replace); - array_shift($explode); + $replace = str_replace('.php', '', $replace); + $replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace); + $explode = explode('\\', $replace); + array_shift($explode); - return $namespace . '\\' . implode('\\', $explode); - } + return $namespace . '\\' . implode('\\', $explode); + } - /** - * @param string $filePath - * @param string $className - */ - public function appendFileToDirectory(string $filePath, string $className) - { - $directory = $this->splitDirectory($filePath); - array_pop($directory); + /** + * @param string $filePath + * @param string $className + */ + public function appendFileToDirectory(string $filePath, string $className) + { + $filePath = str_replace(APP_PATH, '', $filePath); - $tree = null; - foreach ($directory as $value) { - $tree = $this->getTree($tree, $value); - } - if ($tree instanceof FileTree) { - $tree->addFile($className); - } - } + $directory = $this->splitDirectory($filePath); + array_pop($directory); + + $tree = null; + foreach ($directory as $value) { + $tree = $this->getTree($tree, $value); + } + if ($tree instanceof FileTree) { + $tree->addFile($className); + } + } - /** - * @param string $filePath - * @return $this - */ - private function each(string $filePath, ?string $output): static - { - $tree = null; - $directory = $this->splitDirectory($filePath); + /** + * @param string $filePath + * @param string|null $output + * @return $this + */ + private function each(string $filePath, ?string $output): static + { + $tree = null; + $filePath = str_replace(APP_PATH, '', $filePath); - if (!empty($output)) { - $output = DIRECTORY_SEPARATOR . trim($output, '/'); - } + $directory = $this->splitDirectory($filePath); - $out_path = ''; - foreach ($directory as $key => $value) { - $out_path .= DIRECTORY_SEPARATOR . $value; - if ($out_path === $output) { - break; - } - $tree = $this->getTree($tree, $value); - } - if ($tree instanceof FileTree) { - $this->eachNode($tree->getChildes()); - $this->execute($tree->getFiles()); - } - return $this; - } + if (!empty($output)) { + $output = DIRECTORY_SEPARATOR . trim($output, '/'); + } + $output = str_replace(APP_PATH, '', $output); + + $out_path = ''; + foreach ($directory as $key => $value) { + $out_path .= DIRECTORY_SEPARATOR . $value; + if ($out_path === $output) { + break; + } + $tree = $this->getTree($tree, $value); + } + if ($tree instanceof FileTree) { + $this->eachNode($tree->getChildes()); + $this->execute($tree->getFiles()); + } + return $this; + } - /** - * @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 string $filePath + * @return false|string[] + */ + private function splitDirectory(string $filePath): array|bool + { + $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); + return array_filter($DIRECTORY, function ($value) { + return !empty($value); + }); + } - /** - * @param $tree - * @param $value - * @return FileTree - */ - private function getTree($tree, $value): FileTree - { - if ($tree === null) { - $tree = $this->files->getChild($value); - } else { - $tree = $tree->getChild($value); - } - return $tree; - } + /** + * @param $tree + * @param $value + * @return FileTree + */ + private function getTree($tree, $value): FileTree + { + if ($tree === null) { + $tree = $this->files->getChild($value); + } else { + $tree = $tree->getChild($value); + } + return $tree; + } - /** - * @param FileTree[] $nodes - */ - private function eachNode(array $nodes) - { - foreach ($nodes as $node) { - $childes = $node->getChildes(); - if (!empty($childes)) { - $this->eachNode($childes); - } - $this->execute($node->getFiles()); - } - } + /** + * @param FileTree[] $nodes + */ + private function eachNode(array $nodes) + { + foreach ($nodes as $node) { + $childes = $node->getChildes(); + if (!empty($childes)) { + $this->eachNode($childes); + } + $this->execute($node->getFiles()); + } + } - /** - * @param string $filename - * @return mixed - */ - public function getClassByFilepath(string $filename): mixed - { - if (!isset($this->_fileMap[$filename])) { - return null; - } - return $this->_classes[$this->_fileMap[$filename]]; - } + /** + * @param string $filename + * @return mixed + */ + public function getClassByFilepath(string $filename): mixed + { + if (!isset($this->_fileMap[$filename])) { + return null; + } + return $this->_classes[$this->_fileMap[$filename]]; + } - /** - * @param array $classes - */ - private function execute(array $classes) - { - if (empty($classes)) { - return; - } - foreach ($classes as $className) { - $annotations = $this->_classes[$className] ?? null; - if ($annotations === null) { - continue; - } - 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]); - } - } - } - } + /** + * @param array $classes + */ + private function execute(array $classes) + { + if (empty($classes)) { + return; + } + foreach ($classes as $className) { + $annotations = $this->_classes[$className] ?? null; + if ($annotations === null) { + continue; + } + 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]); + } + } + } + } } diff --git a/System/Runtime.php b/System/Runtime.php index 693902bc..f95d8c4e 100644 --- a/System/Runtime.php +++ b/System/Runtime.php @@ -19,9 +19,10 @@ class Runtime extends Command public string $command = 'runtime:builder'; - /** - * @param Input $dtl - */ + /** + * @param Input $dtl + * @throws \Exception + */ public function onHandler(Input $dtl) { // TODO: Implement onHandler() method.