modify
This commit is contained in:
@@ -85,9 +85,19 @@ class Annotation extends Component
|
|||||||
* @param string $dir
|
* @param string $dir
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function instanceDirectoryFiles(string $dir)
|
public function instanceDirectoryFiles(string $dir, ?string $outPath = null)
|
||||||
{
|
{
|
||||||
$this->_loader->loadByDirectory($dir);
|
$this->_loader->loadByDirectory($dir, $outPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $dir
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function runtime(string $dir, ?string $outPath = null)
|
||||||
|
{
|
||||||
|
$this->_loader->directoryRuntime($dir, $outPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+310
-231
@@ -24,281 +24,360 @@ 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;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
$this->appendFileToDirectory($path->getRealPath());
|
||||||
|
|
||||||
if (!in_array($path->getRealPath(), $this->_directoryMap[$DIRECTORY])) {
|
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
|
||||||
$this->_directoryMap[$DIRECTORY][] = $path->getRealPath();
|
if (empty($replace) || !$replace->isInstantiable()) {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
if (!$replace->getAttributes(Target::class)) {
|
||||||
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
|
continue;
|
||||||
if (empty($replace) || !$replace->isInstantiable()) {
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$replace->getAttributes(Target::class)) {
|
$_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []];
|
||||||
continue;
|
foreach ($replace->getAttributes() as $attribute) {
|
||||||
}
|
if ($attribute->getName() == Attribute::class) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$_array['target'][] = $attribute->newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
$_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []];
|
$methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||||
foreach ($replace->getAttributes() as $attribute) {
|
foreach ($methods as $method) {
|
||||||
if ($attribute->getName() == Attribute::class) {
|
$_method = [];
|
||||||
continue;
|
foreach ($method->getAttributes() as $attribute) {
|
||||||
}
|
if (!class_exists($attribute->getName())) {
|
||||||
$_array['target'][] = $attribute->newInstance();
|
continue;
|
||||||
}
|
}
|
||||||
|
$_method[] = $attribute->newInstance();
|
||||||
|
}
|
||||||
|
$_array['methods'][$method->getName()] = $_method;
|
||||||
|
}
|
||||||
|
|
||||||
$methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
|
$methods = $replace->getProperties();
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
$_method = [];
|
$_property = [];
|
||||||
foreach ($method->getAttributes() as $attribute) {
|
if ($method->isStatic()) continue;
|
||||||
if (!class_exists($attribute->getName())) {
|
foreach ($method->getAttributes() as $attribute) {
|
||||||
continue;
|
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();
|
// $property = $attribute->newInstance();
|
||||||
// if ($property instanceof Inject) {
|
// if ($property instanceof Inject) {
|
||||||
// $property->execute([$_array['handler'], $method]);
|
// $property->execute([$_array['handler'], $method]);
|
||||||
// }
|
// }
|
||||||
$_property[] = $attribute->newInstance();
|
$_property[] = $attribute->newInstance();
|
||||||
}
|
}
|
||||||
$_array['property'][$method->getName()] = $_property;
|
$_array['property'][$method->getName()] = $_property;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_fileMap[$replace->getFileName()] = $replace->getName();
|
$this->_fileMap[$replace->getFileName()] = $replace->getName();
|
||||||
|
|
||||||
$this->_classes[$replace->getName()] = $_array;
|
$this->_classes[$replace->getName()] = $_array;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$this->addError($throwable, 'throwable');
|
$this->addError($throwable, 'throwable');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function loadByDirectory(string $path)
|
public function loadByDirectory(string $path, ?string $outPath = null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
foreach ($this->_fileMap as $fileName => $className) {
|
foreach ($this->_fileMap as $fileName => $className) {
|
||||||
if (!str_starts_with($fileName, $path)) {
|
if (str_starts_with($fileName, $outPath)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!isset($this->_classes[$className])) {
|
if (!str_starts_with($fileName, $path)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!isset($this->_classes[$className])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$annotations = $this->_classes[$className];
|
$annotations = $this->_classes[$className];
|
||||||
if (isset($annotations['target']) && !empty($annotations['target'])) {
|
if (isset($annotations['target']) && !empty($annotations['target'])) {
|
||||||
foreach ($annotations['target'] as $value) {
|
foreach ($annotations['target'] as $value) {
|
||||||
$value->execute([$annotations['handler']]);
|
$value->execute([$annotations['handler']]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($annotations['methods'] as $name => $attribute) {
|
foreach ($annotations['methods'] as $name => $attribute) {
|
||||||
foreach ($attribute as $value) {
|
foreach ($attribute as $value) {
|
||||||
if (!($value instanceof \Annotation\Attribute)) {
|
if (!($value instanceof \Annotation\Attribute)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$value->execute([$annotations['handler'], $name]);
|
$value->execute([$annotations['handler'], $name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Throwable $exception) {
|
} catch (Throwable $exception) {
|
||||||
$this->addError($exception, 'throwable');
|
$this->addError($exception, 'throwable');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param DirectoryIterator $path
|
* @param DirectoryIterator $path
|
||||||
* @param string $namespace
|
* @param string $namespace
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function explodeFileName(DirectoryIterator $path, string $namespace): string
|
private function explodeFileName(DirectoryIterator $path, string $namespace): string
|
||||||
{
|
{
|
||||||
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
|
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
|
||||||
|
|
||||||
$replace = str_replace('.php', '', $replace);
|
$replace = str_replace('.php', '', $replace);
|
||||||
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
|
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
|
||||||
$explode = explode('\\', $replace);
|
$explode = explode('\\', $replace);
|
||||||
array_shift($explode);
|
array_shift($explode);
|
||||||
|
|
||||||
return $namespace . '\\' . implode('\\', $explode);
|
return $namespace . '\\' . implode('\\', $explode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param DirectoryIterator $directoryIterator
|
* @param DirectoryIterator $directoryIterator
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function createDirectoryMap(DirectoryIterator $directoryIterator): string
|
public function createDirectoryMap(DirectoryIterator $directoryIterator): string
|
||||||
{
|
{
|
||||||
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath());
|
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $directoryIterator->getRealPath());
|
||||||
array_pop($DIRECTORY);
|
array_pop($DIRECTORY);
|
||||||
|
|
||||||
$DIRECTORY = implode(DIRECTORY_SEPARATOR, $DIRECTORY);
|
$path = DIRECTORY_SEPARATOR;
|
||||||
|
foreach ($DIRECTORY as $value) {
|
||||||
if (!isset($this->_directoryMap[$DIRECTORY])) {
|
$path = $this->makeMoneyDirectoryArray($path, $value);
|
||||||
$this->_directoryMap[$DIRECTORY] = [];
|
}
|
||||||
}
|
return $DIRECTORY;
|
||||||
return $DIRECTORY;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $Directory
|
* @param $path
|
||||||
* @return array
|
* @param $value
|
||||||
*/
|
* @return string
|
||||||
public function getDirectoryFiles(string $Directory): array
|
*/
|
||||||
{
|
private function makeMoneyDirectoryArray($path, $value)
|
||||||
if (!isset($this->_directoryMap[$Directory])) {
|
{
|
||||||
return [];
|
$path .= $value . DIRECTORY_SEPARATOR;
|
||||||
}
|
if (!isset($this->_directoryMap[$path])) {
|
||||||
return $this->_directoryMap[$Directory];
|
$this->_directoryMap[$path] = [];
|
||||||
}
|
}
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filename
|
* @param string $filePath
|
||||||
* @return mixed
|
*/
|
||||||
*/
|
public function appendFileToDirectory(string $filePath)
|
||||||
public function getClassByFilepath(string $filename): mixed
|
{
|
||||||
{
|
$DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath);
|
||||||
if (!isset($this->_fileMap[$filename])) {
|
array_pop($DIRECTORY);
|
||||||
return null;
|
|
||||||
}
|
$path = DIRECTORY_SEPARATOR;
|
||||||
return $this->_classes[$this->_fileMap[$filename]];
|
foreach ($DIRECTORY as $value) {
|
||||||
}
|
$path = $this->makeMoneyDirectoryArray($path, $value);
|
||||||
|
|
||||||
|
$this->_directoryMap[$path][] = $filePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $Directory
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getDirectoryFiles(string $Directory): array
|
||||||
|
{
|
||||||
|
if (!isset($this->_directoryMap[$Directory])) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return $this->_directoryMap[$Directory];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $Directory
|
||||||
|
* @param string|null $output
|
||||||
|
*/
|
||||||
|
public function directoryRuntime(string $Directory, ?string $output)
|
||||||
|
{
|
||||||
|
if (!empty($output) && isset($this->_directoryMap[$output])) {
|
||||||
|
unset($this->_directoryMap[$output]);
|
||||||
|
}
|
||||||
|
$this->execute($Directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 $directory
|
||||||
|
*/
|
||||||
|
private function execute(string $directory)
|
||||||
|
{
|
||||||
|
if (!isset($this->_directoryMap[$directory])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$directories = $this->_directoryMap[$directory];
|
||||||
|
foreach ($directories as $className) {
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,11 +46,17 @@ class OnWorkerStart extends Callback
|
|||||||
$annotation->setLoader($runtime);
|
$annotation->setLoader($runtime);
|
||||||
|
|
||||||
if ($worker_id >= $server->setting['worker_num']) {
|
if ($worker_id >= $server->setting['worker_num']) {
|
||||||
$annotation->instanceDirectoryFiles(MODEL_PATH);
|
$annotation->runtime(MODEL_PATH);
|
||||||
|
|
||||||
$this->onTask($server, $worker_id);
|
$this->onTask($server, $worker_id);
|
||||||
} else {
|
} else {
|
||||||
$annotation->instanceDirectoryFiles(APP_PATH);
|
|
||||||
|
$start = microtime(true);
|
||||||
|
|
||||||
|
$annotation->runtime(CONTROLLER_PATH);
|
||||||
|
$annotation->runtime(APP_PATH, CONTROLLER_PATH);
|
||||||
|
|
||||||
|
$this->error('use time ' . (microtime(true) - $start));
|
||||||
|
|
||||||
$this->onWorker($server, $worker_id);
|
$this->onWorker($server, $worker_id);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user