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..42c5581b 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 (!($method instanceof ReflectionProperty)) { + $method = 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 ($method->isPrivate() || $method->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..a7e42c8c 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..66c8d0d8 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); + $path = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri); - $router->addRoute($method, $handler, 'sw::socket'); + $router->addRoute($path, [$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; + } } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index de233768..35d9b698 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -431,19 +431,19 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess [$sql, $param] = SqlBuilder::builder(static::find())->insert($param); - $trance = $dbConnection->beginTransaction(); +// $trance = $dbConnection->beginTransaction(); try { if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) { throw new Exception('保存失败.'); } - $trance->commit(); +// $trance->commit(); $lastId = $this->setPrimary($lastId, $param); $this->refresh(); SEvent::trigger(self::AFTER_SAVE, [$attributes, $param]); } catch (\Throwable $exception) { - $trance->rollback(); +// $trance->rollback(); $lastId = $this->addError($exception, 'mysql'); } return $lastId; @@ -498,13 +498,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess return $generate; } - $trance = $command->beginTransaction(); +// $trance = $command->beginTransaction(); if (!$command->createCommand(...$generate)->save(false, $this)) { - $trance->rollback(); +// $trance->rollback(); return false; } - $trance->commit(); +// $trance->commit(); $this->refresh(); diff --git a/Database/Command.php b/Database/Command.php index 2eaef4ee..06d20009 100644 --- a/Database/Command.php +++ b/Database/Command.php @@ -22,249 +22,291 @@ use Swoole\Coroutine; */ class Command extends Component { - const ROW_COUNT = 'ROW_COUNT'; - const FETCH = 'FETCH'; - const FETCH_ALL = 'FETCH_ALL'; - const EXECUTE = 'EXECUTE'; - const FETCH_COLUMN = 'FETCH_COLUMN'; + const ROW_COUNT = 'ROW_COUNT'; + const FETCH = 'FETCH'; + const FETCH_ALL = 'FETCH_ALL'; + const EXECUTE = 'EXECUTE'; + const FETCH_COLUMN = 'FETCH_COLUMN'; - const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; + const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; - /** @var Connection */ - public Connection $db; + /** @var Connection */ + public Connection $db; - /** @var ?string */ - public ?string $sql = ''; + /** @var ?string */ + public ?string $sql = ''; - /** @var array */ - public array $params = []; + /** @var array */ + public array $params = []; - /** @var string */ - private string $_modelName; + /** @var string */ + private string $_modelName; - private ?PDOStatement $prepare = null; + private ?PDOStatement $prepare = null; - /** - * @return array|bool|int|string|PDOStatement|null - * @throws Exception - */ - public function incrOrDecr(): array|bool|int|string|PDOStatement|null - { - return $this->execute(static::EXECUTE); - } + /** + * @return array|bool|int|string|PDOStatement|null + * @throws Exception + */ + public function incrOrDecr(): array|bool|int|string|PDOStatement|null + { + return $this->execute(static::EXECUTE); + } - /** - * @param bool $isInsert - * @param bool $hasAutoIncrement - * @return int|bool|array|string|null - * @throws Exception - */ - public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null - { - return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement); - } + /** + * @param bool $isInsert + * @param bool $hasAutoIncrement + * @return int|bool|array|string|null + * @throws Exception + */ + public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null + { + return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement); + } - /** - * @return int|bool|array|string|null - * @throws Exception - */ - public function all(): int|bool|array|string|null - { - return $this->execute(static::FETCH_ALL); - } + /** + * @return int|bool|array|string|null + * @throws Exception + */ + public function all(): int|bool|array|string|null + { + return $this->execute(static::FETCH_ALL); + } - /** - * @return array|bool|int|string|null - * @throws Exception - */ - public function one(): null|array|bool|int|string - { - return $this->execute(static::FETCH); - } + /** + * @return array|bool|int|string|null + * @throws Exception + */ + public function one(): null|array|bool|int|string + { + return $this->execute(static::FETCH); + } - /** - * @return int|bool|array|string|null - * @throws Exception - */ - public function fetchColumn(): int|bool|array|string|null - { - return $this->execute(static::FETCH_COLUMN); - } + /** + * @return int|bool|array|string|null + * @throws Exception + */ + public function fetchColumn(): int|bool|array|string|null + { + return $this->execute(static::FETCH_COLUMN); + } - /** - * @return int|bool|array|string|null - * @throws Exception - */ - public function rowCount(): int|bool|array|string|null - { - return $this->execute(static::ROW_COUNT); - } + /** + * @return int|bool|array|string|null + * @throws Exception + */ + public function rowCount(): int|bool|array|string|null + { + return $this->execute(static::ROW_COUNT); + } - /** - * @return int|bool|array|string|null - * @throws Exception - */ - public function flush(): int|bool|array|string|null - { - return $this->execute(static::EXECUTE); - } + /** + * @return int|bool|array|string|null + * @throws Exception + */ + public function flush(): int|bool|array|string|null + { + return $this->execute(static::EXECUTE); + } - /** - * @param $type - * @param null $isInsert - * @param bool $hasAutoIncrement - * @return int|bool|array|string|null - * @throws Exception - */ - private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null - { - try { - if ($type === static::EXECUTE) { - $result = $this->insert_or_change($isInsert, $hasAutoIncrement); - } else { - $result = $this->search($type); - } - return $result; - } catch (\Throwable $exception) { - return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); - } - } + /** + * @param $type + * @param null $isInsert + * @param bool $hasAutoIncrement + * @return int|bool|array|string|null + * @throws Exception + */ + private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null + { + try { + if ($type === static::EXECUTE) { + $result = $this->insert_or_change($isInsert, $hasAutoIncrement); + } else { + $result = $this->search($type); + } + if ($this->prepare) { + $this->prepare->closeCursor(); + } + return $result; + } catch (\Throwable $exception) { + return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); + } + } - /** - * @param $type - * @return mixed - * @throws Exception - */ - private function search($type): mixed - { - $connect = $this->db->getConnect($this->sql); - if (!($query = $connect?->query($this->sql))) { - return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.'); - } - defer(fn() => $query->closeCursor()); - if ($type === static::FETCH_COLUMN) { - return $query->fetchAll(PDO::FETCH_ASSOC); - } else if ($type === static::ROW_COUNT) { - return $query->rowCount(); - } else if ($type === static::FETCH_ALL) { - return $query->fetchAll(PDO::FETCH_ASSOC); - } else { - return $query->fetch(PDO::FETCH_ASSOC); - } - } + /** + * @param $type + * @return mixed + * @throws Exception + */ + private function search($type): mixed + { + if (($prepare = $this->prepare()) == false) { + return false; + } + if ($type === static::FETCH_COLUMN) { + $data = $prepare->fetchAll(PDO::FETCH_ASSOC); + } else if ($type === static::ROW_COUNT) { + $data = $prepare->rowCount(); + } else if ($type === static::FETCH_ALL) { + $data = $prepare->fetchAll(PDO::FETCH_ASSOC); + } else { + $data = $prepare->fetch(PDO::FETCH_ASSOC); + } + $prepare->closeCursor(); + return $data; + } - /** - * @param $isInsert - * @param $hasAutoIncrement - * @return bool|string|int - * @throws Exception - */ - private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int - { - if (($result = $this->initPDOStatement()) === false) { - return $result; - } - if ($isInsert === false) { - return true; - } - if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) { - return $this->addError(static::DB_ERROR_MESSAGE, 'mysql'); - } - return $result == 0 ? true : $result; - } + /** + * @param $isInsert + * @param $hasAutoIncrement + * @return bool|string|int + * @throws Exception + */ + private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int + { + if (($result = $this->getPdoStatement()) === false) { + return $result; + } + if ($isInsert === false) { + return true; + } + if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) { + return $this->addError(static::DB_ERROR_MESSAGE, 'mysql'); + } + return $result == 0 ? true : $result; + } - /** - * 重新构建 - * @throws - */ - private function initPDOStatement(): bool|int - { - if (empty($this->sql)) { - return $this->addError('no sql.', 'mysql'); - } - if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { - return $this->addError('get client error.', 'mysql'); - } - if (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) { - $error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE; + /** + * 重新构建 + * @throws + */ + private function getPdoStatement(): bool|int + { + if (empty($this->sql)) { + return $this->addError('no sql.', 'mysql'); + } + if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { + return $this->addError('get client error.', 'mysql'); + } + if (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) { + return $this->addError($this->errorMessage($prepare), 'mysql'); + } + $result = $this->checkResponse($prepare, $connect); + $prepare->closeCursor(); + return $result; + } - return $this->addError($this->sql . ':' . $error, 'mysql'); - } - $result = $prepare->execute($this->params); - defer(fn() => $prepare->closeCursor()); - if ($result === false) { - return $this->addError($connect->errorInfo()[2], 'mysql'); - } - return (int)$connect->lastInsertId(); - } - /** - * @param $modelName - * @return $this - */ - public function setModelName($modelName): static - { - $this->_modelName = $modelName; - return $this; - } + /** + * @param $prepare + * @return string + */ + private function errorMessage($prepare) + { + return $this->sql . ':' . ($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); + } - /** - * @return string - */ - public function getModelName(): string - { - return $this->_modelName; - } - /** - * @return int|bool|array|string|null - * @throws Exception - */ - public function delete(): int|bool|array|string|null - { - return $this->execute(static::EXECUTE); - } + /** + * @return bool|\PDOStatement + * @throws \Exception + */ + private function prepare(): bool|PDOStatement + { + if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { + return $this->addError('get client error.', 'mysql'); + } + if (!(($prepare = $connect->query($this->sql)) instanceof PDOStatement)) { + $error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE; + return $this->addError($this->sql . ':' . $error, 'mysql'); + } + return $prepare; + } - /** - * @param null $scope - * @param bool $insert - * @return int|bool|array|string|null - * @throws Exception - */ - public function exec($scope = null, $insert = false): int|bool|array|string|null - { - return $this->execute(static::EXECUTE, $insert, $scope); - } - /** - * @param array|null $data - * @return $this - */ - public function bindValues(array $data = NULL): static - { - if (!is_array($this->params)) { - $this->params = []; - } - if (!empty($data)) { - $this->params = array_merge($this->params, $data); - } - return $this; - } + /** + * @param $prepare + * @param $connect + * @return bool|int + * @throws \Exception + */ + private function checkResponse($prepare, $connect) + { + $result = $prepare->execute($this->params); + if ($result === false) { + return $this->addError($connect->errorInfo()[2], 'mysql'); + } + return (int)$connect->lastInsertId(); + } - /** - * @param $sql - * @return $this - * @throws Exception - */ - public function setSql($sql): static - { - $this->sql = $sql; - return $this; - } + + /** + * @param $modelName + * @return $this + */ + public function setModelName($modelName): static + { + $this->_modelName = $modelName; + return $this; + } + + /** + * @return string + */ + public function getModelName(): string + { + return $this->_modelName; + } + + /** + * @return int|bool|array|string|null + * @throws Exception + */ + public function delete(): int|bool|array|string|null + { + return $this->execute(static::EXECUTE); + } + + /** + * @param null $scope + * @param bool $insert + * @return int|bool|array|string|null + * @throws Exception + */ + public function exec($scope = null, $insert = false): int|bool|array|string|null + { + return $this->execute(static::EXECUTE, $insert, $scope); + } + + /** + * @param array $data + * @return $this + */ + public function bindValues(array $data = []): static + { + if (!is_array($this->params)) { + $this->params = []; + } + if (!empty($data)) { + $this->params = array_merge($this->params, $data); + } + return $this; + } + + /** + * @param $sql + * @return $this + * @throws Exception + */ + public function setSql($sql): static + { + $this->sql = $sql; + return $this; + } } diff --git a/Database/Connection.php b/Database/Connection.php index ca9eef49..ca1f5337 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -27,293 +27,293 @@ use Snowflake\Snowflake; */ class Connection extends Component { - const TRANSACTION_COMMIT = 'transaction::commit'; - const TRANSACTION_ROLLBACK = 'transaction::rollback'; + const TRANSACTION_COMMIT = 'transaction::commit'; + const TRANSACTION_ROLLBACK = 'transaction::rollback'; - public string $id = 'db'; - public string $cds = ''; - public string $password = ''; - public string $username = ''; - public string $charset = 'utf-8'; - public string $tablePrefix = ''; + public string $id = 'db'; + public string $cds = ''; + public string $password = ''; + public string $username = ''; + public string $charset = 'utf-8'; + public string $tablePrefix = ''; - public int $timeout = 1900; + public int $timeout = 1900; - public int $maxNumber = 200; + public int $maxNumber = 200; - /** - * @var bool - * enable database cache - */ - public bool $enableCache = false; - public string $cacheDriver = 'redis'; + /** + * @var bool + * enable database cache + */ + public bool $enableCache = false; + public string $cacheDriver = 'redis'; - /** - * @var array - * - * @example [ - * 'cds' => 'mysql:dbname=dbname;host=127.0.0.1', - * 'username' => 'root', - * 'password' => 'root' - * ] - */ - public array $slaveConfig = []; + /** + * @var array + * + * @example [ + * 'cds' => 'mysql:dbname=dbname;host=127.0.0.1', + * 'username' => 'root', + * 'password' => 'root' + * ] + */ + public array $slaveConfig = []; - private ?Schema $_schema = null; + private ?Schema $_schema = null; - /** - * @throws Exception - */ - public function init() - { - Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); - Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); - } + /** + * @throws Exception + */ + public function init() + { + Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); + Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); + } - /** - * @throws Exception - */ - public function enablingTransactions() - { - if (!Db::transactionsActive()) { - return; - } - $this->beginTransaction(); + /** + * @throws Exception + */ + public function enablingTransactions() + { + if (!Db::transactionsActive()) { + return; + } + $this->beginTransaction(); - Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); - Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); - } + Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); + Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); + } - /** - * @param null $sql - * @return PDO - * @throws Exception - */ - public function getConnect($sql = NULL): PDO - { - return $this->getPdo($sql); - } + /** + * @param null $sql + * @return PDO + * @throws Exception + */ + public function getConnect($sql = NULL): PDO + { + return $this->getPdo($sql); + } - /** - * @throws Exception - */ - public function fill() - { - $connections = $this->connections(); - $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); - if (!empty($this->slaveConfig)) { - $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); - } - } + /** + * @throws Exception + */ + public function fill() + { + $connections = $this->connections(); + $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); + if (!empty($this->slaveConfig)) { + $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); + } + } - /** - * @param $sql - * @return PDO - * @throws Exception - */ - private function getPdo($sql): PDO - { - $connections = $this->connections(); - $connections->setTimeout($this->timeout); - if ($this->isWrite($sql)) { - $connect = $connections->get(['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password], true); - } else { - $connect = $connections->get(['cds' => $this->slaveConfig['cds'], 'username' => $this->username, 'password' => $this->password], false); - } - return $connect; - } + /** + * @param $sql + * @return PDO + * @throws Exception + */ + private function getPdo($sql): PDO + { + $connections = $this->connections(); + $connections->setTimeout($this->timeout); + if ($this->isWrite($sql)) { + $connect = $connections->get(['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password], true); + } else { + $connect = $connections->get(['cds' => $this->slaveConfig['cds'], 'username' => $this->username, 'password' => $this->password], false); + } + return $connect; + } - /** - * @return mixed - * @throws ReflectionException - * @throws NotFindClassException - */ - public function getSchema(): Schema - { - if ($this->_schema === null) { - $this->_schema = Snowflake::createObject([ - 'class' => Schema::class, - 'db' => $this - ]); - } - return $this->_schema; - } + /** + * @return mixed + * @throws ReflectionException + * @throws NotFindClassException + */ + public function getSchema(): Schema + { + if ($this->_schema === null) { + $this->_schema = Snowflake::createObject([ + 'class' => Schema::class, + 'db' => $this + ]); + } + return $this->_schema; + } - /** - * @param $sql - * @return bool - */ - #[Pure] public function isWrite($sql): bool - { - if (empty($sql)) return false; - if (str_starts_with(strtolower($sql), 'select')) { - return false; - } - return true; - } + /** + * @param $sql + * @return bool + */ + #[Pure] public function isWrite($sql): bool + { + if (empty($sql)) return false; + if (str_starts_with(strtolower($sql), 'select')) { + return false; + } + return true; + } - /** - * @return mixed - * @throws Exception - */ - public function getCacheDriver(): mixed - { - if (!$this->enableCache) { - return null; - } - return Snowflake::app()->get($this->cacheDriver); - } + /** + * @return mixed + * @throws Exception + */ + public function getCacheDriver(): mixed + { + if (!$this->enableCache) { + return null; + } + return Snowflake::app()->get($this->cacheDriver); + } - /** - * @return PDO - * @throws Exception - */ - public function masterInstance(): PDO - { - return $this->connections()->get([ - 'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password - ], true); - } + /** + * @return PDO + * @throws Exception + */ + public function masterInstance(): PDO + { + return $this->connections()->get([ + 'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password + ], true); + } - /** - * @return PDO - * @throws Exception - */ - public function slaveInstance(): PDO - { - if ($this->inTransaction() || empty($this->slaveConfig)) { - return $this->masterInstance(); - } - return $this->connections()->get($this->slaveConfig, false); - } + /** + * @return PDO + * @throws Exception + */ + public function slaveInstance(): PDO + { + if (empty($this->slaveConfig)) { + $this->slaveConfig = ['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password]; + } + return $this->connections()->get($this->slaveConfig, false); + } - /** - * @return \Snowflake\Pool\Connection - * @throws Exception - */ - private function connections(): \Snowflake\Pool\Connection - { - return Snowflake::app()->getMysqlFromPool(); - } + /** + * @return \Snowflake\Pool\Connection + * @throws Exception + */ + private function connections(): \Snowflake\Pool\Connection + { + return Snowflake::app()->getMysqlFromPool(); + } - /** - * @return $this - * @throws Exception - */ - public function beginTransaction(): static - { - $this->connections()->beginTransaction($this->cds); - return $this; - } + /** + * @return $this + * @throws Exception + */ + public function beginTransaction(): static + { + $this->connections()->beginTransaction($this->cds); + return $this; + } - /** - * @return $this|bool - * @throws Exception - */ - public function inTransaction(): bool|static - { - return $this->connections()->inTransaction($this->cds); - } + /** + * @return $this|bool + * @throws Exception + */ + public function inTransaction(): bool|static + { + return $this->connections()->inTransaction($this->cds); + } - /** - * @throws Exception - * 事务回滚 - */ - public function rollback() - { - $this->connections()->rollback($this->cds); - } + /** + * @throws Exception + * 事务回滚 + */ + public function rollback() + { + $this->connections()->rollback($this->cds); + } - /** - * @throws Exception - * 事务提交 - */ - public function commit() - { - $this->connections()->commit($this->cds); - } + /** + * @throws Exception + * 事务提交 + */ + public function commit() + { + $this->connections()->commit($this->cds); + } - /** - * @param $sql - * @return PDO - * @throws Exception - */ - public function refresh($sql): PDO - { - if ($this->isWrite($sql)) { - $instance = $this->masterInstance(); - } else { - $instance = $this->slaveInstance(); - } - return $instance; - } + /** + * @param $sql + * @return PDO + * @throws Exception + */ + public function refresh($sql): PDO + { + if ($this->isWrite($sql)) { + $instance = $this->masterInstance(); + } else { + $instance = $this->slaveInstance(); + } + return $instance; + } - /** - * @param $sql - * @param array $attributes - * @return Command - * @throws - */ - public function createCommand($sql = null, $attributes = []): Command - { - $command = new Command(['db' => $this, 'sql' => $sql]); - return $command->bindValues($attributes); - } + /** + * @param $sql + * @param array $attributes + * @return Command + * @throws + */ + public function createCommand($sql = null, $attributes = []): Command + { + $command = new Command(['db' => $this, 'sql' => $sql]); + return $command->bindValues($attributes); + } - /** - * - * 回收链接 - * @throws - */ - public function release() - { - $connections = $this->connections(); + /** + * + * 回收链接 + * @throws + */ + public function release() + { + $connections = $this->connections(); - $connections->release($this->cds, true); - $connections->release($this->slaveConfig['cds'], false); - } + $connections->release($this->cds, true); + $connections->release($this->slaveConfig['cds'], false); + } - /** - * @throws Exception - */ - public function recovery() - { - $connections = $this->connections(); + /** + * @throws Exception + */ + public function recovery() + { + $connections = $this->connections(); - $connections->release($this->cds, true); - $connections->release($this->slaveConfig['cds'], false); - } + $connections->release($this->cds, true); + $connections->release($this->slaveConfig['cds'], false); + } - /** - * - * 回收链接 - * @throws - */ - public function clear_connection() - { - $connections = $this->connections(); + /** + * + * 回收链接 + * @throws + */ + public function clear_connection() + { + $connections = $this->connections(); - $connections->release($this->cds, true); - $connections->release($this->slaveConfig['cds'], false); - } + $connections->release($this->cds, true); + $connections->release($this->slaveConfig['cds'], false); + } - /** - * @throws Exception - */ - public function disconnect() - { - $connections = $this->connections(); - $connections->disconnect($this->cds, true); - $connections->disconnect($this->slaveConfig['cds'], false); - } + /** + * @throws Exception + */ + public function disconnect() + { + $connections = $this->connections(); + $connections->disconnect($this->cds, true); + $connections->disconnect($this->slaveConfig['cds'], false); + } } diff --git a/Database/Pagination.php b/Database/Pagination.php index 7f1b65ee..e7050529 100644 --- a/Database/Pagination.php +++ b/Database/Pagination.php @@ -17,186 +17,186 @@ use Swoole\Coroutine; class Pagination extends Component { - /** @var ActiveQuery */ - private ActiveQuery $activeQuery; + /** @var ActiveQuery */ + private ActiveQuery $activeQuery; - /** @var int 从第几个开始查 */ - private int $_offset = 0; + /** @var int 从第几个开始查 */ + private int $_offset = 0; - /** @var int 每页数量 */ - private int $_limit = 100; + /** @var int 每页数量 */ + private int $_limit = 100; - /** @var int 最大查询数量 */ - private int $_max = 0; + /** @var int 最大查询数量 */ + private int $_max = 0; - /** @var int 当前已查询数量 */ - private int $_length = 0; + /** @var int 当前已查询数量 */ + private int $_length = 0; - /** @var Closure */ - private Closure $_callback; + /** @var Closure */ + private Closure $_callback; - /** - * PaginationIteration constructor. - * @param ActiveQuery $activeQuery - * @param array $config - * @throws Exception - */ - public function __construct(ActiveQuery $activeQuery, array $config = []) - { - parent::__construct($config); - $this->activeQuery = $activeQuery; - } + /** + * PaginationIteration constructor. + * @param ActiveQuery $activeQuery + * @param array $config + * @throws Exception + */ + public function __construct(ActiveQuery $activeQuery, array $config = []) + { + parent::__construct($config); + $this->activeQuery = $activeQuery; + } - public function clean() - { - unset($this->activeQuery, $this->_callback, $this->_group); - $this->_offset = 0; - $this->_limit = 100; - $this->_max = 0; - $this->_length = 0;; - } + public function clean() + { + unset($this->activeQuery, $this->_callback, $this->_group); + $this->_offset = 0; + $this->_limit = 100; + $this->_max = 0; + $this->_length = 0;; + } - /** - * recover class by clone - */ - public function __clone() - { - $this->clean(); - } + /** + * recover class by clone + */ + public function __clone() + { + $this->clean(); + } - /** - * @param array|Closure $callback - * @throws Exception - */ - public function setCallback(array|Closure $callback) - { - if (!is_callable($callback, true)) { - throw new Exception('非法回调函数~'); - } - $this->_callback = $callback; - } + /** + * @param array|Closure $callback + * @throws Exception + */ + public function setCallback(array|Closure $callback) + { + if (!is_callable($callback, true)) { + throw new Exception('非法回调函数~'); + } + $this->_callback = $callback; + } - /** - * @param int $number - * @return Pagination - */ - public function setOffset(int $number): static - { - if ($number < 0) { - $number = 0; - } - $this->_offset = $number; - return $this; - } + /** + * @param int $number + * @return Pagination + */ + public function setOffset(int $number): static + { + if ($number < 0) { + $number = 0; + } + $this->_offset = $number; + return $this; + } - /** - * @param int $number - * @return Pagination - */ - public function setLimit(int $number): static - { - if ($number < 1) { - $number = 100; - } else if ($number > 5000) { - $number = 5000; - } - $this->_limit = $number; - return $this; - } + /** + * @param int $number + * @return Pagination + */ + public function setLimit(int $number): static + { + if ($number < 1) { + $number = 100; + } else if ($number > 5000) { + $number = 5000; + } + $this->_limit = $number; + return $this; + } - /** - * @param int $number - * @return Pagination - */ - public function setMax(int $number): static - { - if ($number < 0) { - return $this; - } - $this->_max = $number; - return $this; - } + /** + * @param int $number + * @return Pagination + */ + public function setMax(int $number): static + { + if ($number < 0) { + return $this; + } + $this->_max = $number; + return $this; + } - /** - * @param array $param - * @return void - * @throws Exception - */ - public function plunk($param = []) - { - $this->loop($param); - } + /** + * @param array $param + * @return void + * @throws Exception + */ + public function plunk($param = []) + { + $this->loop($param); + } - /** - * 轮训 - * @param $param - * @return array - * @throws Exception - */ - public function loop($param): array - { - if ($this->_max > 0 && $this->_length >= $this->_max) { - return $this->output(); - } - [$length, $data] = $this->get(); + /** + * 轮训 + * @param $param + * @return array + * @throws Exception + */ + public function loop($param): array + { + if ($this->_max > 0 && $this->_length >= $this->_max) { + return $this->output(); + } + [$length, $data] = $this->get(); - $this->executed($data, $param); + $this->executed($data, $param); - unset($data); - if ($length < $this->_limit) { - return $this->output(); - } - return $this->loop($param); - } + unset($data); + if ($length < $this->_limit) { + return $this->output(); + } + return $this->loop($param); + } - /** - * @return array - */ - public function output(): array - { - return []; - } + /** + * @return array + */ + public function output(): array + { + return []; + } - /** - * @param $data - * @param $param - * @throws Exception - */ - private function executed($data, $param): void - { - try { - call_user_func($this->_callback, $data, $param); - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); - } finally { - fire(Event::SYSTEM_RESOURCE_RELEASES); - } - } + /** + * @param $data + * @param $param + * @throws Exception + */ + private function executed($data, $param): void + { + try { + call_user_func($this->_callback, $data, $param); + } catch (\Throwable $exception) { + $this->addError($exception, 'throwable'); + } finally { + logger()->insert(); + } + } - /** - * @return array|Collection - */ - private function get(): Collection|array - { - if ($this->_length + $this->_limit > $this->_max) { - $this->_limit = $this->_length + $this->_limit - $this->_max; - } - $data = $this->activeQuery->limit($this->_offset, $this->_limit)->get(); - $this->_offset += $this->_limit; - $this->_length += $data->size(); - return [$data->size(), $data]; - } + /** + * @return array|Collection + */ + private function get(): Collection|array + { + if ($this->_length + $this->_limit > $this->_max) { + $this->_limit = $this->_length + $this->_limit - $this->_max; + } + $data = $this->activeQuery->limit($this->_offset, $this->_limit)->get(); + $this->_offset += $this->_limit; + $this->_length += $data->size(); + return [$data->size(), $data]; + } } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index fb88f7ce..23911d3a 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -30,507 +30,507 @@ use function Input; class Node extends HttpService { - public string $path = ''; - public int $index = 0; - public string $method = ''; - - /** @var Node[] $childes */ - public array $childes = []; - - public array $group = []; - - private string $_error = ''; - - private string $_dataType = ''; - - /** @var ?Closure|?array */ - public Closure|array|null $handler; - public string $htmlSuffix = '.html'; - public bool $enableHtmlSuffix = false; - public array $namespace = []; - public array $middleware = []; - - /** @var array|Closure */ - public Closure|array $callback = []; - - private string $_alias = ''; - - private array $_interceptors = []; - private array $_after = []; - private array $_limits = []; - - - /** - * @param string $dataType - */ - public function setDataType(string $dataType) - { - $this->_dataType = $dataType; - } - - - /** - * @param string $data - * @return mixed - */ - public function unpack(string $data): mixed - { - if ($this->_dataType == RpcProducer::PROTOCOL_JSON) { - return json_decode($data, true); - } - if ($this->_dataType == RpcProducer::PROTOCOL_SERIALIZE) { - return unserialize($data); - } - return $data; - } - - - /** - * @param $handler - * @return Node - * @throws - */ - public function bindHandler($handler): static - { - if (is_string($handler) && str_contains($handler, '@')) { - list($controller, $action) = explode('@', $handler); - if (!empty($this->namespace)) { - $controller = implode('\\', $this->namespace) . '\\' . $controller; - } - $this->handler = $this->getReflect($controller, $action); - } else if ($handler != null && !is_callable($handler, true)) { - $this->_error = 'Controller is con\'t exec.'; - } else if ($handler instanceof Closure) { - $this->handler = $handler; - } else { - [$controller, $action] = $this->handler = $handler; - if (!($controller instanceof Controller)) { - return $this; - } - $this->annotationInject($controller::class, $action); - } - if (!empty($this->handler)) { - $this->callback = Reduce::reduce($this->createDispatch(), $this->annotation()); - } - return $this; - } - - - /** - * @return Closure - */ - public function createDispatch(): Closure - { - return function () { - $dispatchParam = Context::getContext('dispatch-param'); - if (empty($dispatchParam)) { - $dispatchParam = [\request()]; - } - if ($this->handler instanceof Closure) { - return call_user_func($this->handler, ...$dispatchParam); - } - return \aop($this->handler, $dispatchParam); - }; - } - - - private bool $_hasBeforeAction = false; - - - /** - * @return bool - * @throws Exception - */ - public function beforeAction(): bool - { - if (!$this->_hasBeforeAction) return true; - [$controller, $action] = $this->handler; - if (!$controller->beforeAction(\request())) { - return false; - } - return true; - } - - - /** - * @return array - */ - protected function annotation(): array - { - $middleWares = $this->getMiddleWares(); - $middleWares = $this->annotation_limit($this, $middleWares); - $middleWares = $this->annotation_interceptor($this, $middleWares); - return $middleWares; - } - - - /** - * @param Node $node - * @param $middleWares - * @return array - */ - protected function annotation_interceptor(Node $node, $middleWares = []): array - { - if (!$node->hasInterceptor()) { - return $middleWares; - } - foreach ($node->getInterceptor() as $item) { - $middleWares[] = $item; - } - return $middleWares; - } - - - /** - * @param Node $node - * @param $middleWares - * @return array - */ - protected function annotation_limit(Node $node, $middleWares = []): array - { - if (!$node->hasLimits()) { - return $middleWares; - } - foreach ($node->getLimits() as $item) { - $middleWares[] = $item; - } - return $middleWares; - } - - - /** - * @return bool - */ - #[Pure] public function hasInterceptor(): bool - { - return count($this->_interceptors) > 0; - } - - - /** - * @return bool - */ - #[Pure] public function hasLimits(): bool - { - return count($this->_limits) > 0; - } - - - /** - * @param null $response - * @return mixed - * @throws Exception - */ - public function afterDispatch($response = null): mixed - { - if (is_object($this->_after[0])) { - return call_user_func($this->_after, \request(), $response); - } - foreach ($this->_after as $value) { - call_user_func($value, \request(), $response); - } - return $this->_after; - } - - - /** - * @return array - */ - public function getInterceptor(): array - { - return $this->_interceptors; - } - - - /** - * @return array - */ - public function getAfters(): array - { - return $this->_after; - } - - - /** - * @return bool - */ - #[Pure] public function hasAfter(): bool - { - return count($this->_after) > 0; - } - - - /** - * @return array - */ - public function getLimits(): array - { - return $this->_limits; - } - - /** - * @param $request - * @return bool - */ - public function methodAllow(Request $request): bool - { - if ($this->method == $request->getMethod()) { - return true; - } - return $this->method == 'any'; - } - - /** - * @return bool - * @throws Exception - */ - public function checkSuffix(): bool - { - if ($this->enableHtmlSuffix) { - $url = request()->getUri(); - $nowLength = strlen($this->htmlSuffix); - if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) { - return false; - } - } - return true; - } - - - /** - * @param string $controller - * @param string $action - * @return null|array - * @throws Exception - */ - private function getReflect(string $controller, string $action): ?array - { - try { - $reflect = Snowflake::getDi()->getReflect($controller); - if (empty($reflect)) { - throw new Exception($controller . ' Class is con\'t Instantiable.'); - } - if (!empty($action) && !$reflect->hasMethod($action)) { - throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); - } - - $this->_hasBeforeAction = $reflect->hasMethod('beforeAction'); - - $this->annotationInject($reflect->getName(), $action); - - return [$reflect->newInstance(), $action]; - } catch (Throwable $exception) { - $this->_error = $exception->getMessage(); - $this->addError($exception, 'router'); - return null; - } - } - - - /** - * @param Closure|array|string $handler - * @throws Exception - */ - public function addInterceptor(Closure|string|array $handler) - { - if (!is_array($handler) || is_object($handler[0])) { - $handler = [$handler]; - } - foreach ($handler as $closure) { - if (in_array($closure, $this->_interceptors)) { - continue; - } - $this->_interceptors[] = $closure; - } - } - - - /** - * @param string $className - * @param string $action - * @return Node - * @throws Exception - */ - public function annotationInject(string $className, string $action): Node - { - $annotation = annotation()->getMethods($className, $action); - if (empty($annotation)) { - return $this; - } - foreach ($annotation as $attribute) { - if ($attribute instanceof Interceptor) { - $this->addInterceptor($attribute->interceptor); - } - if ($attribute instanceof After) { - $this->addAfter($attribute->after); - } - if ($attribute instanceof Middleware) { - $this->addMiddleware($attribute->middleware); - } - if ($attribute instanceof Limits) { - $this->addLimits($attribute->limits); - } - } - return $this; - } - - - /** - * @param Closure|array|string $handler - * @throws Exception - */ - public function addAfter(Closure|string|array $handler) - { - if (!is_array($handler) || is_object($handler[0])) { - $handler = [$handler]; - } - foreach ($handler as $closure) { - if (in_array($closure, $this->_after)) { - continue; - } - $this->_after[] = $closure; - } - } - - - /** - * @param Closure|array|string $handler - * @throws Exception - */ - public function addLimits(Closure|string|array $handler) - { - if (!is_array($handler) || is_object($handler[0])) { - $handler = [$handler]; - } - foreach ($handler as $closure) { - if (in_array($closure, $this->_limits)) { - continue; - } - $this->_limits[] = $closure; - } - } - - - /** - * @return string - * 错误信息 - */ - public function getError(): string - { - return $this->_error; - } - - /** - * @param Node $node - * @param string $field - * @return Node - */ - public function addChild(Node $node, string $field): Node - { - $field = (string)$field; - /** @var Node $oLod */ - $oLod = $this->childes[$field] ?? null; - if (!empty($oLod)) { - $node = $oLod; - } - $this->childes[$field] = $node; - return $this->childes[$field]; - } - - - /** - * @param string $search - * @return Node|null - * @throws Exception - */ - public function findNode(string $search): ?Node - { - if (empty($this->childes)) { - return null; - } - - if (isset($this->childes[$search])) { - return $this->childes[$search]; - } - - $_searchMatch = '/<(\w+)?:(.+)?>/'; - foreach ($this->childes as $key => $val) { - if (preg_match($_searchMatch, (string)$key, $match)) { - Input()->addGetParam($match[1] ?? '--', $search); - return $this->childes[$key]; - } - } - return null; - } - - - /** - * @param string $alias - * @return $this - * 别称 - */ - public function alias(string $alias): static - { - $this->_alias = $alias; - return $this; - } - - - /** - * @return string - */ - public function getAlias(): string - { - return $this->_alias; - } - - - /** - * @param Closure|array $class - * @return $this - */ - public function addMiddleware(Closure|array $class): static - { - if (empty($class)) return $this; - foreach ($class as $closure) { - if (in_array($closure, $this->middleware)) { - continue; - } - $this->middleware[] = $closure; - } - return $this; - } - - - /** - * @return array - */ - public function getMiddleWares(): array - { - return $this->middleware; - } - - - /** - * @return mixed - * @throws Exception - */ - public function dispatch(): mixed - { - Context::setContext('dispatch-param', func_get_args()); - if (empty($this->callback)) { - return Json::to(404, $this->errorMsg()); - } - return call_user_func($this->callback, \request()); - } - - - /** - * @return string - */ - private function errorMsg(): string - { - return $this->_error ?? 'Page not found.'; - } + public string $path = ''; + public int $index = 0; + public string $method = ''; + + /** @var Node[] $childes */ + public array $childes = []; + + public array $group = []; + + private string $_error = ''; + + private string $_dataType = ''; + + /** @var ?Closure|?array */ + public Closure|array|null $handler; + public string $htmlSuffix = '.html'; + public bool $enableHtmlSuffix = false; + public array $namespace = []; + public array $middleware = []; + + /** @var array|Closure */ + public Closure|array $callback = []; + + private string $_alias = ''; + + private array $_interceptors = []; + private array $_after = []; + private array $_limits = []; + + + /** + * @param string $dataType + */ + public function setDataType(string $dataType) + { + $this->_dataType = $dataType; + } + + + /** + * @param string $data + * @return mixed + */ + public function unpack(string $data): mixed + { + if ($this->_dataType == RpcProducer::PROTOCOL_JSON) { + return json_decode($data, true); + } + if ($this->_dataType == RpcProducer::PROTOCOL_SERIALIZE) { + return unserialize($data); + } + return $data; + } + + + /** + * @param $handler + * @return Node + * @throws + */ + public function bindHandler($handler): static + { + if (is_string($handler) && str_contains($handler, '@')) { + list($controller, $action) = explode('@', $handler); + if (!empty($this->namespace)) { + $controller = implode('\\', $this->namespace) . '\\' . $controller; + } + $this->handler = $this->getReflect($controller, $action); + } else if ($handler != null && !is_callable($handler, true)) { + $this->_error = 'Controller is con\'t exec.'; + } else if ($handler instanceof Closure) { + $this->handler = $handler; + } else { + [$controller, $action] = $this->handler = $handler; + if (!($controller instanceof Controller)) { + return $this; + } + $this->annotationInject($controller::class, $action); + } + if (!empty($this->handler)) { + $this->callback = Reduce::reduce($this->createDispatch(), $this->annotation()); + } + return $this; + } + + + /** + * @return Closure + */ + public function createDispatch(): Closure + { + return function () { + $dispatchParam = Context::getContext('dispatch-param'); + if (empty($dispatchParam)) { + $dispatchParam = [\request()]; + } + if ($this->handler instanceof Closure) { + return call_user_func($this->handler, ...$dispatchParam); + } + return \aop($this->handler, $dispatchParam); + }; + } + + + private bool $_hasBeforeAction = false; + + + /** + * @return bool + * @throws Exception + */ + public function beforeAction(): bool + { + if (!$this->_hasBeforeAction) return true; + [$controller, $action] = $this->handler; + if (!$controller->beforeAction(\request())) { + return false; + } + return true; + } + + + /** + * @return array + */ + protected function annotation(): array + { + $middleWares = $this->getMiddleWares(); + $middleWares = $this->annotation_limit($this, $middleWares); + $middleWares = $this->annotation_interceptor($this, $middleWares); + return $middleWares; + } + + + /** + * @param Node $node + * @param $middleWares + * @return array + */ + protected function annotation_interceptor(Node $node, $middleWares = []): array + { + if (!$node->hasInterceptor()) { + return $middleWares; + } + foreach ($node->getInterceptor() as $item) { + $middleWares[] = $item; + } + return $middleWares; + } + + + /** + * @param Node $node + * @param $middleWares + * @return array + */ + protected function annotation_limit(Node $node, $middleWares = []): array + { + if (!$node->hasLimits()) { + return $middleWares; + } + foreach ($node->getLimits() as $item) { + $middleWares[] = $item; + } + return $middleWares; + } + + + /** + * @return bool + */ + #[Pure] public function hasInterceptor(): bool + { + return count($this->_interceptors) > 0; + } + + + /** + * @return bool + */ + #[Pure] public function hasLimits(): bool + { + return count($this->_limits) > 0; + } + + + /** + * @param null $response + * @return mixed + * @throws Exception + */ + public function afterDispatch($response = null): mixed + { + if (is_object($this->_after[0])) { + return call_user_func($this->_after, \request(), $response); + } + foreach ($this->_after as $value) { + call_user_func($value, \request(), $response); + } + return $this->_after; + } + + + /** + * @return array + */ + public function getInterceptor(): array + { + return $this->_interceptors; + } + + + /** + * @return array + */ + public function getAfters(): array + { + return $this->_after; + } + + + /** + * @return bool + */ + #[Pure] public function hasAfter(): bool + { + return count($this->_after) > 0; + } + + + /** + * @return array + */ + public function getLimits(): array + { + return $this->_limits; + } + + /** + * @param $request + * @return bool + */ + public function methodAllow(Request $request): bool + { + if ($this->method == $request->getMethod()) { + return true; + } + return $this->method == 'any'; + } + + /** + * @return bool + * @throws Exception + */ + public function checkSuffix(): bool + { + if ($this->enableHtmlSuffix) { + $url = request()->getUri(); + $nowLength = strlen($this->htmlSuffix); + if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) { + return false; + } + } + return true; + } + + + /** + * @param string $controller + * @param string $action + * @return null|array + * @throws Exception + */ + private function getReflect(string $controller, string $action): ?array + { + try { + $reflect = Snowflake::getDi()->getReflect($controller); + if (empty($reflect)) { + throw new Exception($controller . ' Class is con\'t Instantiable.'); + } + if (!empty($action) && !$reflect->hasMethod($action)) { + throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); + } + + $this->_hasBeforeAction = $reflect->hasMethod('beforeAction'); + + $this->annotationInject($reflect->getName(), $action); + + return [$reflect->newInstance(), $action]; + } catch (Throwable $exception) { + $this->_error = $exception->getMessage(); + $this->addError($exception, 'router'); + return null; + } + } + + + /** + * @param Closure|array|string $handler + * @throws Exception + */ + public function addInterceptor(Closure|string|array $handler) + { + if (!is_array($handler) || is_object($handler[0])) { + $handler = [$handler]; + } + foreach ($handler as $closure) { + if (in_array($closure, $this->_interceptors)) { + continue; + } + $this->_interceptors[] = $closure; + } + } + + + /** + * @param string $className + * @param string $action + * @return Node + * @throws Exception + */ + public function annotationInject(string $className, string $action): Node + { + $annotation = annotation()->getMethods($className, $action); + if (empty($annotation)) { + return $this; + } + foreach ($annotation as $attribute) { + if ($attribute instanceof Interceptor) { + $this->addInterceptor($attribute->interceptor); + } + if ($attribute instanceof After) { + $this->addAfter($attribute->after); + } + if ($attribute instanceof Middleware) { + $this->addMiddleware($attribute->middleware); + } + if ($attribute instanceof Limits) { + $this->addLimits($attribute->limits); + } + } + return $this; + } + + + /** + * @param Closure|array|string $handler + * @throws Exception + */ + public function addAfter(Closure|string|array $handler) + { + if (!is_array($handler) || is_object($handler[0])) { + $handler = [$handler]; + } + foreach ($handler as $closure) { + if (in_array($closure, $this->_after)) { + continue; + } + $this->_after[] = $closure; + } + } + + + /** + * @param Closure|array|string $handler + * @throws Exception + */ + public function addLimits(Closure|string|array $handler) + { + if (!is_array($handler) || is_object($handler[0])) { + $handler = [$handler]; + } + foreach ($handler as $closure) { + if (in_array($closure, $this->_limits)) { + continue; + } + $this->_limits[] = $closure; + } + } + + + /** + * @return string + * 错误信息 + */ + public function getError(): string + { + return $this->_error; + } + + /** + * @param Node $node + * @param string $field + * @return Node + */ + public function addChild(Node $node, string $field): Node + { + $field = (string)$field; + /** @var Node $oLod */ + $oLod = $this->childes[$field] ?? null; + if (!empty($oLod)) { + $node = $oLod; + } + $this->childes[$field] = $node; + return $this->childes[$field]; + } + + + /** + * @param string $search + * @return Node|null + * @throws Exception + */ + public function findNode(string $search): ?Node + { + if (empty($this->childes)) { + return null; + } + + if (isset($this->childes[$search])) { + return $this->childes[$search]; + } + + $_searchMatch = '/<(\w+)?:(.+)?>/'; + foreach ($this->childes as $key => $val) { + if (preg_match($_searchMatch, (string)$key, $match)) { + Input()->addGetParam($match[1] ?? '--', $search); + return $this->childes[$key]; + } + } + return null; + } + + + /** + * @param string $alias + * @return $this + * 别称 + */ + public function alias(string $alias): static + { + $this->_alias = $alias; + return $this; + } + + + /** + * @return string + */ + public function getAlias(): string + { + return $this->_alias; + } + + + /** + * @param Closure|array $class + * @return $this + */ + public function addMiddleware(Closure|array $class): static + { + if (empty($class)) return $this; + foreach ($class as $closure) { + if (in_array($closure, $this->middleware)) { + continue; + } + $this->middleware[] = $closure; + } + return $this; + } + + + /** + * @return array + */ + public function getMiddleWares(): array + { + return $this->middleware; + } + + + /** + * @return mixed + * @throws Exception + */ + public function dispatch(): mixed + { + Context::setContext('dispatch-param', func_get_args()); + if (empty($this->callback)) { + return Json::to(404, $this->errorMsg()); + } + return call_user_func($this->callback, \request()); + } + + + /** + * @return string + */ + private function errorMsg(): string + { + return $this->_error ?? 'Page not found.'; + } } diff --git a/System/Async.php b/System/Async.php index b95c5d6c..e926f416 100644 --- a/System/Async.php +++ b/System/Async.php @@ -26,7 +26,7 @@ class Async extends Component */ public function addAsync(string $name, Task $handler) { - $this->_absences[$name] = $handler; + $this->_absences[$name] = $handler::class; } @@ -47,7 +47,7 @@ class Async extends Component } /** @var Task $class */ - $class = $this->_absences[$name]; + $class = new $this->_absences[$name](); $class->setParams($params); $randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1); diff --git a/System/Di/Container.php b/System/Di/Container.php index c6223688..b26c4a93 100644 --- a/System/Di/Container.php +++ b/System/Di/Container.php @@ -185,7 +185,7 @@ class Container extends BaseObject } foreach ($this->_property[$reflect->getName()] as $property => $inject) { /** @var Inject $inject */ - $inject->execute([$object, $property]); + $inject->execute($object, $property); } return $object; }