This commit is contained in:
as2252258@163.com
2021-05-03 04:22:47 +08:00
32 changed files with 1440 additions and 1431 deletions
+5 -8
View File
@@ -28,12 +28,9 @@ 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))) {
@@ -42,9 +39,9 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
/** @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;
}
+3 -2
View File
@@ -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;
}
+4 -7
View File
@@ -22,14 +22,11 @@ abstract class Attribute implements IAnnotation
{
/**
* @param array $handler
* @return mixed
*/
public function execute(array $handler): mixed
public function execute(mixed $class, mixed $method = ''): mixed
{
return $this;
// TODO: Implement execute() method.
return true;
}
}
+2 -2
View File
@@ -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;
}
-80
View File
@@ -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;
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ interface IAnnotation
* @param array $handler
* @return mixed
*/
public function execute(array $handler): mixed;
public function execute(mixed $class, mixed $method = ''): mixed;
}
+10 -10
View File
@@ -31,24 +31,24 @@ use Snowflake\Snowflake;
* @return mixed
* @throws Exception
*/
public function execute(array $handler): mixed
public function execute(mixed $class, mixed $method = null): mixed
{
$injectValue = $this->parseInjectValue();
if (!($handler[1] instanceof ReflectionProperty)) {
$handler[1] = new ReflectionProperty($handler[0], $handler[1]);
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)) {
/** @var ReflectionProperty $class */
if ($method->isPrivate() || $method->isProtected()) {
$method = 'set' . ucfirst($class->getName());
if (!method_exists($class, $method)) {
return false;
}
$handler[0]->$method($injectValue);
$class->$method($injectValue);
} else {
$handler[0]->{$handler[1]->getName()} = $injectValue;
$class->{$method->getName()} = $injectValue;
}
return $handler[0];
return true;
}
+4 -4
View File
@@ -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;
}
+79 -23
View File
@@ -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,13 +163,50 @@ 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' => []];
$_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) {
if ($attribute->getName() == Attribute::class) {
continue;
@@ -189,7 +216,17 @@ class Loader extends BaseObject
}
$_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 = [];
@@ -201,7 +238,17 @@ class Loader extends BaseObject
}
$_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 = [];
@@ -214,13 +261,7 @@ class Loader extends BaseObject
}
$_array['property'][$method->getName()] = $_property;
}
$this->_fileMap[$replace->getFileName()] = $replace->getName();
$this->_classes[$replace->getName()] = $_array;
} catch (Throwable $throwable) {
$this->addError($throwable, 'throwable');
}
return $_array;
}
@@ -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();
}
}
+6 -7
View File
@@ -35,21 +35,20 @@ use Snowflake\Snowflake;
/**
* @param array $handler
* @param object $class
* @param string $method
* @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)) {
$class = array_merge($class, $this->args);
}
Snowflake::set($this->service, $class);
return parent::execute($handler); // TODO: Change the autogenerated stub
return true; // TODO: Change the autogenerated stub
}
}
+6 -8
View File
@@ -7,6 +7,7 @@ namespace Annotation\Model;
use Annotation\Annotation;
use Attribute;
use Database\ActiveRecord;
use Snowflake\Snowflake;
/**
@@ -21,9 +22,7 @@ use Database\ActiveRecord;
* Get constructor.
* @param string $name
*/
public function __construct(
public string $name
)
public function __construct(public string $name)
{
}
@@ -32,12 +31,11 @@ use Database\ActiveRecord;
* @param array $handler
* @return ActiveRecord
*/
public function execute(array $handler): ActiveRecord
public function execute(mixed $class, mixed $method = null): bool
{
/** @var ActiveRecord $activeRecord */
[$activeRecord, $method] = $handler;
return $activeRecord->addGets($this->name, $method);
$annotation = Snowflake::getAnnotation();
$annotation->addGets($class::class, $this->name, $method);
return true;
}
+4 -6
View File
@@ -7,6 +7,7 @@ namespace Annotation\Model;
use Annotation\Attribute;
use Database\ActiveRecord;
use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake;
/**
@@ -30,13 +31,10 @@ 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);
$annotation = Snowflake::getAnnotation();
$annotation->addRelate($class::class, $this->name, $method);
return true;
}
+9 -8
View File
@@ -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
* @param mixed $class
* @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 */
[$activeRecord, $method] = $handler;
return $activeRecord->addSets($this->name, $method);
$annotation = Snowflake::getAnnotation();
$annotation->addSets($class::class, $this->name, $method);
return true;
}
+4 -4
View File
@@ -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;
}
}
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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];
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
+3 -3
View File
@@ -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;
}
+3 -3
View File
@@ -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
}
+3 -3
View File
@@ -42,12 +42,12 @@ 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->addProducer($this->cmd, $handler, $this->config);
$rpc->addProducer($this->cmd, [$class, $method], $this->config);
return parent::execute($handler);
return true;
}
+6 -6
View File
@@ -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();
+56 -14
View File
@@ -126,6 +126,9 @@ class Command extends Component
} 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');
@@ -140,20 +143,20 @@ class Command extends Component
*/
private function search($type): mixed
{
$connect = $this->db->getConnect($this->sql);
if (!($query = $connect?->query($this->sql))) {
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
if (($prepare = $this->prepare()) == false) {
return false;
}
defer(fn() => $query->closeCursor());
if ($type === static::FETCH_COLUMN) {
return $query->fetchAll(PDO::FETCH_ASSOC);
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
} else if ($type === static::ROW_COUNT) {
return $query->rowCount();
$data = $prepare->rowCount();
} else if ($type === static::FETCH_ALL) {
return $query->fetchAll(PDO::FETCH_ASSOC);
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
} 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
{
if (($result = $this->initPDOStatement()) === false) {
if (($result = $this->getPdoStatement()) === false) {
return $result;
}
if ($isInsert === false) {
@@ -182,7 +185,7 @@ class Command extends Component
* 重新构建
* @throws
*/
private function initPDOStatement(): bool|int
private function getPdoStatement(): bool|int
{
if (empty($this->sql)) {
return $this->addError('no sql.', 'mysql');
@@ -191,18 +194,57 @@ class Command extends Component
return $this->addError('get client error.', 'mysql');
}
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 $prepare;
}
/**
* @param $prepare
* @param $connect
* @return bool|int
* @throws \Exception
*/
private function checkResponse($prepare, $connect)
{
$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
@@ -242,10 +284,10 @@ class Command extends Component
}
/**
* @param array|null $data
* @param array $data
* @return $this
*/
public function bindValues(array $data = NULL): static
public function bindValues(array $data = []): static
{
if (!is_array($this->params)) {
$this->params = [];
+2 -2
View File
@@ -185,8 +185,8 @@ class Connection extends Component
*/
public function slaveInstance(): PDO
{
if ($this->inTransaction() || empty($this->slaveConfig)) {
return $this->masterInstance();
if (empty($this->slaveConfig)) {
$this->slaveConfig = ['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password];
}
return $this->connections()->get($this->slaveConfig, false);
}
+1 -1
View File
@@ -180,7 +180,7 @@ class Pagination extends Component
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
} finally {
fire(Event::SYSTEM_RESOURCE_RELEASES);
logger()->insert();
}
}
+2 -2
View File
@@ -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);
+1 -1
View File
@@ -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;
}