This commit is contained in:
2021-07-12 17:17:36 +08:00
parent f2083f1eaf
commit 857ec53870
20 changed files with 384 additions and 346 deletions
+4 -3
View File
@@ -28,9 +28,10 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
} }
/**
* @throws Exception
public function execute(mixed $class, mixed $method = ''): mixed */
public function execute(mixed $class, mixed $method = ''): bool
{ {
// TODO: Change the autogenerated stub // TODO: Change the autogenerated stub
if (!in_array(IAspect::class, class_implements($this->aspect))) { if (!in_array(IAspect::class, class_implements($this->aspect))) {
+4 -3
View File
@@ -29,12 +29,13 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): mixed public function execute(mixed $class, mixed $method = null): bool
{ {
$async = Snowflake::app()->getAsync(); $async = Snowflake::app()->getAsync();
$async->addAsync($this->name, $class); $async->addAsync($this->name, $class);
return true; return true;
+4 -3
View File
@@ -29,12 +29,13 @@ use Snowflake\Event as SEvent;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): mixed public function execute(mixed $class, mixed $method = null): bool
{ {
// TODO: Implement execute() method. // TODO: Implement execute() method.
SEvent::on($this->name, [$class, $method], $this->params); SEvent::on($this->name, [$class, $method], $this->params);
return true; return true;
+5 -4
View File
@@ -9,10 +9,11 @@ use Closure;
interface IAnnotation interface IAnnotation
{ {
/** /**
* @param array $handler * @param mixed $class
* @return mixed * @param mixed $method
*/ * @return mixed
*/
public function execute(mixed $class, mixed $method = ''): mixed; public function execute(mixed $class, mixed $method = ''): mixed;
+9 -6
View File
@@ -5,6 +5,7 @@ namespace Annotation;
use Exception; use Exception;
use ReflectionException;
use ReflectionProperty; use ReflectionProperty;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -26,12 +27,14 @@ use Snowflake\Snowflake;
} }
/** /**
* @param array $handler * @param mixed $class
* @return mixed * @param mixed|null $method
* @throws Exception * @return bool
*/ * @throws ReflectionException
public function execute(mixed $class, mixed $method = null): mixed * @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{ {
$injectValue = $this->parseInjectValue(); $injectValue = $this->parseInjectValue();
if (!($method instanceof ReflectionProperty)) { if (!($method instanceof ReflectionProperty)) {
+8 -5
View File
@@ -4,6 +4,7 @@
namespace Annotation; namespace Annotation;
use Exception;
use Kafka\ConsumerInterface; use Kafka\ConsumerInterface;
use Kafka\TaskContainer; use Kafka\TaskContainer;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -26,11 +27,13 @@ use Snowflake\Snowflake;
} }
/** /**
* @param array $handler * @param mixed $class
* @return mixed * @param mixed|null $method
*/ * @return bool
public function execute(mixed $class, mixed $method = null): mixed * @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{ {
if (!($class instanceof ConsumerInterface)) { if (!($class instanceof ConsumerInterface)) {
return false; return false;
+303 -288
View File
@@ -10,8 +10,11 @@ use Annotation\Model\Set;
use Attribute; use Attribute;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod; use ReflectionMethod;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Throwable; use Throwable;
@@ -24,331 +27,343 @@ class Loader extends BaseObject
{ {
private array $_classes = []; private array $_classes = [];
private array $_directory = []; private array $_directory = [];
/** private array $_annotationMaps = [];
* @return array
*/
public function getDirectory(): array
{
return $this->_directory;
}
/**
* @param $path
* @param $namespace
* @throws Exception
*/
public function loader($path, $namespace)
{
$this->_scanDir(new DirectoryIterator($path), $namespace);
}
/** /**
* @return array * @return array
*/ */
public function getClasses(): array public function getDirectory(): array
{ {
return $this->_classes; return $this->_directory;
} }
/**
* @param $path
* @param $namespace
* @throws Exception
*/
public function loader($path, $namespace)
{
$this->_scanDir(new DirectoryIterator($path), $namespace);
}
/** /**
* @param string $class * @return array
* @param string $property */
* @return mixed public function getClasses(): array
*/ {
public function getProperty(string $class, string $property = ''): mixed return $this->_classes;
{ }
if (!isset($this->_classes[$class])) {
return null;
}
$properties = $this->_classes[$class]['property'];
if (!empty($property) && isset($properties[$property])) {
return $properties[$property];
}
return $properties;
}
/** /**
* @param string $class * @param string $class
* @param mixed $handler * @param string $property
* @return Loader * @return mixed
*/ */
public function injectProperty(string $class, object $handler): static public function getProperty(string $class, string $property = ''): mixed
{ {
$properties = $this->getProperty($class); if (!isset($this->_classes[$class])) {
if (empty($properties)) { return null;
return $this; }
} $properties = $this->_classes[$class]['property'];
foreach ($properties as $property => $attributes) { if (!empty($property) && isset($properties[$property])) {
foreach ($attributes as $attribute) { return $properties[$property];
$attribute->execute($handler, $property); }
} return $properties;
} }
return $this;
}
/** /**
* @param string $class * @param string $class
* @param string $method * @param mixed $handler
* @return mixed * @return Loader
*/ */
public function getMethod(string $class, string $method = ''): array public function injectProperty(string $class, object $handler): static
{ {
if (!isset($this->_classes[$class])) { $properties = $this->getProperty($class);
return []; if (empty($properties)) {
} return $this;
$properties = $this->_classes[$class]['methods']; }
if (!empty($method) && isset($properties[$method])) { foreach ($properties as $property => $attributes) {
return $properties[$method]; foreach ($attributes as $attribute) {
} $attribute->execute($handler, $property);
return $properties; }
} }
return $this;
}
/** /**
* @param string $class * @param string $class
* @return array * @param string $method
*/ * @return mixed
public function getTarget(string $class): array */
{ public function getMethod(string $class, string $method = ''): array
return $this->_classes[$class] ?? []; {
} if (!isset($this->_classes[$class])) {
return [];
}
$properties = $this->_classes[$class]['methods'];
if (!empty($method) && isset($properties[$method])) {
return $properties[$method];
}
return $properties;
}
/** /**
* @param DirectoryIterator $paths * @param string $class
* @param $namespace * @return array
* @throws Exception */
*/ public function getTarget(string $class): array
public function _scanDir(DirectoryIterator $paths, $namespace) {
{ return $this->_classes[$class] ?? [];
foreach ($paths as $path) { }
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
continue;
}
if ($path->isDir()) {
$iterator = new DirectoryIterator($path->getRealPath());
$directory = rtrim($path->getRealPath(), '/');
if (!isset($this->_directory[$directory])) {
$this->_directory[$directory] = [];
}
$this->_scanDir($iterator, $namespace);
} else {
$this->readFile($path, $namespace);
}
}
}
/** /**
* @param DirectoryIterator $path * @param DirectoryIterator $paths
* @param $namespace * @param $namespace
* @throws Exception * @throws Exception
*/ */
private function readFile(DirectoryIterator $path, $namespace) public function _scanDir(DirectoryIterator $paths, $namespace)
{ {
try { foreach ($paths as $path) {
if ($path->getExtension() !== 'php') { if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
return; continue;
} }
$replace = $this->getReflect($path, $namespace); if ($path->isDir()) {
if (empty($replace) || count($replace->getAttributes(Target::class)) < 1) { $iterator = new DirectoryIterator($path->getRealPath());
return; $directory = rtrim($path->getRealPath(), '/');
} if (!isset($this->_directory[$directory])) {
$this->appendFileToDirectory($path->getRealPath(), $replace->getName()); $this->_directory[$directory] = [];
}
$_array['handler'] = $replace->newInstance(); $this->_scanDir($iterator, $namespace);
$_array['target'] = []; } else {
$_array['methods'] = []; $this->readFile($path, $namespace);
$_array['property'] = []; }
}
$_array = $this->_targets($replace, $_array); }
$_array = $this->_methods($replace, $_array);
$_array = $this->_properties($replace, $_array);
$this->_classes[$replace->getName()] = $_array;
} catch (Throwable $throwable) {
$this->addError($throwable, 'throwable');
}
}
/** /**
* @param string $path * @param DirectoryIterator $path
* @param string $namespace * @param $namespace
* @return \ReflectionClass|null * @throws Exception
* @throws \ReflectionException */
* @throws \Snowflake\Exception\NotFindClassException private function readFile(DirectoryIterator $path, $namespace)
*/ {
private function getReflect(DirectoryIterator $path, string $namespace): ?\ReflectionClass try {
{ if ($path->getExtension() !== 'php') {
return Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); return;
} }
$replace = $this->getReflect($path, $namespace);
if (empty($replace) || count($replace->getAttributes(Target::class)) < 1) {
return;
}
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
$_array['handler'] = $replace->newInstance();
$_array['target'] = [];
$_array['methods'] = [];
$_array['property'] = [];
$_array = $this->_targets($replace, $_array);
$_array = $this->_methods($replace, $_array);
$_array = $this->_properties($replace, $_array);
$this->_classes[$replace->getName()] = $_array;
} catch (Throwable $throwable) {
$this->addError($throwable, 'throwable');
}
}
/** /**
* @param \ReflectionClass $replace * @param DirectoryIterator $path
* @param array $_array * @param string $namespace
* @return array * @return ReflectionClass|null
*/ * @throws ReflectionException
private function _targets(\ReflectionClass $replace, array $_array): array * @throws NotFindClassException
{ */
foreach ($replace->getAttributes() as $attribute) { private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass
if ($attribute->getName() == Attribute::class) { {
continue; return Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
} }
if ($attribute->getName() == Target::class) {
continue;
}
$_array['target'][] = $attribute->newInstance();
}
return $_array;
}
/** /**
* @param \ReflectionClass $replace * @param ReflectionClass $replace
* @param array $_array * @param array $_array
* @return array * @return array
*/ */
private function _methods(\ReflectionClass $replace, array $_array): array private function _targets(ReflectionClass $replace, array $_array): array
{ {
$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())) { if ($attribute->getName() == Target::class) {
continue; continue;
} }
$_method[] = $attribute->newInstance(); $_array['target'][] = $attribute->newInstance();
} }
$_array['methods'][$method->getName()] = $_method; return $_array;
} }
return $_array;
}
/** /**
* @param \ReflectionClass $replace * @param ReflectionClass $replace
* @param array $_array * @param array $_array
* @return array * @return array
*/ */
private function _properties(\ReflectionClass $replace, array $_array): array private function _methods(ReflectionClass $replace, array $_array): array
{ {
$methods = $replace->getProperties(); $methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) { foreach ($methods as $method) {
$_property = []; $_method = [];
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; }
} $_method[] = $attribute->newInstance();
$_property[] = $attribute->newInstance(); }
} $_array['methods'][$method->getName()] = $_method;
$_array['property'][$method->getName()] = $_property; }
} return $_array;
return $_array; }
}
/** /**
* @param string $path * @param ReflectionClass $replace
* @param string|array $outPath * @param array $_array
* @throws Exception * @return array
*/ */
public function loadByDirectory(string $path, string|array $outPath = '') private function _properties(ReflectionClass $replace, array $_array): array
{ {
try { $methods = $replace->getProperties();
$path = '/' . trim($path, '/'); foreach ($methods as $method) {
foreach ($this->_directory as $key => $_path) { $_property = [];
$key = '/' . trim($key, '/'); if ($method->isStatic()) continue;
if (!str_starts_with($key, $path) || in_array($key, $outPath)) { foreach ($method->getAttributes() as $attribute) {
continue; if (!class_exists($attribute->getName())) {
} continue;
$this->execute($_path); }
} $_property[] = $attribute->newInstance();
} catch (Throwable $exception) { }
$this->addError($exception, 'throwable'); $_array['property'][$method->getName()] = $_property;
} }
} return $_array;
}
/** /**
* @param DirectoryIterator $path * @param string $path
* @param string $namespace * @param string|array $outPath
* @return string * @throws Exception
*/ */
private function explodeFileName(DirectoryIterator $path, string $namespace): string public function loadByDirectory(string $path, string|array $outPath = '')
{ {
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath()); try {
$path = '/' . trim($path, '/');
$replace = str_replace('.php', '', $replace); foreach ($this->_directory as $key => $_path) {
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace); $key = '/' . trim($key, '/');
$explode = explode('\\', $replace); if (!str_starts_with($key, $path) || in_array($key, $outPath)) {
array_shift($explode); continue;
}
return $namespace . '\\' . implode('\\', $explode); $this->execute($_path);
} }
} catch (Throwable $exception) {
$this->addError($exception, 'throwable');
}
}
/** /**
* @param string $filePath * @param DirectoryIterator $path
* @param string $className * @param string $namespace
*/ * @return string
public function appendFileToDirectory(string $filePath, string $className) */
{ private function explodeFileName(DirectoryIterator $path, string $namespace): string
$array = explode('/', $filePath); {
unset($array[count($array) - 1]); $replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
$array = '/' . trim(implode('/', $array), '/'); $replace = str_replace('.php', '', $replace);
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
$explode = explode('\\', $replace);
array_shift($explode);
$this->_directory[$array][] = $className; return $namespace . '\\' . implode('\\', $explode);
} }
/** /**
* @param array $classes * @param string $filePath
*/ * @param string $className
private function execute(array $classes) */
{ public function appendFileToDirectory(string $filePath, string $className)
if (empty($classes)) { {
return; $array = explode('/', $filePath);
} unset($array[count($array) - 1]);
$annotation = Snowflake::getAnnotation();
foreach ($classes as $className) { $array = '/' . trim(implode('/', $array), '/');
$annotations = $this->_classes[$className] ?? null;
if ($annotations === null) {
continue;
}
/** @var \Annotation\Attribute $value */ $this->_directory[$array][] = $className;
foreach ($annotations['target'] ?? [] as $value) { }
$value->execute($annotations['handler']);
}
foreach ($annotations['methods'] as $name => $attribute) { /**
foreach ($attribute as $value) { * @param array $classes
if ($value instanceof Relation) { * @throws Exception
$annotation->addRelate($className, $value->name, $name); */
} else if ($value instanceof Get) { private function execute(array $classes)
$annotation->addGets($className, $value->name, $name); {
} else if ($value instanceof Set) { if (empty($classes)) {
$annotation->addSets($className, $value->name, $name); return;
} else { }
$value->execute($annotations['handler'], $name); $annotation = Snowflake::getAnnotation();
}
} foreach ($classes as $className) {
} $annotations = $this->_classes[$className] ?? null;
} if ($annotations === null) {
} continue;
}
foreach ($annotations['methods'] as $name => $attribute) {
$this->methods($annotations, $attribute, $annotation, $className, $name);
}
}
}
/**
* @param $annotations
* @param $attribute
* @param $annotation
* @param $className
* @param $name
*/
private function methods($annotations, $attribute, $annotation, $className, $name)
{
foreach ($attribute as $value) {
if ($value instanceof Relation) {
$annotation->addRelate($className, $value->name, $name);
} else if ($value instanceof Get) {
$annotation->addGets($className, $value->name, $name);
} else if ($value instanceof Set) {
$annotation->addSets($className, $value->name, $name);
} else {
$value->execute($annotations['handler'], $name);
}
}
}
} }
+3 -3
View File
@@ -37,10 +37,10 @@ use Snowflake\Snowflake;
/** /**
* @param object $class * @param object $class
* @param string $method * @param string $method
* @return mixed * @return bool
* @throws \Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): mixed public function execute(mixed $class, mixed $method = null): bool
{ {
$class = ['class' => $class::class]; $class = ['class' => $class::class];
if (!empty($this->args)) { if (!empty($this->args)) {
+7 -4
View File
@@ -7,6 +7,7 @@ namespace Annotation\Model;
use Annotation\Annotation; use Annotation\Annotation;
use Attribute; use Attribute;
use Database\ActiveRecord; use Database\ActiveRecord;
use Exception;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -27,10 +28,12 @@ use Snowflake\Snowflake;
} }
/** /**
* @param array $handler * @param mixed $class
* @return ActiveRecord * @param mixed|null $method
*/ * @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
$annotation = Snowflake::getAnnotation(); $annotation = Snowflake::getAnnotation();
+4 -1
View File
@@ -6,6 +6,7 @@ namespace Annotation\Model;
use Annotation\Attribute; use Annotation\Attribute;
use Database\ActiveRecord; use Database\ActiveRecord;
use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -28,8 +29,10 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return bool * @return bool
* @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
+2 -1
View File
@@ -6,6 +6,7 @@ namespace Annotation\Model;
use Annotation\Attribute; use Annotation\Attribute;
use Database\ActiveRecord; use Database\ActiveRecord;
use Exception;
use Snowflake\Snowflake; use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute #[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
@@ -25,7 +26,7 @@ use Snowflake\Snowflake;
* @param mixed $class * @param mixed $class
* @param mixed|null $method * @param mixed|null $method
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
+2 -1
View File
@@ -35,7 +35,8 @@ use Annotation\Attribute;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return array * @return array
*/ */
public function execute(mixed $class, mixed $method = null): array public function execute(mixed $class, mixed $method = null): array
+2 -2
View File
@@ -29,9 +29,9 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return bool * @return bool
* @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
+3 -2
View File
@@ -21,7 +21,7 @@ use Snowflake\Snowflake;
* @param string|array $interceptor * @param string|array $interceptor
* @throws * @throws
*/ */
#[Pure] public function __construct(public string|array $interceptor) public function __construct(public string|array $interceptor)
{ {
if (is_string($this->interceptor)) { if (is_string($this->interceptor)) {
$this->interceptor = [$this->interceptor]; $this->interceptor = [$this->interceptor];
@@ -40,7 +40,8 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return Interceptor * @return Interceptor
*/ */
public function execute(mixed $class, mixed $method = null): static public function execute(mixed $class, mixed $method = null): static
+2 -1
View File
@@ -39,7 +39,8 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return Limits * @return Limits
*/ */
public function execute(mixed $class, mixed $method = null): static public function execute(mixed $class, mixed $method = null): static
+2 -1
View File
@@ -40,7 +40,8 @@ use HttpServer\IInterface\Middleware as IMiddleware;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return Middleware * @return Middleware
*/ */
public function execute(mixed $class, mixed $method = null): static public function execute(mixed $class, mixed $method = null): static
+2 -2
View File
@@ -29,9 +29,9 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return Router * @return Router
* @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): Router public function execute(mixed $class, mixed $method = null): Router
+6 -6
View File
@@ -35,12 +35,12 @@ use Snowflake\Snowflake;
} }
/**
/** * @param mixed $class
* @param array $handler * @param mixed|null $method
* @return Router * @return Router
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): Router public function execute(mixed $class, mixed $method = null): Router
{ {
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
+5 -4
View File
@@ -28,12 +28,13 @@ use Snowflake\Snowflake;
/** /**
* @param array $handler * @param mixed $class
* @return mixed * @param mixed $method
* @return bool
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = ''): mixed public function execute(mixed $class, mixed $method = ''): bool
{ {
$rpc = Snowflake::app()->getRpc(); $rpc = Snowflake::app()->getRpc();
$rpc->addConsumer($this->cmd, [$class, $method]); $rpc->addConsumer($this->cmd, [$class, $method]);
return true; // TODO: Change the autogenerated stub return true; // TODO: Change the autogenerated stub
+7 -6
View File
@@ -37,12 +37,13 @@ use Snowflake\Snowflake;
} }
/** /**
* @param array $handler * @param mixed $class
* @return mixed * @param mixed $method
* @throws Exception * @return bool
*/ * @throws Exception
public function execute(mixed $class, mixed $method = ''): mixed */
public function execute(mixed $class, mixed $method = ''): bool
{ {
$rpc = Snowflake::app()->getRpc(); $rpc = Snowflake::app()->getRpc();
$rpc->addProducer($this->cmd, [$class, $method], $this->config); $rpc->addProducer($this->cmd, [$class, $method], $this->config);