This commit is contained in:
2021-04-09 09:51:17 +08:00
parent 59c4981913
commit e4e11aaadd
2 changed files with 305 additions and 300 deletions
+301 -297
View File
@@ -7,11 +7,8 @@ namespace Annotation;
use Attribute; use Attribute;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use ReflectionClass;
use ReflectionMethod; use ReflectionMethod;
use ReflectionProperty;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Throwable; 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() public function init()
{ {
$this->files = new FileTree(); $this->files = new FileTree();
} }
/** /**
* @param $path * @param $path
* @param $namespace * @param $namespace
* @throws Exception * @throws Exception
*/ */
public function loader($path, $namespace) public function loader($path, $namespace)
{ {
$this->_scanDir(new DirectoryIterator($path), $namespace); $this->_scanDir(new DirectoryIterator($path), $namespace);
} }
/** /**
* @return array * @return array
*/ */
public function getClasses(): array public function getClasses(): array
{ {
return $this->_classes; return $this->_classes;
} }
/** /**
* @param string $class * @param string $class
* @param string $property * @param string $property
* @return mixed * @return mixed
*/ */
public function getProperty(string $class, string $property = ''): mixed public function getProperty(string $class, string $property = ''): mixed
{ {
if (!isset($this->_classes[$class])) { if (!isset($this->_classes[$class])) {
return null; return null;
} }
$properties = $this->_classes[$class]['property']; $properties = $this->_classes[$class]['property'];
if (!empty($property) && isset($properties[$property])) { if (!empty($property) && isset($properties[$property])) {
return $properties[$property]; return $properties[$property];
} }
return $properties; return $properties;
} }
/** /**
* @param string $class * @param string $class
* @param mixed $handler * @param mixed $handler
* @return Loader * @return Loader
*/ */
public function injectProperty(string $class, object $handler): static public function injectProperty(string $class, object $handler): static
{ {
$properties = $this->getProperty($class); $properties = $this->getProperty($class);
if (empty($properties)) { if (empty($properties)) {
return $this; return $this;
} }
foreach ($properties as $property => $attributes) { foreach ($properties as $property => $attributes) {
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$attribute->execute([$handler, $property]); $attribute->execute([$handler, $property]);
} }
} }
return $this; return $this;
} }
/** /**
* @param string $class * @param string $class
* @param string $method * @param string $method
* @return mixed * @return mixed
*/ */
public function getMethod(string $class, string $method = ''): array public function getMethod(string $class, string $method = ''): array
{ {
if (!isset($this->_classes[$class])) { if (!isset($this->_classes[$class])) {
return []; return [];
} }
$properties = $this->_classes[$class]['methods']; $properties = $this->_classes[$class]['methods'];
if (!empty($method) && isset($properties[$method])) { if (!empty($method) && isset($properties[$method])) {
return $properties[$method]; return $properties[$method];
} }
return $properties; return $properties;
} }
/** /**
* @param string $class * @param string $class
* @return array * @return array
*/ */
public function getTarget(string $class): array public function getTarget(string $class): array
{ {
return $this->_classes[$class] ?? []; return $this->_classes[$class] ?? [];
} }
/** /**
* @param DirectoryIterator $paths * @param DirectoryIterator $paths
* @param $namespace * @param $namespace
* @throws Exception * @throws Exception
*/ */
public function _scanDir(DirectoryIterator $paths, $namespace) public function _scanDir(DirectoryIterator $paths, $namespace)
{ {
foreach ($paths as $path) { foreach ($paths as $path) {
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) { if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
continue; continue;
} }
if ($path->isDir()) { if ($path->isDir()) {
$iterator = new DirectoryIterator($path->getRealPath()); $iterator = new DirectoryIterator($path->getRealPath());
$this->_scanDir($iterator, $namespace); $this->_scanDir($iterator, $namespace);
} else { } else {
$this->readFile($path, $namespace); $this->readFile($path, $namespace);
} }
} }
} }
/** /**
* @param DirectoryIterator $path * @param DirectoryIterator $path
* @param $namespace * @param $namespace
* @throws Exception * @throws Exception
*/ */
private function readFile(DirectoryIterator $path, $namespace) private function readFile(DirectoryIterator $path, $namespace)
{ {
try { try {
if ($path->getExtension() !== 'php') { if ($path->getExtension() !== 'php') {
return; return;
} }
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
if (empty($replace) || !$replace->getAttributes(Target::class)) { if (empty($replace) || !$replace->getAttributes(Target::class)) {
return; return;
} }
$this->appendFileToDirectory($path->getRealPath(), $replace->getName()); $this->appendFileToDirectory($path->getRealPath(), $replace->getName());
$_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []]; $_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []];
foreach ($replace->getAttributes() as $attribute) { foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) { if ($attribute->getName() == Attribute::class) {
continue; continue;
} }
$_array['target'][] = $attribute->newInstance(); $_array['target'][] = $attribute->newInstance();
} }
$methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC); $methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) { foreach ($methods as $method) {
$_method = []; $_method = [];
foreach ($method->getAttributes() as $attribute) { foreach ($method->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$_method[] = $attribute->newInstance(); $_method[] = $attribute->newInstance();
} }
$_array['methods'][$method->getName()] = $_method; $_array['methods'][$method->getName()] = $_method;
} }
$methods = $replace->getProperties(); $methods = $replace->getProperties();
foreach ($methods as $method) { foreach ($methods as $method) {
$_property = []; $_property = [];
if ($method->isStatic()) continue; if ($method->isStatic()) continue;
foreach ($method->getAttributes() as $attribute) { foreach ($method->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$_property[] = $attribute->newInstance(); $_property[] = $attribute->newInstance();
} }
$_array['property'][$method->getName()] = $_property; $_array['property'][$method->getName()] = $_property;
} }
$this->_fileMap[$replace->getFileName()] = $replace->getName(); $this->_fileMap[$replace->getFileName()] = $replace->getName();
$this->_classes[$replace->getName()] = $_array; $this->_classes[$replace->getName()] = $_array;
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
$this->addError($throwable, 'throwable'); $this->addError($throwable, 'throwable');
} }
} }
/** /**
* @param string $path * @param string $path
* @throws Exception * @param string|null $outPath
*/ * @throws Exception
public function loadByDirectory(string $path, ?string $outPath = null) */
{ public function loadByDirectory(string $path, ?string $outPath = null)
try { {
$this->each($path, $outPath); try {
} catch (Throwable $exception) { $this->each($path, $outPath);
$this->addError($exception, 'throwable'); } catch (Throwable $exception) {
} $this->addError($exception, 'throwable');
} }
}
/** /**
* @param DirectoryIterator $path * @param DirectoryIterator $path
* @param string $namespace * @param string $namespace
* @return string * @return string
*/ */
private function explodeFileName(DirectoryIterator $path, string $namespace): string private function explodeFileName(DirectoryIterator $path, string $namespace): string
{ {
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath()); $replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
$replace = str_replace('.php', '', $replace); $replace = str_replace('.php', '', $replace);
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace); $replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
$explode = explode('\\', $replace); $explode = explode('\\', $replace);
array_shift($explode); array_shift($explode);
return $namespace . '\\' . implode('\\', $explode); return $namespace . '\\' . implode('\\', $explode);
} }
/** /**
* @param string $filePath * @param string $filePath
* @param string $className * @param string $className
*/ */
public function appendFileToDirectory(string $filePath, string $className) public function appendFileToDirectory(string $filePath, string $className)
{ {
$directory = $this->splitDirectory($filePath); $filePath = str_replace(APP_PATH, '', $filePath);
array_pop($directory);
$tree = null; $directory = $this->splitDirectory($filePath);
foreach ($directory as $value) { array_pop($directory);
$tree = $this->getTree($tree, $value);
} $tree = null;
if ($tree instanceof FileTree) { foreach ($directory as $value) {
$tree->addFile($className); $tree = $this->getTree($tree, $value);
} }
} if ($tree instanceof FileTree) {
$tree->addFile($className);
}
}
/** /**
* @param string $filePath * @param string $filePath
* @return $this * @param string|null $output
*/ * @return $this
private function each(string $filePath, ?string $output): static */
{ private function each(string $filePath, ?string $output): static
$tree = null; {
$directory = $this->splitDirectory($filePath); $tree = null;
$filePath = str_replace(APP_PATH, '', $filePath);
if (!empty($output)) { $directory = $this->splitDirectory($filePath);
$output = DIRECTORY_SEPARATOR . trim($output, '/');
}
$out_path = ''; if (!empty($output)) {
foreach ($directory as $key => $value) { $output = DIRECTORY_SEPARATOR . trim($output, '/');
$out_path .= DIRECTORY_SEPARATOR . $value; }
if ($out_path === $output) { $output = str_replace(APP_PATH, '', $output);
break;
} $out_path = '';
$tree = $this->getTree($tree, $value); foreach ($directory as $key => $value) {
} $out_path .= DIRECTORY_SEPARATOR . $value;
if ($tree instanceof FileTree) { if ($out_path === $output) {
$this->eachNode($tree->getChildes()); break;
$this->execute($tree->getFiles()); }
} $tree = $this->getTree($tree, $value);
return $this; }
} if ($tree instanceof FileTree) {
$this->eachNode($tree->getChildes());
$this->execute($tree->getFiles());
}
return $this;
}
/** /**
* @param string $filePath * @param string $filePath
* @return false|string[] * @return false|string[]
*/ */
private function splitDirectory(string $filePath) private function splitDirectory(string $filePath): array|bool
{ {
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath);
return array_filter($DIRECTORY, function ($value) { return array_filter($DIRECTORY, function ($value) {
return !empty($value); return !empty($value);
}); });
} }
/** /**
* @param $tree * @param $tree
* @param $value * @param $value
* @return FileTree * @return FileTree
*/ */
private function getTree($tree, $value): FileTree private function getTree($tree, $value): FileTree
{ {
if ($tree === null) { if ($tree === null) {
$tree = $this->files->getChild($value); $tree = $this->files->getChild($value);
} else { } else {
$tree = $tree->getChild($value); $tree = $tree->getChild($value);
} }
return $tree; return $tree;
} }
/** /**
* @param FileTree[] $nodes * @param FileTree[] $nodes
*/ */
private function eachNode(array $nodes) private function eachNode(array $nodes)
{ {
foreach ($nodes as $node) { foreach ($nodes as $node) {
$childes = $node->getChildes(); $childes = $node->getChildes();
if (!empty($childes)) { if (!empty($childes)) {
$this->eachNode($childes); $this->eachNode($childes);
} }
$this->execute($node->getFiles()); $this->execute($node->getFiles());
} }
} }
/** /**
* @param string $filename * @param string $filename
* @return mixed * @return mixed
*/ */
public function getClassByFilepath(string $filename): mixed public function getClassByFilepath(string $filename): mixed
{ {
if (!isset($this->_fileMap[$filename])) { if (!isset($this->_fileMap[$filename])) {
return null; return null;
} }
return $this->_classes[$this->_fileMap[$filename]]; return $this->_classes[$this->_fileMap[$filename]];
} }
/** /**
* @param array $classes * @param array $classes
*/ */
private function execute(array $classes) private function execute(array $classes)
{ {
if (empty($classes)) { if (empty($classes)) {
return; return;
} }
foreach ($classes as $className) { foreach ($classes as $className) {
$annotations = $this->_classes[$className] ?? null; $annotations = $this->_classes[$className] ?? null;
if ($annotations === null) { if ($annotations === null) {
continue; continue;
} }
foreach ($annotations['target'] ?? [] as $value) { foreach ($annotations['target'] ?? [] as $value) {
$value->execute([$annotations['handler']]); $value->execute([$annotations['handler']]);
} }
foreach ($annotations['methods'] as $name => $attribute) { foreach ($annotations['methods'] as $name => $attribute) {
foreach ($attribute as $value) { foreach ($attribute as $value) {
if (!($value instanceof \Annotation\Attribute)) { if (!($value instanceof \Annotation\Attribute)) {
continue; continue;
} }
$value->execute([$annotations['handler'], $name]); $value->execute([$annotations['handler'], $name]);
} }
} }
} }
} }
} }
+4 -3
View File
@@ -19,9 +19,10 @@ class Runtime extends Command
public string $command = 'runtime:builder'; public string $command = 'runtime:builder';
/** /**
* @param Input $dtl * @param Input $dtl
*/ * @throws \Exception
*/
public function onHandler(Input $dtl) public function onHandler(Input $dtl)
{ {
// TODO: Implement onHandler() method. // TODO: Implement onHandler() method.