2021-03-03 18:35:04 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Attribute;
|
|
|
|
|
use DirectoryIterator;
|
2021-03-23 16:14:05 +08:00
|
|
|
use Exception;
|
2021-03-03 18:35:04 +08:00
|
|
|
use ReflectionMethod;
|
|
|
|
|
use Snowflake\Abstracts\BaseObject;
|
2021-03-03 19:09:37 +08:00
|
|
|
use Snowflake\Snowflake;
|
2021-03-03 18:35:04 +08:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Loader
|
|
|
|
|
* @package Annotation
|
|
|
|
|
*/
|
|
|
|
|
class Loader extends BaseObject
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-04-09 09:51:17 +08:00
|
|
|
private array $_classes = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $_fileMap = [];
|
|
|
|
|
|
|
|
|
|
|
2021-04-19 11:50:35 +08:00
|
|
|
private array $_directory = [];
|
|
|
|
|
|
|
|
|
|
|
2021-04-09 09:51:17 +08:00
|
|
|
private FileTree $files;
|
|
|
|
|
|
|
|
|
|
|
2021-04-19 12:36:04 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getDirectory(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_directory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-09 09:51:17 +08:00
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->files = new FileTree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 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 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());
|
2021-04-19 12:37:56 +08:00
|
|
|
$directory = rtrim($path->getRealPath(), '/');
|
|
|
|
|
if (!isset($this->_directory[$directory])) {
|
|
|
|
|
$this->_directory[$directory] = [];
|
|
|
|
|
}
|
|
|
|
|
$this->_scanDir($iterator, $namespace);
|
2021-04-09 09:51:17 +08:00
|
|
|
} else {
|
2021-04-19 12:33:07 +08:00
|
|
|
$this->readFile($path, $namespace);
|
2021-04-09 09:51:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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());
|
|
|
|
|
|
|
|
|
|
$_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []];
|
|
|
|
|
foreach ($replace->getAttributes() as $attribute) {
|
|
|
|
|
if ($attribute->getName() == Attribute::class) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-16 15:18:38 +08:00
|
|
|
if ($attribute->getName() == Target::class) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-09 09:51:17 +08:00
|
|
|
$_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->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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
2021-04-19 14:25:58 +08:00
|
|
|
* @param string|array $outPath
|
2021-04-09 09:51:17 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-04-19 15:33:44 +08:00
|
|
|
public function loadByDirectory(string $path, string|array $outPath = '')
|
2021-04-09 09:51:17 +08:00
|
|
|
{
|
|
|
|
|
try {
|
2021-04-20 10:36:27 +08:00
|
|
|
$path = '/' . trim($path, '/');
|
|
|
|
|
foreach ($this->_directory as $key => $_path) {
|
|
|
|
|
$key = '/' . trim($key, '/');
|
|
|
|
|
if (!str_starts_with($key, $path)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (in_array($key, $outPath)) continue;
|
|
|
|
|
$this->execute($_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// $this->each($path, $outPath);
|
2021-04-09 09:51:17 +08:00
|
|
|
} 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());
|
|
|
|
|
|
|
|
|
|
$replace = str_replace('.php', '', $replace);
|
|
|
|
|
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
|
|
|
|
|
$explode = explode('\\', $replace);
|
|
|
|
|
array_shift($explode);
|
|
|
|
|
|
|
|
|
|
return $namespace . '\\' . implode('\\', $explode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $filePath
|
|
|
|
|
* @param string $className
|
|
|
|
|
*/
|
|
|
|
|
public function appendFileToDirectory(string $filePath, string $className)
|
|
|
|
|
{
|
2021-04-19 15:36:15 +08:00
|
|
|
$array = explode('/', $filePath);
|
2021-04-20 10:35:57 +08:00
|
|
|
unset($array[count($array) - 1]);
|
2021-04-19 12:43:07 +08:00
|
|
|
|
2021-04-19 16:10:57 +08:00
|
|
|
$array = '/' . trim(implode('/', $array), '/');
|
|
|
|
|
|
|
|
|
|
$this->_directory[$array][] = $className;
|
|
|
|
|
|
2021-04-20 10:35:57 +08:00
|
|
|
// $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, $filePath);
|
|
|
|
|
// }
|
2021-04-09 09:51:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $filePath
|
2021-04-14 18:13:17 +08:00
|
|
|
* @param string|null $outPath
|
2021-04-09 09:51:17 +08:00
|
|
|
* @return $this
|
2021-04-14 18:14:27 +08:00
|
|
|
* @throws Exception
|
2021-04-09 09:51:17 +08:00
|
|
|
*/
|
2021-04-14 18:13:17 +08:00
|
|
|
private function each(string $filePath, ?string $outPath): static
|
2021-04-09 09:51:17 +08:00
|
|
|
{
|
|
|
|
|
$tree = null;
|
|
|
|
|
$directory = $this->splitDirectory($filePath);
|
|
|
|
|
|
2021-04-14 18:16:36 +08:00
|
|
|
$_tmp = '';
|
2021-04-14 18:13:17 +08:00
|
|
|
if (!empty($outPath)) {
|
|
|
|
|
$outPath = rtrim($outPath, '/');
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 09:51:17 +08:00
|
|
|
foreach ($directory as $key => $value) {
|
2021-04-14 18:13:17 +08:00
|
|
|
$_tmp .= DIRECTORY_SEPARATOR . $value;
|
2021-04-14 18:15:29 +08:00
|
|
|
if (!empty($outPath) && str_contains($_tmp, $outPath)) {
|
2021-04-14 18:13:17 +08:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-14 17:32:17 +08:00
|
|
|
$tree = $this->getTree($tree, $value);
|
2021-04-09 09:51:17 +08:00
|
|
|
}
|
|
|
|
|
if ($tree instanceof FileTree) {
|
2021-04-14 18:22:55 +08:00
|
|
|
$this->eachNode($tree->getChildes(), $outPath);
|
2021-04-09 09:51:17 +08:00
|
|
|
$this->execute($tree->getFiles());
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 FileTree[] $nodes
|
2021-04-14 18:22:55 +08:00
|
|
|
* @param string|null $outPath
|
2021-04-14 18:26:50 +08:00
|
|
|
* @throws Exception
|
2021-04-09 09:51:17 +08:00
|
|
|
*/
|
2021-04-14 18:22:55 +08:00
|
|
|
private function eachNode(array $nodes, ?string $outPath = '')
|
2021-04-09 09:51:17 +08:00
|
|
|
{
|
|
|
|
|
foreach ($nodes as $node) {
|
2021-04-14 18:22:55 +08:00
|
|
|
$this->execute($node->getFiles());
|
2021-04-14 18:29:35 +08:00
|
|
|
if (!empty($outPath) && str_contains($node->getDirPath(), $outPath)) {
|
2021-04-14 18:22:55 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-09 09:51:17 +08:00
|
|
|
$childes = $node->getChildes();
|
|
|
|
|
if (!empty($childes)) {
|
2021-04-14 18:22:55 +08:00
|
|
|
$this->eachNode($childes, $outPath);
|
2021-04-09 09:51:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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) {
|
|
|
|
|
$value->execute([$annotations['handler'], $name]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-08 00:38:29 +08:00
|
|
|
|
2021-03-03 18:35:04 +08:00
|
|
|
}
|