This commit is contained in:
as2252258@163.com
2021-04-08 01:07:48 +08:00
parent 4d0fa183ba
commit 3d77ed9d10
3 changed files with 136 additions and 81 deletions
+4 -4
View File
@@ -95,10 +95,10 @@ class Annotation extends Component
* @param string $dir * @param string $dir
* @throws Exception * @throws Exception
*/ */
public function runtime(string $dir, ?string $outPath = null) // public function runtime(string $dir, ?string $outPath = null)
{ // {
$this->_loader->directoryRuntime($dir, $outPath); // $this->_loader->directoryRuntime($dir, $outPath);
} // }
/** /**
+66
View File
@@ -0,0 +1,66 @@
<?php
namespace Annotation;
class FileTree
{
private array $files = [];
private array $childes = [];
/**
* @param $path
* @return $this|null
*/
public function getChild($path): ?static
{
if (!isset($this->childes[$path])) {
$this->addChild($path, new FileTree());
}
return $this->childes[$path];
}
/**
* @param string $path
* @param FileTree $fileTree
*/
public function addChild(string $path, FileTree $fileTree)
{
$this->childes[$path] = $fileTree;
}
/**
* @param string $className
*/
public function addFile(string $className)
{
$this->files[] = $className;
}
/**
* @return array
*/
public function getFiles(): array
{
return $this->files;
}
/**
* @return array
*/
public function getChildes(): array
{
return $this->childes;
}
}
+66 -77
View File
@@ -32,6 +32,14 @@ class Loader extends BaseObject
private array $_directoryMap = []; private array $_directoryMap = [];
private FileTree $files;
public function init()
{
$this->files = new FileTree();
}
/** /**
* @param $path * @param $path
@@ -126,8 +134,6 @@ class Loader extends BaseObject
*/ */
public function _scanDir(DirectoryIterator $paths, $namespace) public function _scanDir(DirectoryIterator $paths, $namespace)
{ {
/** @var DirectoryIterator $path */
$DIRECTORY = $this->createDirectoryMap($paths);
foreach ($paths as $path) { foreach ($paths as $path) {
if ($path->isDot()) continue; if ($path->isDot()) continue;
@@ -208,30 +214,33 @@ class Loader extends BaseObject
public function loadByDirectory(string $path, ?string $outPath = null) public function loadByDirectory(string $path, ?string $outPath = null)
{ {
try { try {
foreach ($this->_fileMap as $fileName => $className) {
if (!str_starts_with($fileName, $path)) {
continue;
}
if (!isset($this->_classes[$className])) {
continue;
}
$annotations = $this->_classes[$className]; return $this->each($path);
if (isset($annotations['target']) && !empty($annotations['target'])) {
foreach ($annotations['target'] as $value) {
$value->execute([$annotations['handler']]);
}
}
foreach ($annotations['methods'] as $name => $attribute) { // foreach ($this->_fileMap as $fileName => $className) {
foreach ($attribute as $value) { // if (!str_starts_with($fileName, $path)) {
if (!($value instanceof \Annotation\Attribute)) { // continue;
continue; // }
} // if (!isset($this->_classes[$className])) {
$value->execute([$annotations['handler'], $name]); // 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]);
// }
// }
// }
} catch (Throwable $exception) { } catch (Throwable $exception) {
$this->addError($exception, 'throwable'); $this->addError($exception, 'throwable');
} }
@@ -256,38 +265,6 @@ class Loader extends BaseObject
} }
/**
* @param DirectoryIterator $directoryIterator
* @return string
*/
public function createDirectoryMap(DirectoryIterator $directoryIterator): string
{
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath());
array_pop($DIRECTORY);
$path = DIRECTORY_SEPARATOR;
foreach ($DIRECTORY as $value) {
$path = $this->makeMoneyDirectoryArray($path, $value);
}
return $DIRECTORY;
}
/**
* @param $path
* @param $value
* @return string
*/
private function makeMoneyDirectoryArray($path, $value)
{
$path .= $value . DIRECTORY_SEPARATOR;
if (!isset($this->_directoryMap[$path])) {
$this->_directoryMap[$path] = [];
}
return $path;
}
/** /**
* @param string $filePath * @param string $filePath
* @param string $className * @param string $className
@@ -297,38 +274,52 @@ class Loader extends BaseObject
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath);
array_pop($DIRECTORY); array_pop($DIRECTORY);
$path = DIRECTORY_SEPARATOR; $tree = $this->files;
foreach ($DIRECTORY as $value) { foreach ($DIRECTORY as $value) {
$path = $this->makeMoneyDirectoryArray($path, $value); $tree = $tree->getChild($value);
$this->_directoryMap[$path][] = $className; $tree->addFile($className);
} }
} }
/** /**
* @param string $Directory * @param string $filePath
* @return array * @return $this
*/ */
public function getDirectoryFiles(string $Directory): array private function each(string $filePath): static
{ {
if (!isset($this->_directoryMap[$Directory])) { $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath);
return [];
$tree = null;
foreach ($DIRECTORY as $value) {
if ($tree === null) {
$tree = $this->files->getChild($value);
} else {
$tree = $tree->getChild($value);
}
} }
return $this->_directoryMap[$Directory]; if ($tree instanceof FileTree) {
$this->eachNode($tree->getChildes());
$this->execute($tree->getFiles());
}
return $this;
} }
/** /**
* @param string $Directory * @param FileTree[] $nodes
* @param string|null $output
*/ */
public function directoryRuntime(string $Directory, ?string $output) private function eachNode(array $nodes)
{ {
if (!empty($output) && isset($this->_directoryMap[$output])) { foreach ($nodes as $node) {
unset($this->_directoryMap[$output]); $childes = $node->getChildes();
if (!empty($childes)) {
$this->eachNode($childes);
}
$this->execute($node->getFiles());
} }
$this->execute($Directory);
} }
@@ -346,16 +337,14 @@ class Loader extends BaseObject
/** /**
* @param $directory * @param array $classes
*/ */
private function execute(string $directory) private function execute(array $classes)
{ {
if (!isset($this->_directoryMap[$directory])) { if (empty($classes)) {
return; return;
} }
foreach ($classes as $className) {
$directories = $this->_directoryMap[$directory];
foreach ($directories as $className) {
if (!isset($this->_classes[$className])) { if (!isset($this->_classes[$className])) {
continue; continue;
} }