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
+5 -8
View File
@@ -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;
} }
+3 -2
View File
@@ -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;
} }
+4 -7
View File
@@ -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;
} }
} }
+2 -2
View File
@@ -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;
} }
-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 * @param array $handler
* @return mixed * @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 * @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 (!($class instanceof ReflectionProperty)) {
$handler[1] = new ReflectionProperty($handler[0], $handler[1]); $class = new ReflectionProperty($class, $method);
} }
/** @var ReflectionProperty $handler [1] */ /** @var ReflectionProperty $class */
if ($handler[1]->isPrivate() || $handler[1]->isProtected()) { if ($class->isPrivate() || $class->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;
} }
+4 -4
View File
@@ -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
View File
@@ -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();
}
} }
+6 -7
View File
@@ -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
} }
} }
+6 -8
View File
@@ -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);
} }
+4 -6
View File
@@ -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;
} }
+9 -8
View File
@@ -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
View File
@@ -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;
} }
} }
+1 -1
View File
@@ -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;
} }
+1 -1
View File
@@ -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];
+1 -1
View File
@@ -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;
} }
+1 -1
View File
@@ -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;
} }
+1 -1
View File
@@ -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;
} }
+1 -1
View File
@@ -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;
} }
+2 -2
View File
@@ -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;
} }
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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); $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; return $router;
} }
+3 -3
View File
@@ -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
} }
+3 -3
View File
@@ -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;
} }