diff --git a/Annotation/Aspect.php b/Annotation/Aspect.php index 35442f05..f1baa74e 100644 --- a/Annotation/Aspect.php +++ b/Annotation/Aspect.php @@ -28,23 +28,20 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem } - /** - * @param array $handler - * @return mixed - * @throws Exception - */ - public function execute(array $handler): mixed + + + public function execute(mixed $class, mixed $method = ''): mixed { - // TODO: Change the autogenerated stub - if (!in_array(IAspect::class, class_implements($this->aspect))) { + // TODO: Change the autogenerated stub + if (!in_array(IAspect::class, class_implements($this->aspect))) { throw new Exception(ASPECT_ERROR . IAspect::class); } /** @var Aop $aop */ $aop = Snowflake::app()->get('aop'); - $aop->aop_add($handler, $this->aspect); + $aop->aop_add([$class, $method], $this->aspect); - return parent::execute($handler); + return true; } diff --git a/Annotation/Asynchronous.php b/Annotation/Asynchronous.php index 723dbe64..b74cdc10 100644 --- a/Annotation/Asynchronous.php +++ b/Annotation/Asynchronous.php @@ -5,6 +5,7 @@ namespace Annotation; use Exception; +use HttpServer\IInterface\Task; use Snowflake\Snowflake; @@ -32,10 +33,10 @@ use Snowflake\Snowflake; * @return bool * @throws Exception */ - public function execute(array $handler): bool + public function execute(mixed $class, mixed $method = null): mixed { $async = Snowflake::app()->getAsync(); - $async->addAsync($this->name, current($handler)); + $async->addAsync($this->name, $class); return true; } diff --git a/Annotation/Attribute.php b/Annotation/Attribute.php index 27cc7580..067281bc 100644 --- a/Annotation/Attribute.php +++ b/Annotation/Attribute.php @@ -22,14 +22,11 @@ abstract class Attribute implements IAnnotation { - /** - * @param array $handler - * @return mixed - */ - public function execute(array $handler): mixed - { - return $this; - } + public function execute(mixed $class, mixed $method = ''): mixed + { + // TODO: Implement execute() method. + return true; + } } diff --git a/Annotation/Event.php b/Annotation/Event.php index 6e40c0f8..3698d28a 100644 --- a/Annotation/Event.php +++ b/Annotation/Event.php @@ -33,10 +33,10 @@ use Snowflake\Event as SEvent; * @return bool * @throws Exception */ - public function execute(array $handler): bool + public function execute(mixed $class, mixed $method = null): mixed { // TODO: Implement execute() method. - SEvent::on($this->name, $handler, $this->params); + SEvent::on($this->name, [$class, $method], $this->params); return true; } diff --git a/Annotation/FileTree.php b/Annotation/FileTree.php deleted file mode 100644 index 4474fe5f..00000000 --- a/Annotation/FileTree.php +++ /dev/null @@ -1,80 +0,0 @@ -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 - * @param string $path - */ - public function addFile(string $className, string $path) - { - $this->files[] = $className; - $this->_filePath = $path; - } - - - /** - * @return array - */ - public function getFiles(): array - { - return $this->files; - } - - - /** - * @return string - */ - public function getDirPath(): string - { - return $this->_filePath; - } - - - /** - * @return array - */ - public function getChildes(): array - { - return $this->childes; - } - - -} diff --git a/Annotation/IAnnotation.php b/Annotation/IAnnotation.php index fae00476..8a707b68 100644 --- a/Annotation/IAnnotation.php +++ b/Annotation/IAnnotation.php @@ -9,11 +9,11 @@ use Closure; interface IAnnotation { - /** - * @param array $handler - * @return mixed - */ - public function execute(array $handler): mixed; + /** + * @param array $handler + * @return mixed + */ + public function execute(mixed $class, mixed $method = ''): mixed; } diff --git a/Annotation/Inject.php b/Annotation/Inject.php index 3de489ef..cad8e098 100644 --- a/Annotation/Inject.php +++ b/Annotation/Inject.php @@ -16,59 +16,59 @@ use Snowflake\Snowflake; { - /** - * Inject constructor. - * @param string $className - * @param array $args - */ - public function __construct(private string $className, private array $args = []) - { - } + /** + * Inject constructor. + * @param string $className + * @param array $args + */ + public function __construct(private string $className, private array $args = []) + { + } - /** - * @param array $handler - * @return mixed - * @throws Exception - */ - public function execute(array $handler): mixed - { - $injectValue = $this->parseInjectValue(); - if (!($handler[1] instanceof ReflectionProperty)) { - $handler[1] = new ReflectionProperty($handler[0], $handler[1]); - } + /** + * @param array $handler + * @return mixed + * @throws Exception + */ + public function execute(mixed $class, mixed $method = null): mixed + { + $injectValue = $this->parseInjectValue(); + if (!($class instanceof ReflectionProperty)) { + $class = new ReflectionProperty($class, $method); + } - /** @var ReflectionProperty $handler [1] */ - if ($handler[1]->isPrivate() || $handler[1]->isProtected()) { - $method = 'set' . ucfirst($handler[1]->getName()); - if (!method_exists($handler[0], $method)) { - return false; - } - $handler[0]->$method($injectValue); - } else { - $handler[0]->{$handler[1]->getName()} = $injectValue; - } - return $handler[0]; - } + /** @var ReflectionProperty $class */ + if ($class->isPrivate() || $class->isProtected()) { + $method = 'set' . ucfirst($class->getName()); + if (!method_exists($class, $method)) { + return false; + } + $class->$method($injectValue); + } else { + $class->{$method->getName()} = $injectValue; + } + return true; + } - /** - * @return mixed - * @throws Exception - */ - private function parseInjectValue(): mixed - { - if (class_exists($this->className)) { - $injectValue = Snowflake::createObject($this->className, $this->args); - } else if (Snowflake::app()->has($this->className)) { - $injectValue = Snowflake::app()->get($this->className); - } else { - $injectValue = $this->className; - } + /** + * @return mixed + * @throws Exception + */ + private function parseInjectValue(): mixed + { + if (class_exists($this->className)) { + $injectValue = Snowflake::createObject($this->className, $this->args); + } else if (Snowflake::app()->has($this->className)) { + $injectValue = Snowflake::app()->get($this->className); + } else { + $injectValue = $this->className; + } // if (!empty($this->args) && is_object($injectValue)) { // Snowflake::configure($injectValue, $this->args); // } - return $injectValue; - } + return $injectValue; + } } diff --git a/Annotation/Kafka.php b/Annotation/Kafka.php index 101f149a..314571bb 100644 --- a/Annotation/Kafka.php +++ b/Annotation/Kafka.php @@ -30,17 +30,17 @@ use Snowflake\Snowflake; * @param array $handler * @return mixed */ - public function execute(array $handler): mixed + public function execute(mixed $class, mixed $method = null): mixed { - if (!($handler[0] instanceof ConsumerInterface)) { + if (!($class instanceof ConsumerInterface)) { return false; } /** @var TaskContainer $container */ $container = Snowflake::app()->get('kafka-container'); - $container->addConsumer($this->topic, [$handler[0], 'onHandler']); + $container->addConsumer($this->topic, [$class, 'onHandler']); - return parent::execute($handler); // TODO: Change the autogenerated stub + return true; } diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 1049785d..9ce0477e 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -33,9 +33,6 @@ class Loader extends BaseObject private array $_directory = []; - private FileTree $files; - - /** * @return array */ @@ -44,13 +41,6 @@ class Loader extends BaseObject return $this->_directory; } - - public function init() - { - $this->files = new FileTree(); - } - - /** * @param $path * @param $namespace @@ -102,7 +92,7 @@ class Loader extends BaseObject } foreach ($properties as $property => $attributes) { foreach ($attributes as $attribute) { - $attribute->execute([$handler, $property]); + $attribute->execute($handler, $property); } } return $this; @@ -173,47 +163,20 @@ class Loader extends BaseObject if ($path->getExtension() !== 'php') { return; } - $replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace)); + $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(), 'target' => [], 'methods' => [], 'property' => []]; - foreach ($replace->getAttributes() as $attribute) { - if ($attribute->getName() == Attribute::class) { - continue; - } - if ($attribute->getName() == Target::class) { - continue; - } - $_array['target'][] = $attribute->newInstance(); - } + $_array['handler'] = $replace->getName(); + $_array['target'] = []; + $_array['methods'] = []; + $_array['property'] = []; - $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; - } - - $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; - } + $_array = $this->_targets($replace, $_array); + $_array = $this->_methods($replace, $_array); + $_array = $this->_properties($replace, $_array); $this->_fileMap[$replace->getFileName()] = $replace->getName(); @@ -224,6 +187,84 @@ class Loader extends BaseObject } + /** + * @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 \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 _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 _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 string $path * @param string|array $outPath @@ -301,14 +342,17 @@ class Loader extends BaseObject return; } $annotation = Snowflake::getAnnotation(); + foreach ($classes as $className) { $annotations = $this->_classes[$className] ?? null; if ($annotations === null) { continue; } - $class = clone $annotations['handler']; + + $class = $this->newInstance($annotations['handler']); + /** @var \Annotation\Attribute $value */ foreach ($annotations['target'] ?? [] as $value) { - $value->execute([$class]); + $value->execute($class); } foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { @@ -319,7 +363,7 @@ class Loader extends BaseObject } else if ($value instanceof Set) { $annotation->addSets($class::class, $value->name, $name); } else { - $value->execute([$class, $name]); + $value->execute($class, $name); } } } @@ -327,4 +371,16 @@ class Loader extends BaseObject } + /** + * @param $class + * @return object + * @throws \ReflectionException + * @throws \Snowflake\Exception\NotFindClassException + */ + private function newInstance($class) + { + $reflection = Snowflake::getDi()->getReflect($class); + return $reflection->newInstance(); + } + } diff --git a/Annotation/LocalService.php b/Annotation/LocalService.php index 3ad630af..33c0d8e9 100644 --- a/Annotation/LocalService.php +++ b/Annotation/LocalService.php @@ -34,22 +34,21 @@ use Snowflake\Snowflake; } - /** - * @param array $handler - * @return mixed - * @throws Exception - */ - public function execute(array $handler): mixed - { - $class = ['class' => $handler[0]::class]; - if (!empty($this->args)) { - $class = array_merge($class, $this->args); - } - - Snowflake::set($this->service, $class); - - return parent::execute($handler); // TODO: Change the autogenerated stub - } + /** + * @param object $class + * @param string $method + * @return mixed + * @throws \Exception + */ + public function execute(mixed $class, mixed $method = null): mixed + { + $class = ['class' => $class::class]; + if (!empty($this->args)) { + $class = array_merge($class, $this->args); + } + Snowflake::set($this->service, $class); + return true; // TODO: Change the autogenerated stub + } } diff --git a/Annotation/Model/Get.php b/Annotation/Model/Get.php index 05be24c7..ef6165e1 100644 --- a/Annotation/Model/Get.php +++ b/Annotation/Model/Get.php @@ -7,6 +7,7 @@ namespace Annotation\Model; use Annotation\Annotation; use Attribute; use Database\ActiveRecord; +use Snowflake\Snowflake; /** @@ -17,28 +18,25 @@ use Database\ActiveRecord; { - /** - * Get constructor. - * @param string $name - */ - public function __construct( - public string $name - ) - { - } + /** + * Get constructor. + * @param string $name + */ + public function __construct(public string $name) + { + } - /** - * @param array $handler - * @return ActiveRecord - */ - public function execute(array $handler): ActiveRecord - { - /** @var ActiveRecord $activeRecord */ - [$activeRecord, $method] = $handler; - - return $activeRecord->addGets($this->name, $method); - } + /** + * @param array $handler + * @return ActiveRecord + */ + public function execute(mixed $class, mixed $method = null): bool + { + $annotation = Snowflake::getAnnotation(); + $annotation->addGets($class::class, $this->name, $method); + return true; + } } diff --git a/Annotation/Model/Relation.php b/Annotation/Model/Relation.php index 71d5ad85..19520ba5 100644 --- a/Annotation/Model/Relation.php +++ b/Annotation/Model/Relation.php @@ -7,6 +7,7 @@ namespace Annotation\Model; use Annotation\Attribute; use Database\ActiveRecord; use JetBrains\PhpStorm\Pure; +use Snowflake\Snowflake; /** @@ -30,14 +31,11 @@ use JetBrains\PhpStorm\Pure; * @param array $handler * @return bool */ - public function execute(array $handler): bool + public function execute(mixed $class, mixed $method = null): bool { - /** @var ActiveRecord $activeRecord */ - [$activeRecord, $method] = $handler; - - $activeRecord->setRelate($this->name, $method); - - return true; + $annotation = Snowflake::getAnnotation(); + $annotation->addRelate($class::class, $this->name, $method); + return true; } } diff --git a/Annotation/Model/Set.php b/Annotation/Model/Set.php index ee82c42c..0d83c642 100644 --- a/Annotation/Model/Set.php +++ b/Annotation/Model/Set.php @@ -6,6 +6,7 @@ namespace Annotation\Model; use Annotation\Attribute; use Database\ActiveRecord; +use Snowflake\Snowflake; #[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute { @@ -20,17 +21,17 @@ use Database\ActiveRecord; } - - /** - * @param array $handler - * @return ActiveRecord - */ - public function execute(array $handler): ActiveRecord + /** + * @param mixed $class + * @param mixed|null $method + * @return bool + * @throws \Exception + */ + public function execute(mixed $class, mixed $method = null): bool { - /** @var ActiveRecord $activeRecord */ - [$activeRecord, $method] = $handler; - - return $activeRecord->addSets($this->name, $method); + $annotation = Snowflake::getAnnotation(); + $annotation->addSets($class::class, $this->name, $method); + return true; } diff --git a/Annotation/Port.php b/Annotation/Port.php index 42e607b1..410c7783 100644 --- a/Annotation/Port.php +++ b/Annotation/Port.php @@ -31,14 +31,14 @@ use Snowflake\Snowflake; * @return mixed * @throws Exception */ - public function execute(array $handler): mixed + public function execute(mixed $class, mixed $method = null): mixed { $router = Snowflake::app()->getRouter(); - if (!($handler[0] instanceof Porters)) { + if (!($class instanceof Porters)) { return true; } - $router->addPortListen($this->port, [$handler[0], 'process']); - return parent::execute($handler); + $router->addPortListen($this->port, [$class, 'process']); + return true; } } diff --git a/Annotation/Route/After.php b/Annotation/Route/After.php index feec0f99..e5d67a36 100644 --- a/Annotation/Route/After.php +++ b/Annotation/Route/After.php @@ -38,7 +38,7 @@ use Snowflake\Snowflake; * @param array $handler * @return After */ - public function execute(array $handler): static + public function execute(mixed $class, mixed $method = null): static { return $this; } diff --git a/Annotation/Route/Document.php b/Annotation/Route/Document.php index 760406aa..75e7a4d9 100644 --- a/Annotation/Route/Document.php +++ b/Annotation/Route/Document.php @@ -38,7 +38,7 @@ use Annotation\Attribute; * @param array $handler * @return array */ - public function execute(array $handler): array + public function execute(mixed $class, mixed $method = null): array { // TODO: Implement execute() method. return [$this->request, $this->response]; diff --git a/Annotation/Route/Filter.php b/Annotation/Route/Filter.php index 4b78d3f3..4deaa808 100644 --- a/Annotation/Route/Filter.php +++ b/Annotation/Route/Filter.php @@ -33,7 +33,7 @@ use Snowflake\Snowflake; * @return bool * @throws Exception */ - public function execute(array $handler): bool + public function execute(mixed $class, mixed $method = null): bool { return true; } diff --git a/Annotation/Route/Interceptor.php b/Annotation/Route/Interceptor.php index 28ec3c84..224b661d 100644 --- a/Annotation/Route/Interceptor.php +++ b/Annotation/Route/Interceptor.php @@ -43,7 +43,7 @@ use Snowflake\Snowflake; * @param array $handler * @return Interceptor */ - public function execute(array $handler): static + public function execute(mixed $class, mixed $method = null): static { return $this; } diff --git a/Annotation/Route/Limits.php b/Annotation/Route/Limits.php index 1928916c..19097d6e 100644 --- a/Annotation/Route/Limits.php +++ b/Annotation/Route/Limits.php @@ -42,7 +42,7 @@ use Snowflake\Snowflake; * @param array $handler * @return Limits */ - public function execute(array $handler): static + public function execute(mixed $class, mixed $method = null): static { return $this; } diff --git a/Annotation/Route/Middleware.php b/Annotation/Route/Middleware.php index 31fbee4d..e6558fdd 100644 --- a/Annotation/Route/Middleware.php +++ b/Annotation/Route/Middleware.php @@ -43,7 +43,7 @@ use HttpServer\IInterface\Middleware as IMiddleware; * @param array $handler * @return Middleware */ - public function execute(array $handler): static + public function execute(mixed $class, mixed $method = null): static { return $this; } diff --git a/Annotation/Route/Route.php b/Annotation/Route/Route.php index d42394ba..98c6e462 100644 --- a/Annotation/Route/Route.php +++ b/Annotation/Route/Route.php @@ -34,12 +34,12 @@ use Snowflake\Snowflake; * @throws ConfigException * @throws Exception */ - public function execute(array $handler): Router + public function execute(mixed $class, mixed $method = null): Router { // TODO: Implement setHandler() method. $router = Snowflake::app()->getRouter(); - $router->addRoute($this->uri, $handler, $this->method); + $router->addRoute($this->uri, [$class, $method], $this->method); return $router; } diff --git a/Annotation/Route/RpcProducer.php b/Annotation/Route/RpcProducer.php index cd17d47f..cad5fbaa 100644 --- a/Annotation/Route/RpcProducer.php +++ b/Annotation/Route/RpcProducer.php @@ -42,12 +42,12 @@ use Snowflake\Snowflake; * @return Router * @throws Exception */ - public function execute(array $handler): Router + public function execute(mixed $class, mixed $method = null): Router { // TODO: Implement setHandler() method. $router = Snowflake::app()->getRouter(); - $router->addRoute($this->uri, $handler, Request::HTTP_CMD) + $router->addRoute($this->uri, [$class, $method], Request::HTTP_CMD) ->setDataType($this->protocol); return $router; diff --git a/Annotation/Route/Socket.php b/Annotation/Route/Socket.php index c6e894ae..38f3044b 100644 --- a/Annotation/Route/Socket.php +++ b/Annotation/Route/Socket.php @@ -44,14 +44,14 @@ use Snowflake\Snowflake; * @return Router * @throws Exception */ - public function execute(array $handler): Router + public function execute(mixed $class, mixed $method = null): Router { // TODO: Implement setHandler() method. $router = Snowflake::app()->getRouter(); $method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri); - $router->addRoute($method, $handler, 'sw::socket'); + $router->addRoute($method, [$class, $method], 'sw::socket'); return $router; } diff --git a/Annotation/Rpc/Consumer.php b/Annotation/Rpc/Consumer.php index 4d3a6ec7..b498e733 100644 --- a/Annotation/Rpc/Consumer.php +++ b/Annotation/Rpc/Consumer.php @@ -32,11 +32,11 @@ use Snowflake\Snowflake; * @return mixed * @throws Exception */ - public function execute(array $handler): mixed + public function execute(mixed $class, mixed $method = ''): mixed { $rpc = Snowflake::app()->getRpc(); - $rpc->addConsumer($this->cmd, $handler); - return parent::execute($handler); // TODO: Change the autogenerated stub + $rpc->addConsumer($this->cmd, [$class, $method]); + return true; // TODO: Change the autogenerated stub } diff --git a/Annotation/Rpc/RpcClient.php b/Annotation/Rpc/RpcClient.php index e8ba0914..8876d7ea 100644 --- a/Annotation/Rpc/RpcClient.php +++ b/Annotation/Rpc/RpcClient.php @@ -16,39 +16,39 @@ use Snowflake\Snowflake; #[\Attribute(\Attribute::TARGET_CLASS)] class RpcClient extends Attribute { - private array $config; + private array $config; - /** - * RpcClient constructor. - * @param string $cmd - * @param int $port - * @param int $timeout - * @param int $mode - */ - public function __construct( - public string $cmd, - public int $port, - public int $timeout, - public int $mode - ) - { - $this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout]; - } + /** + * RpcClient constructor. + * @param string $cmd + * @param int $port + * @param int $timeout + * @param int $mode + */ + public function __construct( + public string $cmd, + public int $port, + public int $timeout, + public int $mode + ) + { + $this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout]; + } - /** - * @param array $handler - * @return mixed - * @throws Exception - */ - public function execute(array $handler): mixed - { - $rpc = Snowflake::app()->getRpc(); - $rpc->addProducer($this->cmd, $handler, $this->config); + /** + * @param array $handler + * @return mixed + * @throws Exception + */ + public function execute(mixed $class, mixed $method = ''): mixed + { + $rpc = Snowflake::app()->getRpc(); + $rpc->addProducer($this->cmd, [$class, $method], $this->config); - return parent::execute($handler); - } + return true; + } }