This commit is contained in:
as2252258@163.com
2021-04-08 01:37:57 +08:00
parent 8923c6efe3
commit 0b5404055d
3 changed files with 83 additions and 71 deletions
-2
View File
@@ -4,11 +4,9 @@
namespace Annotation; namespace Annotation;
use Database\InjectProperty;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Exception\ComponentException;
/** /**
* Class Annotation * Class Annotation
+79 -66
View File
@@ -30,8 +30,6 @@ class Loader extends BaseObject
private array $_fileMap = []; private array $_fileMap = [];
private array $_directoryMap = [];
private FileTree $files; private FileTree $files;
@@ -135,74 +133,75 @@ class Loader extends BaseObject
public function _scanDir(DirectoryIterator $paths, $namespace) public function _scanDir(DirectoryIterator $paths, $namespace)
{ {
foreach ($paths as $path) { foreach ($paths as $path) {
if ($path->isDot()) continue; if ($path->isDot() || 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); $iterator = new DirectoryIterator($path->getRealPath());
continue; $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') { if ($path->getExtension() !== 'php') {
continue; return;
} }
try {
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
if (empty($replace) || !$replace->isInstantiable()) { if (!$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; continue;
} }
$_array['target'][] = $attribute->newInstance();
}
if (!$replace->getAttributes(Target::class)) { $methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
continue; foreach ($methods as $method) {
} $_method = [];
$this->appendFileToDirectory($path->getRealPath(), $replace->getName()); foreach ($method->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
$_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []];
foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) {
continue; continue;
} }
$_array['target'][] = $attribute->newInstance(); $_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();
// if ($property instanceof Inject) {
// $property->execute([$_array['handler'], $method]);
// }
$_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');
} }
$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');
} }
} }
@@ -214,7 +213,7 @@ class Loader extends BaseObject
public function loadByDirectory(string $path, ?string $outPath = null) public function loadByDirectory(string $path, ?string $outPath = null)
{ {
try { try {
$this->each($path); $this->each($path, $outPath);
} catch (Throwable $exception) { } catch (Throwable $exception) {
$this->addError($exception, 'throwable'); $this->addError($exception, 'throwable');
} }
@@ -245,14 +244,11 @@ class Loader extends BaseObject
*/ */
public function appendFileToDirectory(string $filePath, string $className) public function appendFileToDirectory(string $filePath, string $className)
{ {
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); $directory = $this->splitDirectory($filePath);
array_pop($DIRECTORY); array_pop($DIRECTORY);
$tree = null; $tree = null;
foreach ($DIRECTORY as $value) { foreach ($directory as $value) {
if (empty($value)) {
continue;
}
$tree = $this->getTree($tree, $value); $tree = $this->getTree($tree, $value);
} }
if ($tree instanceof FileTree) { if ($tree instanceof FileTree) {
@@ -265,13 +261,17 @@ class Loader extends BaseObject
* @param string $filePath * @param string $filePath
* @return $this * @return $this
*/ */
private function each(string $filePath): static private function each(string $filePath, string $output): static
{ {
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath);
$tree = null; $tree = null;
foreach ($DIRECTORY as $value) { $directory = $this->splitDirectory($filePath);
if (empty($value)) {
$output = DIRECTORY_SEPARATOR . rtrim($output, '/');
$out_path = '';
foreach ($directory as $key => $value) {
$out_path .= DIRECTORY_SEPARATOR . $value;
if ($out_path === $output) {
continue; continue;
} }
$tree = $this->getTree($tree, $value); $tree = $this->getTree($tree, $value);
@@ -284,6 +284,19 @@ class Loader extends BaseObject
} }
/**
* @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 $tree * @param $tree
* @param $value * @param $value
+4 -3
View File
@@ -50,12 +50,13 @@ class OnWorkerStart extends Callback
$this->onTask($server, $worker_id); $this->onTask($server, $worker_id);
} else { } else {
$start = microtime(true); $start = microtime(true);
$annotation->instanceDirectoryFiles(APP_PATH); $annotation->instanceDirectoryFiles(CONTROLLER_PATH);
$this->error('use time ' . (microtime(true) - $start)); $this->error('use time ' . (microtime(true) - $start));
Coroutine\go(function () use ($annotation) {
$annotation->instanceDirectoryFiles(APP_PATH, CONTROLLER_PATH);
});
$this->onWorker($server, $worker_id); $this->onWorker($server, $worker_id);
} }