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
+44 -31
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,28 +133,34 @@ 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);
}
}
} }
if ($path->getExtension() !== 'php') {
continue; /**
} * @param DirectoryIterator $path
* @param $namespace
* @throws Exception
*/
private function readFile(DirectoryIterator $path, $namespace)
{
try { try {
if ($path->getExtension() !== 'php') {
return;
}
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
if (empty($replace) || !$replace->isInstantiable()) {
continue;
}
if (!$replace->getAttributes(Target::class)) { if (!$replace->getAttributes(Target::class)) {
continue; return;
} }
$this->appendFileToDirectory($path->getRealPath(), $replace->getName()); $this->appendFileToDirectory($path->getRealPath(), $replace->getName());
@@ -188,10 +192,6 @@ class Loader extends BaseObject
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
// $property = $attribute->newInstance();
// if ($property instanceof Inject) {
// $property->execute([$_array['handler'], $method]);
// }
$_property[] = $attribute->newInstance(); $_property[] = $attribute->newInstance();
} }
$_array['property'][$method->getName()] = $_property; $_array['property'][$method->getName()] = $_property;
@@ -204,7 +204,6 @@ class Loader extends BaseObject
$this->addError($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);
} }