Merge branch 'master' of https://gitee.com/dreamwithouttrace/snowflake into v1.0
This commit is contained in:
@@ -28,12 +28,9 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $handler
|
|
||||||
* @return mixed
|
public function execute(mixed $class, mixed $method = ''): mixed
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function execute(array $handler): mixed
|
|
||||||
{
|
{
|
||||||
// TODO: Change the autogenerated stub
|
// TODO: Change the autogenerated stub
|
||||||
if (!in_array(IAspect::class, class_implements($this->aspect))) {
|
if (!in_array(IAspect::class, class_implements($this->aspect))) {
|
||||||
@@ -42,9 +39,9 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
|
|||||||
/** @var Aop $aop */
|
/** @var Aop $aop */
|
||||||
$aop = Snowflake::app()->get('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 Exception;
|
||||||
|
use HttpServer\IInterface\Task;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
|
|
||||||
@@ -32,10 +33,10 @@ use Snowflake\Snowflake;
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): bool
|
public function execute(mixed $class, mixed $method = null): mixed
|
||||||
{
|
{
|
||||||
$async = Snowflake::app()->getAsync();
|
$async = Snowflake::app()->getAsync();
|
||||||
$async->addAsync($this->name, current($handler));
|
$async->addAsync($this->name, $class);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,14 +22,11 @@ abstract class Attribute implements IAnnotation
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $handler
|
public function execute(mixed $class, mixed $method = ''): mixed
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function execute(array $handler): mixed
|
|
||||||
{
|
{
|
||||||
return $this;
|
// TODO: Implement execute() method.
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,10 +33,10 @@ use Snowflake\Event as SEvent;
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): bool
|
public function execute(mixed $class, mixed $method = null): mixed
|
||||||
{
|
{
|
||||||
// TODO: Implement execute() method.
|
// TODO: Implement execute() method.
|
||||||
SEvent::on($this->name, $handler, $this->params);
|
SEvent::on($this->name, [$class, $method], $this->params);
|
||||||
return true;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -13,7 +13,7 @@ interface IAnnotation
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): mixed;
|
public function execute(mixed $class, mixed $method = ''): mixed;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-10
@@ -31,24 +31,24 @@ use Snowflake\Snowflake;
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): mixed
|
public function execute(mixed $class, mixed $method = null): mixed
|
||||||
{
|
{
|
||||||
$injectValue = $this->parseInjectValue();
|
$injectValue = $this->parseInjectValue();
|
||||||
if (!($handler[1] instanceof ReflectionProperty)) {
|
if (!($method instanceof ReflectionProperty)) {
|
||||||
$handler[1] = new ReflectionProperty($handler[0], $handler[1]);
|
$method = new ReflectionProperty($class, $method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var ReflectionProperty $handler [1] */
|
/** @var ReflectionProperty $class */
|
||||||
if ($handler[1]->isPrivate() || $handler[1]->isProtected()) {
|
if ($method->isPrivate() || $method->isProtected()) {
|
||||||
$method = 'set' . ucfirst($handler[1]->getName());
|
$method = 'set' . ucfirst($class->getName());
|
||||||
if (!method_exists($handler[0], $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$handler[0]->$method($injectValue);
|
$class->$method($injectValue);
|
||||||
} else {
|
} else {
|
||||||
$handler[0]->{$handler[1]->getName()} = $injectValue;
|
$class->{$method->getName()} = $injectValue;
|
||||||
}
|
}
|
||||||
return $handler[0];
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,17 +30,17 @@ use Snowflake\Snowflake;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return mixed
|
* @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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var TaskContainer $container */
|
/** @var TaskContainer $container */
|
||||||
$container = Snowflake::app()->get('kafka-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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+79
-23
@@ -33,9 +33,6 @@ class Loader extends BaseObject
|
|||||||
private array $_directory = [];
|
private array $_directory = [];
|
||||||
|
|
||||||
|
|
||||||
private FileTree $files;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -44,13 +41,6 @@ class Loader extends BaseObject
|
|||||||
return $this->_directory;
|
return $this->_directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function init()
|
|
||||||
{
|
|
||||||
$this->files = new FileTree();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param $namespace
|
* @param $namespace
|
||||||
@@ -102,7 +92,7 @@ class Loader extends BaseObject
|
|||||||
}
|
}
|
||||||
foreach ($properties as $property => $attributes) {
|
foreach ($properties as $property => $attributes) {
|
||||||
foreach ($attributes as $attribute) {
|
foreach ($attributes as $attribute) {
|
||||||
$attribute->execute([$handler, $property]);
|
$attribute->execute($handler, $property);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
@@ -173,13 +163,50 @@ class Loader extends BaseObject
|
|||||||
if ($path->getExtension() !== 'php') {
|
if ($path->getExtension() !== 'php') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$replace = Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
|
$replace = $this->getReflect($path, $namespace);
|
||||||
if (empty($replace) || count($replace->getAttributes(Target::class)) < 1) {
|
if (empty($replace) || count($replace->getAttributes(Target::class)) < 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
|
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
|
||||||
|
|
||||||
$_array = ['handler' => $replace->newInstance(), 'target' => [], 'methods' => [], 'property' => []];
|
$_array['handler'] = $replace->getName();
|
||||||
|
$_array['target'] = [];
|
||||||
|
$_array['methods'] = [];
|
||||||
|
$_array['property'] = [];
|
||||||
|
|
||||||
|
$_array = $this->_targets($replace, $_array);
|
||||||
|
$_array = $this->_methods($replace, $_array);
|
||||||
|
$_array = $this->_properties($replace, $_array);
|
||||||
|
|
||||||
|
$this->_fileMap[$replace->getFileName()] = $replace->getName();
|
||||||
|
|
||||||
|
$this->_classes[$replace->getName()] = $_array;
|
||||||
|
} catch (Throwable $throwable) {
|
||||||
|
$this->addError($throwable, 'throwable');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) {
|
foreach ($replace->getAttributes() as $attribute) {
|
||||||
if ($attribute->getName() == Attribute::class) {
|
if ($attribute->getName() == Attribute::class) {
|
||||||
continue;
|
continue;
|
||||||
@@ -189,7 +216,17 @@ class Loader extends BaseObject
|
|||||||
}
|
}
|
||||||
$_array['target'][] = $attribute->newInstance();
|
$_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);
|
$methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
$_method = [];
|
$_method = [];
|
||||||
@@ -201,7 +238,17 @@ class Loader extends BaseObject
|
|||||||
}
|
}
|
||||||
$_array['methods'][$method->getName()] = $_method;
|
$_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();
|
$methods = $replace->getProperties();
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
$_property = [];
|
$_property = [];
|
||||||
@@ -214,13 +261,7 @@ class Loader extends BaseObject
|
|||||||
}
|
}
|
||||||
$_array['property'][$method->getName()] = $_property;
|
$_array['property'][$method->getName()] = $_property;
|
||||||
}
|
}
|
||||||
|
return $_array;
|
||||||
$this->_fileMap[$replace->getFileName()] = $replace->getName();
|
|
||||||
|
|
||||||
$this->_classes[$replace->getName()] = $_array;
|
|
||||||
} catch (Throwable $throwable) {
|
|
||||||
$this->addError($throwable, 'throwable');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -301,14 +342,17 @@ class Loader extends BaseObject
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$annotation = Snowflake::getAnnotation();
|
$annotation = Snowflake::getAnnotation();
|
||||||
|
|
||||||
foreach ($classes as $className) {
|
foreach ($classes as $className) {
|
||||||
$annotations = $this->_classes[$className] ?? null;
|
$annotations = $this->_classes[$className] ?? null;
|
||||||
if ($annotations === null) {
|
if ($annotations === null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$class = clone $annotations['handler'];
|
|
||||||
|
$class = $this->newInstance($annotations['handler']);
|
||||||
|
/** @var \Annotation\Attribute $value */
|
||||||
foreach ($annotations['target'] ?? [] as $value) {
|
foreach ($annotations['target'] ?? [] as $value) {
|
||||||
$value->execute([$class]);
|
$value->execute($class);
|
||||||
}
|
}
|
||||||
foreach ($annotations['methods'] as $name => $attribute) {
|
foreach ($annotations['methods'] as $name => $attribute) {
|
||||||
foreach ($attribute as $value) {
|
foreach ($attribute as $value) {
|
||||||
@@ -319,7 +363,7 @@ class Loader extends BaseObject
|
|||||||
} else if ($value instanceof Set) {
|
} else if ($value instanceof Set) {
|
||||||
$annotation->addSets($class::class, $value->name, $name);
|
$annotation->addSets($class::class, $value->name, $name);
|
||||||
} else {
|
} 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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,21 +35,20 @@ use Snowflake\Snowflake;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $handler
|
* @param object $class
|
||||||
|
* @param string $method
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): mixed
|
public function execute(mixed $class, mixed $method = null): mixed
|
||||||
{
|
{
|
||||||
$class = ['class' => $handler[0]::class];
|
$class = ['class' => $class::class];
|
||||||
if (!empty($this->args)) {
|
if (!empty($this->args)) {
|
||||||
$class = array_merge($class, $this->args);
|
$class = array_merge($class, $this->args);
|
||||||
}
|
}
|
||||||
|
|
||||||
Snowflake::set($this->service, $class);
|
Snowflake::set($this->service, $class);
|
||||||
|
return true; // TODO: Change the autogenerated stub
|
||||||
return parent::execute($handler); // TODO: Change the autogenerated stub
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace Annotation\Model;
|
|||||||
use Annotation\Annotation;
|
use Annotation\Annotation;
|
||||||
use Attribute;
|
use Attribute;
|
||||||
use Database\ActiveRecord;
|
use Database\ActiveRecord;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,9 +22,7 @@ use Database\ActiveRecord;
|
|||||||
* Get constructor.
|
* Get constructor.
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(public string $name)
|
||||||
public string $name
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,12 +31,11 @@ use Database\ActiveRecord;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return ActiveRecord
|
* @return ActiveRecord
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): ActiveRecord
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
/** @var ActiveRecord $activeRecord */
|
$annotation = Snowflake::getAnnotation();
|
||||||
[$activeRecord, $method] = $handler;
|
$annotation->addGets($class::class, $this->name, $method);
|
||||||
|
return true;
|
||||||
return $activeRecord->addGets($this->name, $method);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace Annotation\Model;
|
|||||||
use Annotation\Attribute;
|
use Annotation\Attribute;
|
||||||
use Database\ActiveRecord;
|
use Database\ActiveRecord;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,13 +31,10 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
/** @var ActiveRecord $activeRecord */
|
$annotation = Snowflake::getAnnotation();
|
||||||
[$activeRecord, $method] = $handler;
|
$annotation->addRelate($class::class, $this->name, $method);
|
||||||
|
|
||||||
$activeRecord->setRelate($this->name, $method);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace Annotation\Model;
|
|||||||
|
|
||||||
use Annotation\Attribute;
|
use Annotation\Attribute;
|
||||||
use Database\ActiveRecord;
|
use Database\ActiveRecord;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
|
||||||
{
|
{
|
||||||
@@ -20,17 +21,17 @@ use Database\ActiveRecord;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $handler
|
* @param mixed $class
|
||||||
* @return ActiveRecord
|
* @param mixed|null $method
|
||||||
|
* @return bool
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): ActiveRecord
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
/** @var ActiveRecord $activeRecord */
|
$annotation = Snowflake::getAnnotation();
|
||||||
[$activeRecord, $method] = $handler;
|
$annotation->addSets($class::class, $this->name, $method);
|
||||||
|
return true;
|
||||||
return $activeRecord->addSets($this->name, $method);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -31,14 +31,14 @@ use Snowflake\Snowflake;
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): mixed
|
public function execute(mixed $class, mixed $method = null): mixed
|
||||||
{
|
{
|
||||||
$router = Snowflake::app()->getRouter();
|
$router = Snowflake::app()->getRouter();
|
||||||
if (!($handler[0] instanceof Porters)) {
|
if (!($class instanceof Porters)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$router->addPortListen($this->port, [$handler[0], 'process']);
|
$router->addPortListen($this->port, [$class, 'process']);
|
||||||
return parent::execute($handler);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ use Snowflake\Snowflake;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return After
|
* @return After
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): static
|
public function execute(mixed $class, mixed $method = null): static
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ use Annotation\Attribute;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): array
|
public function execute(mixed $class, mixed $method = null): array
|
||||||
{
|
{
|
||||||
// TODO: Implement execute() method.
|
// TODO: Implement execute() method.
|
||||||
return [$this->request, $this->response];
|
return [$this->request, $this->response];
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ use Snowflake\Snowflake;
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ use Snowflake\Snowflake;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return Interceptor
|
* @return Interceptor
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): static
|
public function execute(mixed $class, mixed $method = null): static
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ use Snowflake\Snowflake;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return Limits
|
* @return Limits
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): static
|
public function execute(mixed $class, mixed $method = null): static
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ use HttpServer\IInterface\Middleware as IMiddleware;
|
|||||||
* @param array $handler
|
* @param array $handler
|
||||||
* @return Middleware
|
* @return Middleware
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): static
|
public function execute(mixed $class, mixed $method = null): static
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,12 +34,12 @@ use Snowflake\Snowflake;
|
|||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): Router
|
public function execute(mixed $class, mixed $method = null): Router
|
||||||
{
|
{
|
||||||
// TODO: Implement setHandler() method.
|
// TODO: Implement setHandler() method.
|
||||||
$router = Snowflake::app()->getRouter();
|
$router = Snowflake::app()->getRouter();
|
||||||
|
|
||||||
$router->addRoute($this->uri, $handler, $this->method);
|
$router->addRoute($this->uri, [$class, $method], $this->method);
|
||||||
|
|
||||||
return $router;
|
return $router;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ use Snowflake\Snowflake;
|
|||||||
* @return Router
|
* @return Router
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): Router
|
public function execute(mixed $class, mixed $method = null): Router
|
||||||
{
|
{
|
||||||
// TODO: Implement setHandler() method.
|
// TODO: Implement setHandler() method.
|
||||||
$router = Snowflake::app()->getRouter();
|
$router = Snowflake::app()->getRouter();
|
||||||
|
|
||||||
$router->addRoute($this->uri, $handler, Request::HTTP_CMD)
|
$router->addRoute($this->uri, [$class, $method], Request::HTTP_CMD)
|
||||||
->setDataType($this->protocol);
|
->setDataType($this->protocol);
|
||||||
|
|
||||||
return $router;
|
return $router;
|
||||||
|
|||||||
@@ -44,14 +44,14 @@ use Snowflake\Snowflake;
|
|||||||
* @return Router
|
* @return Router
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): Router
|
public function execute(mixed $class, mixed $method = null): Router
|
||||||
{
|
{
|
||||||
// TODO: Implement setHandler() method.
|
// TODO: Implement setHandler() method.
|
||||||
$router = Snowflake::app()->getRouter();
|
$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;
|
return $router;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,11 +32,11 @@ use Snowflake\Snowflake;
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): mixed
|
public function execute(mixed $class, mixed $method = ''): mixed
|
||||||
{
|
{
|
||||||
$rpc = Snowflake::app()->getRpc();
|
$rpc = Snowflake::app()->getRpc();
|
||||||
$rpc->addConsumer($this->cmd, $handler);
|
$rpc->addConsumer($this->cmd, [$class, $method]);
|
||||||
return parent::execute($handler); // TODO: Change the autogenerated stub
|
return true; // TODO: Change the autogenerated stub
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ use Snowflake\Snowflake;
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(array $handler): mixed
|
public function execute(mixed $class, mixed $method = ''): mixed
|
||||||
{
|
{
|
||||||
$rpc = Snowflake::app()->getRpc();
|
$rpc = Snowflake::app()->getRpc();
|
||||||
$rpc->addProducer($this->cmd, $handler, $this->config);
|
$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);
|
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
|
||||||
|
|
||||||
$trance = $dbConnection->beginTransaction();
|
// $trance = $dbConnection->beginTransaction();
|
||||||
try {
|
try {
|
||||||
if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) {
|
if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) {
|
||||||
throw new Exception('保存失败.');
|
throw new Exception('保存失败.');
|
||||||
}
|
}
|
||||||
$trance->commit();
|
// $trance->commit();
|
||||||
$lastId = $this->setPrimary($lastId, $param);
|
$lastId = $this->setPrimary($lastId, $param);
|
||||||
|
|
||||||
$this->refresh();
|
$this->refresh();
|
||||||
|
|
||||||
SEvent::trigger(self::AFTER_SAVE, [$attributes, $param]);
|
SEvent::trigger(self::AFTER_SAVE, [$attributes, $param]);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$trance->rollback();
|
// $trance->rollback();
|
||||||
$lastId = $this->addError($exception, 'mysql');
|
$lastId = $this->addError($exception, 'mysql');
|
||||||
}
|
}
|
||||||
return $lastId;
|
return $lastId;
|
||||||
@@ -498,13 +498,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
return $generate;
|
return $generate;
|
||||||
}
|
}
|
||||||
|
|
||||||
$trance = $command->beginTransaction();
|
// $trance = $command->beginTransaction();
|
||||||
if (!$command->createCommand(...$generate)->save(false, $this)) {
|
if (!$command->createCommand(...$generate)->save(false, $this)) {
|
||||||
$trance->rollback();
|
// $trance->rollback();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$trance->commit();
|
// $trance->commit();
|
||||||
|
|
||||||
$this->refresh();
|
$this->refresh();
|
||||||
|
|
||||||
|
|||||||
+56
-14
@@ -126,6 +126,9 @@ class Command extends Component
|
|||||||
} else {
|
} else {
|
||||||
$result = $this->search($type);
|
$result = $this->search($type);
|
||||||
}
|
}
|
||||||
|
if ($this->prepare) {
|
||||||
|
$this->prepare->closeCursor();
|
||||||
|
}
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
||||||
@@ -140,20 +143,20 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
private function search($type): mixed
|
private function search($type): mixed
|
||||||
{
|
{
|
||||||
$connect = $this->db->getConnect($this->sql);
|
if (($prepare = $this->prepare()) == false) {
|
||||||
if (!($query = $connect?->query($this->sql))) {
|
return false;
|
||||||
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
|
|
||||||
}
|
}
|
||||||
defer(fn() => $query->closeCursor());
|
|
||||||
if ($type === static::FETCH_COLUMN) {
|
if ($type === static::FETCH_COLUMN) {
|
||||||
return $query->fetchAll(PDO::FETCH_ASSOC);
|
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} else if ($type === static::ROW_COUNT) {
|
} else if ($type === static::ROW_COUNT) {
|
||||||
return $query->rowCount();
|
$data = $prepare->rowCount();
|
||||||
} else if ($type === static::FETCH_ALL) {
|
} else if ($type === static::FETCH_ALL) {
|
||||||
return $query->fetchAll(PDO::FETCH_ASSOC);
|
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} else {
|
} else {
|
||||||
return $query->fetch(PDO::FETCH_ASSOC);
|
$data = $prepare->fetch(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
$prepare->closeCursor();
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -165,7 +168,7 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int
|
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int
|
||||||
{
|
{
|
||||||
if (($result = $this->initPDOStatement()) === false) {
|
if (($result = $this->getPdoStatement()) === false) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if ($isInsert === false) {
|
if ($isInsert === false) {
|
||||||
@@ -182,7 +185,7 @@ class Command extends Component
|
|||||||
* 重新构建
|
* 重新构建
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
private function initPDOStatement(): bool|int
|
private function getPdoStatement(): bool|int
|
||||||
{
|
{
|
||||||
if (empty($this->sql)) {
|
if (empty($this->sql)) {
|
||||||
return $this->addError('no sql.', 'mysql');
|
return $this->addError('no sql.', 'mysql');
|
||||||
@@ -191,18 +194,57 @@ class Command extends Component
|
|||||||
return $this->addError('get client error.', 'mysql');
|
return $this->addError('get client error.', 'mysql');
|
||||||
}
|
}
|
||||||
if (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) {
|
if (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) {
|
||||||
$error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
|
return $this->addError($this->errorMessage($prepare), 'mysql');
|
||||||
|
}
|
||||||
|
$result = $this->checkResponse($prepare, $connect);
|
||||||
|
$prepare->closeCursor();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $prepare
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function errorMessage($prepare)
|
||||||
|
{
|
||||||
|
return $this->sql . ':' . ($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 $this->addError($this->sql . ':' . $error, 'mysql');
|
||||||
}
|
}
|
||||||
|
return $prepare;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $prepare
|
||||||
|
* @param $connect
|
||||||
|
* @return bool|int
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
private function checkResponse($prepare, $connect)
|
||||||
|
{
|
||||||
$result = $prepare->execute($this->params);
|
$result = $prepare->execute($this->params);
|
||||||
defer(fn() => $prepare->closeCursor());
|
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
return $this->addError($connect->errorInfo()[2], 'mysql');
|
return $this->addError($connect->errorInfo()[2], 'mysql');
|
||||||
}
|
}
|
||||||
return (int)$connect->lastInsertId();
|
return (int)$connect->lastInsertId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $modelName
|
* @param $modelName
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -242,10 +284,10 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|null $data
|
* @param array $data
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function bindValues(array $data = NULL): static
|
public function bindValues(array $data = []): static
|
||||||
{
|
{
|
||||||
if (!is_array($this->params)) {
|
if (!is_array($this->params)) {
|
||||||
$this->params = [];
|
$this->params = [];
|
||||||
|
|||||||
@@ -185,8 +185,8 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function slaveInstance(): PDO
|
public function slaveInstance(): PDO
|
||||||
{
|
{
|
||||||
if ($this->inTransaction() || empty($this->slaveConfig)) {
|
if (empty($this->slaveConfig)) {
|
||||||
return $this->masterInstance();
|
$this->slaveConfig = ['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password];
|
||||||
}
|
}
|
||||||
return $this->connections()->get($this->slaveConfig, false);
|
return $this->connections()->get($this->slaveConfig, false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ class Pagination extends Component
|
|||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->addError($exception, 'throwable');
|
$this->addError($exception, 'throwable');
|
||||||
} finally {
|
} finally {
|
||||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
logger()->insert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -26,7 +26,7 @@ class Async extends Component
|
|||||||
*/
|
*/
|
||||||
public function addAsync(string $name, Task $handler)
|
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 */
|
/** @var Task $class */
|
||||||
$class = $this->_absences[$name];
|
$class = new $this->_absences[$name]();
|
||||||
$class->setParams($params);
|
$class->setParams($params);
|
||||||
|
|
||||||
$randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);
|
$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) {
|
foreach ($this->_property[$reflect->getName()] as $property => $inject) {
|
||||||
/** @var Inject $inject */
|
/** @var Inject $inject */
|
||||||
$inject->execute([$object, $property]);
|
$inject->execute($object, $property);
|
||||||
}
|
}
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user