This commit is contained in:
2021-03-29 16:21:01 +08:00
parent ce0ba186a7
commit c84e19c8e5
+229 -235
View File
@@ -24,280 +24,274 @@ class Loader extends BaseObject
{ {
private array $_classes = []; private array $_classes = [];
private array $_fileMap = []; private array $_fileMap = [];
private array $_directoryMap = []; private array $_directoryMap = [];
/** /**
* @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)
{ {
/** @var DirectoryIterator $path */ /** @var DirectoryIterator $path */
$DIRECTORY = $this->createDirectoryMap($paths); $DIRECTORY = $this->createDirectoryMap($paths);
foreach ($paths as $path) { foreach ($paths as $path) {
if ($path->isDot()) continue; if ($path->isDot()) continue;
if (str_starts_with($path->getFilename(), '.')) { if (str_starts_with($path->getFilename(), '.')) {
continue; continue;
} }
if ($path->isDir()) { if ($path->isDir()) {
$this->_scanDir(new DirectoryIterator($path->getRealPath()), $namespace); $this->_scanDir(new DirectoryIterator($path->getRealPath()), $namespace);
continue; continue;
} }
if ($path->getExtension() !== 'php') { if ($path->getExtension() !== 'php') {
continue; continue;
} }
if (!in_array($path->getRealPath(), $this->_directoryMap[$DIRECTORY])) { if (!in_array($path->getRealPath(), $this->_directoryMap[$DIRECTORY])) {
$this->_directoryMap[$DIRECTORY][] = $path->getRealPath(); $this->_directoryMap[$DIRECTORY][] = $path->getRealPath();
} }
try { try {
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
if (empty($replace) || !$replace->isInstantiable()) { if (empty($replace) || !$replace->isInstantiable()) {
continue; continue;
} }
if (!$replace->getAttributes(Target::class)) { if (!$replace->getAttributes(Target::class)) {
continue; continue;
} }
$_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();
if ($property instanceof Inject) { }
$property->execute([$_array['handler'], $method]); $_array['property'][$method->getName()] = $_property;
} else { }
$_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->error($throwable->getMessage());
$this->error($throwable->getFile());
$this->error($throwable->getLine());
}
}
}
$this->_classes[$replace->getName()] = $_array; /**
} catch (Throwable $throwable) { * @param string $path
$this->error($throwable->getMessage()); */
$this->error($throwable->getFile()); public function loadByDirectory(string $path)
$this->error($throwable->getLine()); {
} 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]);
}
}
}
}
/** /**
* @param string $path * @param DirectoryIterator $path
*/ * @param string $namespace
public function loadByDirectory(string $path) * @return string
{ */
foreach ($this->_fileMap as $fileName => $className) { private function explodeFileName(DirectoryIterator $path, string $namespace): string
if (!str_starts_with($fileName, $path)) { {
continue; $replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
}
if (!isset($this->_classes[$className])) {
continue;
}
$annotations = $this->_classes[$className]; $replace = str_replace('.php', '', $replace);
if (isset($annotations['target']) && !empty($annotations['target'])) { $replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
foreach ($annotations['target'] as $value) { $explode = explode('\\', $replace);
$value->execute([$annotations['handler']]); array_shift($explode);
}
}
foreach ($annotations['methods'] as $name => $attribute) { return $namespace . '\\' . implode('\\', $explode);
foreach ($attribute as $value) { }
if (!($value instanceof \Annotation\Attribute)) {
continue;
}
$value->execute([$annotations['handler'], $name]);
}
}
}
}
/** /**
* @param DirectoryIterator $path * @param DirectoryIterator $directoryIterator
* @param string $namespace * @return string
* @return string */
*/ public function createDirectoryMap(DirectoryIterator $directoryIterator): string
private function explodeFileName(DirectoryIterator $path, string $namespace): string {
{ $DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath());
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath()); array_pop($DIRECTORY);
$replace = str_replace('.php', '', $replace); $DIRECTORY = implode(DIRECTORY_SEPARATOR, $DIRECTORY);
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
$explode = explode('\\', $replace);
array_shift($explode);
return $namespace . '\\' . implode('\\', $explode); if (!isset($this->_directoryMap[$DIRECTORY])) {
} $this->_directoryMap[$DIRECTORY] = [];
}
return $DIRECTORY;
}
/** /**
* @param DirectoryIterator $directoryIterator * @param string $Directory
* @return string * @return array
*/ */
public function createDirectoryMap(DirectoryIterator $directoryIterator): string public function getDirectoryFiles(string $Directory): array
{ {
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath()); if (!isset($this->_directoryMap[$Directory])) {
array_pop($DIRECTORY); return [];
}
$DIRECTORY = implode(DIRECTORY_SEPARATOR, $DIRECTORY); return $this->_directoryMap[$Directory];
}
if (!isset($this->_directoryMap[$DIRECTORY])) {
$this->_directoryMap[$DIRECTORY] = [];
}
return $DIRECTORY;
}
/** /**
* @param string $Directory * @param string $filename
* @return array * @return mixed
*/ */
public function getDirectoryFiles(string $Directory): array public function getClassByFilepath(string $filename): mixed
{ {
if (!isset($this->_directoryMap[$Directory])) { if (!isset($this->_fileMap[$filename])) {
return []; return null;
} }
return $this->_directoryMap[$Directory]; 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]];
}
} }