Merge branch 'master' of https://gitee.com/dreamwithouttrace/snowflake into v1.0
This commit is contained in:
+7
-10
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Annotation;
|
||||
|
||||
|
||||
class FileTree
|
||||
{
|
||||
|
||||
private array $files = [];
|
||||
|
||||
|
||||
private array $childes = [];
|
||||
|
||||
|
||||
private string $_filePath = '';
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @return $this|null
|
||||
*/
|
||||
public function getChild($path): ?static
|
||||
{
|
||||
if (!isset($this->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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
+46
-46
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+105
-49
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+15
-16
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+18
-20
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-10
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-4
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
+257
-215
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+248
-248
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+150
-150
@@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+502
-502
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user