This commit is contained in:
as2252258@163.com
2021-04-24 16:29:55 +08:00
parent ba8cd63b5c
commit eca0de70ed
+322 -320
View File
@@ -21,262 +21,265 @@ class Loader extends BaseObject
{ {
private array $_classes = []; private array $_classes = [];
private array $_fileMap = []; private array $_fileMap = [];
private array $_directory = []; private array $_directory = [];
private FileTree $files; private FileTree $files;
/** /**
* @return array * @return array
*/ */
public function getDirectory(): array public function getDirectory(): array
{ {
return $this->_directory; return $this->_directory;
} }
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());
$directory = rtrim($path->getRealPath(), '/'); $directory = rtrim($path->getRealPath(), '/');
if (!isset($this->_directory[$directory])) { if (!isset($this->_directory[$directory])) {
$this->_directory[$directory] = []; $this->_directory[$directory] = [];
} }
$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)) {
return;
}
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
$_array = ['handler' => $replace, 'target' => [], 'methods' => [], 'property' => []]; var_dump($replace->getName() . '::' . $replace->getAttributes(Target::class));
foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) {
continue;
}
if ($attribute->getName() == Target::class) {
continue;
}
$_array['target'][] = $attribute->newInstance();
}
$methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC); if (empty($replace) || !$replace->getAttributes(Target::class)) {
foreach ($methods as $method) { return;
$_method = []; }
foreach ($method->getAttributes() as $attribute) { $this->appendFileToDirectory($path->getRealPath(), $replace->getName());
if (!class_exists($attribute->getName())) {
continue;
}
$_method[] = $attribute->newInstance();
}
$_array['methods'][$method->getName()] = $_method;
}
$methods = $replace->getProperties(); $_array = ['handler' => $replace, 'target' => [], 'methods' => [], 'property' => []];
foreach ($methods as $method) { foreach ($replace->getAttributes() as $attribute) {
$_property = []; if ($attribute->getName() == Attribute::class) {
if ($method->isStatic()) continue; continue;
foreach ($method->getAttributes() as $attribute) { }
if (!class_exists($attribute->getName())) { if ($attribute->getName() == Target::class) {
continue; continue;
} }
$_property[] = $attribute->newInstance(); $_array['target'][] = $attribute->newInstance();
} }
$_array['property'][$method->getName()] = $_property;
}
$this->_fileMap[$replace->getFileName()] = $replace->getName(); $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;
}
$this->_classes[$replace->getName()] = $_array; $methods = $replace->getProperties();
} catch (Throwable $throwable) { foreach ($methods as $method) {
$this->addError($throwable, 'throwable'); $_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');
}
}
/** /**
* @param string $path * @param string $path
* @param string|array $outPath * @param string|array $outPath
* @throws Exception * @throws Exception
*/ */
public function loadByDirectory(string $path, string|array $outPath = '') public function loadByDirectory(string $path, string|array $outPath = '')
{ {
try { try {
$path = '/' . trim($path, '/'); $path = '/' . trim($path, '/');
foreach ($this->_directory as $key => $_path) { foreach ($this->_directory as $key => $_path) {
$key = '/' . trim($key, '/'); $key = '/' . trim($key, '/');
if (!str_starts_with($key, $path)) { if (!str_starts_with($key, $path)) {
continue; continue;
} }
if (in_array($key, $outPath)) { if (in_array($key, $outPath)) {
continue; continue;
} }
$this->execute($_path); $this->execute($_path);
} }
} catch (Throwable $exception) { } catch (Throwable $exception) {
$this->addError($exception, 'throwable'); $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)
{ {
$array = explode('/', $filePath); $array = explode('/', $filePath);
unset($array[count($array) - 1]); unset($array[count($array) - 1]);
$array = '/' . trim(implode('/', $array), '/'); $array = '/' . trim(implode('/', $array), '/');
$this->_directory[$array][] = $className; $this->_directory[$array][] = $className;
// $directory = $this->splitDirectory($filePath); // $directory = $this->splitDirectory($filePath);
// array_pop($directory); // array_pop($directory);
@@ -289,125 +292,124 @@ class Loader extends BaseObject
// if ($tree instanceof FileTree) { // if ($tree instanceof FileTree) {
// $tree->addFile($className, $filePath); // $tree->addFile($className, $filePath);
// } // }
} }
/** /**
* @param string $filePath * @param string $filePath
* @param string|null $outPath * @param string|null $outPath
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
private function each(string $filePath, ?string $outPath): static private function each(string $filePath, ?string $outPath): static
{ {
$tree = null; $tree = null;
$directory = $this->splitDirectory($filePath); $directory = $this->splitDirectory($filePath);
$_tmp = ''; $_tmp = '';
if (!empty($outPath)) { if (!empty($outPath)) {
$outPath = rtrim($outPath, '/'); $outPath = rtrim($outPath, '/');
} }
foreach ($directory as $key => $value) { foreach ($directory as $key => $value) {
$_tmp .= DIRECTORY_SEPARATOR . $value; $_tmp .= DIRECTORY_SEPARATOR . $value;
if (!empty($outPath) && str_contains($_tmp, $outPath)) { if (!empty($outPath) && str_contains($_tmp, $outPath)) {
break; break;
} }
$tree = $this->getTree($tree, $value); $tree = $this->getTree($tree, $value);
} }
if ($tree instanceof FileTree) { if ($tree instanceof FileTree) {
$this->eachNode($tree->getChildes(), $outPath); $this->eachNode($tree->getChildes(), $outPath);
$this->execute($tree->getFiles()); $this->execute($tree->getFiles());
} }
return $this; return $this;
} }
/** /**
* @param string $filePath * @param string $filePath
* @return false|string[] * @return false|string[]
*/ */
private function splitDirectory(string $filePath): array|bool 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
* @param string|null $outPath * @param string|null $outPath
* @throws Exception * @throws Exception
*/ */
private function eachNode(array $nodes, ?string $outPath = '') private function eachNode(array $nodes, ?string $outPath = '')
{ {
foreach ($nodes as $node) { foreach ($nodes as $node) {
$this->execute($node->getFiles()); $this->execute($node->getFiles());
if (!empty($outPath) && str_contains($node->getDirPath(), $outPath)) { if (!empty($outPath) && str_contains($node->getDirPath(), $outPath)) {
continue; continue;
} }
$childes = $node->getChildes(); $childes = $node->getChildes();
if (!empty($childes)) { if (!empty($childes)) {
$this->eachNode($childes, $outPath); $this->eachNode($childes, $outPath);
} }
} }
} }
/** /**
* @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;
} }
$handler = $annotations['handler']->newInstance(); foreach ($annotations['target'] ?? [] as $value) {
foreach ($annotations['target'] ?? [] as $value) { $value->execute([$annotations['handler']]);
$value->execute([$handler]); }
} foreach ($annotations['methods'] as $name => $attribute) {
foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) {
foreach ($attribute as $value) { $value->execute([$annotations['handler'], $name]);
$value->execute([$handler, $name]); }
} }
} }
} }
}
} }