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
}
public function execute(mixed $class, mixed $method = ''): mixed
/**
* @throws Exception
*/
public function execute(mixed $class, mixed $method = ''): bool
{
// TODO: Change the autogenerated stub
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
* @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->addAsync($this->name, $class);
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
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): mixed
{
public function execute(mixed $class, mixed $method = null): bool
{
// TODO: Implement execute() method.
SEvent::on($this->name, [$class, $method], $this->params);
return true;
+5 -4
View File
@@ -9,10 +9,11 @@ use Closure;
interface IAnnotation
{
/**
* @param array $handler
* @return mixed
*/
/**
* @param mixed $class
* @param mixed $method
* @return mixed
*/
public function execute(mixed $class, mixed $method = ''): mixed;
+9 -6
View File
@@ -5,6 +5,7 @@ namespace Annotation;
use Exception;
use ReflectionException;
use ReflectionProperty;
use Snowflake\Snowflake;
@@ -26,12 +27,14 @@ use Snowflake\Snowflake;
}
/**
* @param array $handler
* @return mixed
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): mixed
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws ReflectionException
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
$injectValue = $this->parseInjectValue();
if (!($method instanceof ReflectionProperty)) {
+8 -5
View File
@@ -4,6 +4,7 @@
namespace Annotation;
use Exception;
use Kafka\ConsumerInterface;
use Kafka\TaskContainer;
use Snowflake\Snowflake;
@@ -26,11 +27,13 @@ use Snowflake\Snowflake;
}
/**
* @param array $handler
* @return mixed
*/
public function execute(mixed $class, mixed $method = null): mixed
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
if (!($class instanceof ConsumerInterface)) {
return false;
+303 -288
View File
@@ -10,8 +10,11 @@ use Annotation\Model\Set;
use Attribute;
use DirectoryIterator;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Throwable;
@@ -24,331 +27,343 @@ class Loader extends BaseObject
{
private array $_classes = [];
private array $_classes = [];
private array $_directory = [];
private array $_directory = [];
/**
* @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);
}
private array $_annotationMaps = [];
/**
* @return array
*/
public function getClasses(): array
{
return $this->_classes;
}
/**
* @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);
}
/**
* @param string $class
* @param string $property
* @return mixed
*/
public function getProperty(string $class, string $property = ''): mixed
{
if (!isset($this->_classes[$class])) {
return null;
}
$properties = $this->_classes[$class]['property'];
if (!empty($property) && isset($properties[$property])) {
return $properties[$property];
}
return $properties;
}
/**
* @return array
*/
public function getClasses(): array
{
return $this->_classes;
}
/**
* @param string $class
* @param mixed $handler
* @return Loader
*/
public function injectProperty(string $class, object $handler): static
{
$properties = $this->getProperty($class);
if (empty($properties)) {
return $this;
}
foreach ($properties as $property => $attributes) {
foreach ($attributes as $attribute) {
$attribute->execute($handler, $property);
}
}
return $this;
}
/**
* @param string $class
* @param string $property
* @return mixed
*/
public function getProperty(string $class, string $property = ''): mixed
{
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 $method
* @return mixed
*/
public function getMethod(string $class, string $method = ''): array
{
if (!isset($this->_classes[$class])) {
return [];
}
$properties = $this->_classes[$class]['methods'];
if (!empty($method) && isset($properties[$method])) {
return $properties[$method];
}
return $properties;
}
/**
* @param string $class
* @param mixed $handler
* @return Loader
*/
public function injectProperty(string $class, object $handler): static
{
$properties = $this->getProperty($class);
if (empty($properties)) {
return $this;
}
foreach ($properties as $property => $attributes) {
foreach ($attributes as $attribute) {
$attribute->execute($handler, $property);
}
}
return $this;
}
/**
* @param string $class
* @return array
*/
public function getTarget(string $class): array
{
return $this->_classes[$class] ?? [];
}
/**
* @param string $class
* @param string $method
* @return mixed
*/
public function getMethod(string $class, string $method = ''): array
{
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 $namespace
* @throws Exception
*/
public function _scanDir(DirectoryIterator $paths, $namespace)
{
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 string $class
* @return array
*/
public function getTarget(string $class): array
{
return $this->_classes[$class] ?? [];
}
/**
* @param DirectoryIterator $path
* @param $namespace
* @throws Exception
*/
private function readFile(DirectoryIterator $path, $namespace)
{
try {
if ($path->getExtension() !== 'php') {
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 DirectoryIterator $paths
* @param $namespace
* @throws Exception
*/
public function _scanDir(DirectoryIterator $paths, $namespace)
{
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 string $path
* @param string $namespace
* @return \ReflectionClass|null
* @throws \ReflectionException
* @throws \Snowflake\Exception\NotFindClassException
*/
private function getReflect(DirectoryIterator $path, string $namespace): ?\ReflectionClass
{
return Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
}
/**
* @param DirectoryIterator $path
* @param $namespace
* @throws Exception
*/
private function readFile(DirectoryIterator $path, $namespace)
{
try {
if ($path->getExtension() !== 'php') {
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 array $_array
* @return array
*/
private function _targets(\ReflectionClass $replace, array $_array): array
{
foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) {
continue;
}
if ($attribute->getName() == Target::class) {
continue;
}
$_array['target'][] = $attribute->newInstance();
}
return $_array;
}
/**
* @param DirectoryIterator $path
* @param string $namespace
* @return ReflectionClass|null
* @throws ReflectionException
* @throws NotFindClassException
*/
private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass
{
return Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
}
/**
* @param \ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _methods(\ReflectionClass $replace, array $_array): array
{
$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;
}
return $_array;
}
/**
* @param ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _targets(ReflectionClass $replace, array $_array): array
{
foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) {
continue;
}
if ($attribute->getName() == Target::class) {
continue;
}
$_array['target'][] = $attribute->newInstance();
}
return $_array;
}
/**
* @param \ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _properties(\ReflectionClass $replace, array $_array): array
{
$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;
}
return $_array;
}
/**
* @param ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _methods(ReflectionClass $replace, array $_array): array
{
$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;
}
return $_array;
}
/**
* @param string $path
* @param string|array $outPath
* @throws Exception
*/
public function loadByDirectory(string $path, string|array $outPath = '')
{
try {
$path = '/' . trim($path, '/');
foreach ($this->_directory as $key => $_path) {
$key = '/' . trim($key, '/');
if (!str_starts_with($key, $path) || in_array($key, $outPath)) {
continue;
}
$this->execute($_path);
}
} catch (Throwable $exception) {
$this->addError($exception, 'throwable');
}
}
/**
* @param ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _properties(ReflectionClass $replace, array $_array): array
{
$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;
}
return $_array;
}
/**
* @param DirectoryIterator $path
* @param string $namespace
* @return string
*/
private function explodeFileName(DirectoryIterator $path, string $namespace): string
{
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
$replace = str_replace('.php', '', $replace);
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
$explode = explode('\\', $replace);
array_shift($explode);
return $namespace . '\\' . implode('\\', $explode);
}
/**
* @param string $path
* @param string|array $outPath
* @throws Exception
*/
public function loadByDirectory(string $path, string|array $outPath = '')
{
try {
$path = '/' . trim($path, '/');
foreach ($this->_directory as $key => $_path) {
$key = '/' . trim($key, '/');
if (!str_starts_with($key, $path) || in_array($key, $outPath)) {
continue;
}
$this->execute($_path);
}
} catch (Throwable $exception) {
$this->addError($exception, 'throwable');
}
}
/**
* @param string $filePath
* @param string $className
*/
public function appendFileToDirectory(string $filePath, string $className)
{
$array = explode('/', $filePath);
unset($array[count($array) - 1]);
/**
* @param DirectoryIterator $path
* @param string $namespace
* @return string
*/
private function explodeFileName(DirectoryIterator $path, string $namespace): string
{
$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
*/
private function execute(array $classes)
{
if (empty($classes)) {
return;
}
$annotation = Snowflake::getAnnotation();
/**
* @param string $filePath
* @param string $className
*/
public function appendFileToDirectory(string $filePath, string $className)
{
$array = explode('/', $filePath);
unset($array[count($array) - 1]);
foreach ($classes as $className) {
$annotations = $this->_classes[$className] ?? null;
if ($annotations === null) {
continue;
}
$array = '/' . trim(implode('/', $array), '/');
/** @var \Annotation\Attribute $value */
foreach ($annotations['target'] ?? [] as $value) {
$value->execute($annotations['handler']);
}
foreach ($annotations['methods'] as $name => $attribute) {
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);
}
}
}
}
}
$this->_directory[$array][] = $className;
}
/**
* @param array $classes
* @throws Exception
*/
private function execute(array $classes)
{
if (empty($classes)) {
return;
}
$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 string $method
* @return mixed
* @throws \Exception
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): mixed
public function execute(mixed $class, mixed $method = null): bool
{
$class = ['class' => $class::class];
if (!empty($this->args)) {
+7 -4
View File
@@ -7,6 +7,7 @@ namespace Annotation\Model;
use Annotation\Annotation;
use Attribute;
use Database\ActiveRecord;
use Exception;
use Snowflake\Snowflake;
@@ -27,10 +28,12 @@ use Snowflake\Snowflake;
}
/**
* @param array $handler
* @return ActiveRecord
*/
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
$annotation = Snowflake::getAnnotation();
+4 -1
View File
@@ -6,6 +6,7 @@ namespace Annotation\Model;
use Annotation\Attribute;
use Database\ActiveRecord;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake;
@@ -28,8 +29,10 @@ use Snowflake\Snowflake;
/**
* @param array $handler
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
+2 -1
View File
@@ -6,6 +6,7 @@ namespace Annotation\Model;
use Annotation\Attribute;
use Database\ActiveRecord;
use Exception;
use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
@@ -25,7 +26,7 @@ use Snowflake\Snowflake;
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws \Exception
* @throws Exception
*/
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
*/
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
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
+3 -2
View File
@@ -21,7 +21,7 @@ use Snowflake\Snowflake;
* @param string|array $interceptor
* @throws
*/
#[Pure] public function __construct(public string|array $interceptor)
public function __construct(public string|array $interceptor)
{
if (is_string($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
*/
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
*/
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
*/
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
* @throws ConfigException
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): Router
+6 -6
View File
@@ -35,12 +35,12 @@ use Snowflake\Snowflake;
}
/**
* @param array $handler
* @return Router
* @throws Exception
*/
/**
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): Router
{
// TODO: Implement setHandler() method.
+5 -4
View File
@@ -28,12 +28,13 @@ use Snowflake\Snowflake;
/**
* @param array $handler
* @return mixed
* @param mixed $class
* @param mixed $method
* @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->addConsumer($this->cmd, [$class, $method]);
return true; // TODO: Change the autogenerated stub
+7 -6
View File
@@ -37,12 +37,13 @@ use Snowflake\Snowflake;
}
/**
* @param array $handler
* @return mixed
* @throws Exception
*/
public function execute(mixed $class, mixed $method = ''): mixed
/**
* @param mixed $class
* @param mixed $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = ''): bool
{
$rpc = Snowflake::app()->getRpc();
$rpc->addProducer($this->cmd, [$class, $method], $this->config);