This commit is contained in:
as2252258@163.com
2021-05-03 03:48:52 +08:00
parent a90e647529
commit 8137b24865
25 changed files with 273 additions and 306 deletions
+7 -10
View File
@@ -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;
}
+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;
}
+5 -8
View File
@@ -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;
}
}
+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;
}
}
+5 -5
View File
@@ -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
View File
@@ -16,59 +16,59 @@ use Snowflake\Snowflake;
{
/**
* Inject constructor.
* @param string $className
* @param array $args
*/
public function __construct(private string $className, private array $args = [])
{
}
/**
* Inject constructor.
* @param string $className
* @param array $args
*/
public function __construct(private string $className, private array $args = [])
{
}
/**
* @param array $handler
* @return mixed
* @throws Exception
*/
public function execute(array $handler): mixed
{
$injectValue = $this->parseInjectValue();
if (!($handler[1] instanceof ReflectionProperty)) {
$handler[1] = new ReflectionProperty($handler[0], $handler[1]);
}
/**
* @param array $handler
* @return mixed
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): mixed
{
$injectValue = $this->parseInjectValue();
if (!($class instanceof ReflectionProperty)) {
$class = new ReflectionProperty($class, $method);
}
/** @var ReflectionProperty $handler [1] */
if ($handler[1]->isPrivate() || $handler[1]->isProtected()) {
$method = 'set' . ucfirst($handler[1]->getName());
if (!method_exists($handler[0], $method)) {
return false;
}
$handler[0]->$method($injectValue);
} else {
$handler[0]->{$handler[1]->getName()} = $injectValue;
}
return $handler[0];
}
/** @var ReflectionProperty $class */
if ($class->isPrivate() || $class->isProtected()) {
$method = 'set' . ucfirst($class->getName());
if (!method_exists($class, $method)) {
return false;
}
$class->$method($injectValue);
} else {
$class->{$method->getName()} = $injectValue;
}
return true;
}
/**
* @return mixed
* @throws Exception
*/
private function parseInjectValue(): mixed
{
if (class_exists($this->className)) {
$injectValue = Snowflake::createObject($this->className, $this->args);
} else if (Snowflake::app()->has($this->className)) {
$injectValue = Snowflake::app()->get($this->className);
} else {
$injectValue = $this->className;
}
/**
* @return mixed
* @throws Exception
*/
private function parseInjectValue(): mixed
{
if (class_exists($this->className)) {
$injectValue = Snowflake::createObject($this->className, $this->args);
} else if (Snowflake::app()->has($this->className)) {
$injectValue = Snowflake::app()->get($this->className);
} else {
$injectValue = $this->className;
}
// if (!empty($this->args) && is_object($injectValue)) {
// Snowflake::configure($injectValue, $this->args);
// }
return $injectValue;
}
return $injectValue;
}
}
+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;
}
+105 -49
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,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
View File
@@ -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
View File
@@ -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;
}
}
+5 -7
View File
@@ -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
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
*/
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
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;
+2 -2
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);
$router->addRoute($method, $handler, 'sw::socket');
$router->addRoute($method, [$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
}
+28 -28
View File
@@ -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;
}
}