This commit is contained in:
2020-09-01 03:11:34 +08:00
parent ae753bc267
commit 689c2556e6
8 changed files with 194 additions and 191 deletions
+17 -16
View File
@@ -11,6 +11,7 @@ namespace HttpServer\Events;
use Exception;
use HttpServer\ServerManager;
use ReflectionException;
use Snowflake\Application;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
@@ -20,6 +21,7 @@ use Swoole\Http\Response as SResponse;
use Swoole\Process\Pool;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
/**
* Class ServerWebSocket
@@ -32,6 +34,7 @@ class WebSocket extends Server
public $callback = [];
/** @var Application */
public $application;
@@ -63,8 +66,14 @@ class WebSocket extends Server
public function set(array $settings, $pool = null, $events = [], $config = [])
{
parent::set($settings);
Snowflake::get()->set(WebSocket::class, $this);
Snowflake::get()->set(Pool::class, $pool);
$application = Snowflake::get();
$application->set(WebSocket::class, $this);
$application->set(Pool::class, $pool);
$annotation = $application->annotation;
$annotation->register('websocket', AWebsocket::class);
ServerManager::set($this, $settings, $this->application, $events, $config);
}
@@ -87,11 +96,12 @@ class WebSocket extends Server
}
$json = json_decode($frame->data, true);
$manager = Snowflake::get()->annotation;
$manager->runWith($this->getName($json), [$frame->fd, $server]);
/** @var AWebsocket $manager */
$manager = Snowflake::get()->annotation->get('websocket');
$path = $manager->getName(AWebsocket::MESSAGE, $json['route']);
$manager->runWith($path, [$frame->fd, $server]);
} catch (Exception $exception) {
// $this->error($exception->getMessage(), __METHOD__, __FILE__);
// $this->addError($exception->getMessage());
$this->application->addError($exception->getMessage(), 'websocket');
} finally {
$event = Snowflake::get()->event;
$event->trigger(Event::EVENT_AFTER_REQUEST);
@@ -99,15 +109,6 @@ class WebSocket extends Server
}
}
/**
* @param $json
* @return string
*/
private function getName($json)
{
return 'WEBSOCKET:MESSAGE:' . $json['route'];
}
/**
* @param SRequest $request
* @param SResponse $response
@@ -177,7 +178,7 @@ class WebSocket extends Server
$event->trigger(Event::SERVER_CLOSE, [$fd]);
}
} catch (\Throwable $exception) {
// $this->addError($exception->getMessage());
$this->application->addError($exception->getMessage());
} finally {
$event->trigger(Event::RELEASE_ALL);
Logger::insert();
@@ -3,6 +3,7 @@
namespace HttpServer\Route\Annotation;
use ReflectionClass;
use ReflectionException;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Snowflake;
@@ -30,6 +31,47 @@ class Annotation extends \Snowflake\Annotation\Annotation
protected $_annotations = [];
/**
* @param ReflectionClass $reflect
* @param $method
* @param $annotations
* @return mixed|null
* @throws ReflectionException
*/
public function read($reflect, $method, $annotations)
{
$method = $reflect->getMethod($method);
$_annotations = $this->getDocCommentAnnotation($annotations, $method->getDocComment());
$array = [];
foreach ($_annotations as $keyName => $annotation) {
if (!in_array($keyName, $annotations)) {
continue;
}
$array[$keyName] = $this->pop($this->getName(...$annotation));
}
return $array;
}
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
* @throws
*/
public function createHandler($controller, $methodName, $events)
{
return Snowflake::createObject($events[2]);
}
/**
* @param $events
* @return bool
@@ -1,9 +1,8 @@
<?php
namespace Snowflake\Annotation\Definition;
namespace HttpServer\Route\Annotation;
use ReflectionClass;
use Snowflake\Annotation\Annotation;
/**
@@ -13,7 +12,9 @@ use Snowflake\Annotation\Annotation;
class Websocket extends Annotation
{
const WEBSOCKET_ANNOTATION = 'WEBSOCKET:ANNOTATION:';
const MESSAGE = 'Message:';
const HANDSHAKE = 'Handshake:';
const CLOSE = 'Close:';
private $Message = 'required|not empty';
@@ -53,7 +54,7 @@ class Websocket extends Annotation
*/
public function getName($events, $comment)
{
$prefix = self::WEBSOCKET_ANNOTATION . $events;
$prefix = 'WEBSOCKET:ANNOTATION:' . $events;
if (isset($comment[2])) {
return $prefix . ':' . $comment[2];
}
+4 -3
View File
@@ -8,6 +8,7 @@ use HttpServer\Http\Request;
use Exception;
use HttpServer\Application;
use HttpServer\Route\Annotation\Annotation;
use Snowflake\Annotation\Definition\Http;
use Snowflake\Snowflake;
/**
@@ -149,9 +150,9 @@ class Node extends Application
}
/** @var Annotation $annotation */
$annotation = Snowflake::get()->annotation->http;
if (!empty($methods = $annotation->getAnnotation(Annotation::class))) {
$this->_interceptors = $annotation->instance($reflect, $action, $methods);
$annotation = Snowflake::get()->annotation->get('http');
if (!empty($annotations = $annotation->getAnnotation(Annotation::class))) {
$this->_interceptors = $annotation->read($reflect, $action, $annotations);
}
return [$reflect->newInstance(), $action];
} catch (Exception $exception) {
+6 -3
View File
@@ -511,9 +511,12 @@ class Router extends Application implements RouterInterface
$prefix = APP_PATH . 'app/Http/';
/** @var Annotation $annotation */
$annotation = Snowflake::get()->annotation->getHttp();
$annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor',Annotation::class);
$annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits',Annotation::class);
$annotation = Snowflake::get()->annotation;
$annotation->register('http',Annotation::class);
$annotation = $annotation->get('http');
$annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor');
$annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits');
include_once "$file";
}
+50 -37
View File
@@ -19,34 +19,6 @@ abstract class BaseAnnotation extends Component
{
/**
* @param ReflectionClass $reflect
* @return array
*/
protected function getPrivates(ReflectionClass $reflect)
{
$arrays = [];
$properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE);
foreach ($properties as $property) {
$arrays[] = $property->getName();
}
return $arrays;
}
/**
* @param string $class
* @return string[]
* @throws ReflectionException
*/
public function getAnnotation(string $class)
{
$reflect = Snowflake::getDi()->getReflect($class);
return $this->getPrivates($reflect);
}
/**
* @param ReflectionClass $reflect
* @param string $method
@@ -86,23 +58,64 @@ abstract class BaseAnnotation extends Component
protected function resolveDocComment($function, $object, $annotations, $array)
{
$comment = $function->getDocComment();
$array = $this->getDocCommentAnnotation($annotations, $comment);
foreach ($array as $name => $annotation) {
foreach ($annotation as $index => $events) {
if (!isset($events[1])) {
continue;
}
if (!($_key = $this->getName($name, $events))) {
continue;
}
if (isset($item[2])) {
$handler = Snowflake::createObject($events[2]);
} else {
$handler = [$object, $events[1]];
}
if (!isset($array[$annotation])) {
$array[$annotation] = [];
}
$array[$name][] = [$_key, $handler];
}
}
return $array;
}
/**
* @param $object
* @param $events
* @throws NotFindClassException
* @throws ReflectionException
*/
protected function getOrCreate($object, $events)
{
if (isset($item[2])) {
$handler = Snowflake::createObject($events[2]);
} else {
$handler = [$object, $events[1]];
}
}
/**
* @param $annotations
* @param $comment
* @return array
*/
protected function getDocCommentAnnotation($annotations, $comment)
{
$array = [];
foreach ($annotations as $annotation) {
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
if (!isset($events[1])) {
continue;
}
if (!($_key = $this->getName($function, $events))) {
continue;
}
if (isset($events[2])) {
$handler = Snowflake::createObject($events[2]);
} else {
$handler = [$object, $events[1]];
}
if (!isset($array[$annotation])) {
$array[$annotation] = [];
}
$array[$annotation][] = [$_key, $handler];
$array[$annotation] = [$annotation, $events];
}
return $array;
}
+70 -59
View File
@@ -6,9 +6,8 @@ namespace Snowflake\Annotation;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Annotation\Definition\Http;
use Snowflake\Annotation\Definition\Websocket;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use validator\RequiredValidator;
@@ -17,15 +16,9 @@ use validator\RequiredValidator as NotEmptyValidator;
/**
* Class Annotation
* @package Snowflake\Snowflake\Annotation
* @property Websocket $websocket
* @property Http $http
*/
class Annotation extends BaseAnnotation
{
protected $_Scan_directory = [];
protected $params = [];
public $namespace = '';
public $prefix = '';
@@ -33,72 +26,34 @@ class Annotation extends BaseAnnotation
public $path = '';
private $rules = [
'required' => [
'class' => RequiredValidator::class
],
'not empty' => [
'class' => NotEmptyValidator::class
]
];
protected $_Scan_directory = [];
protected $_alias = [];
protected $params = [];
private $_classMap = [
'websocket' => Websocket::class,
'http' => Http::class
];
private $_classMap = [];
/**
* @param $name
* @param $class
* @throws
*/
public function register($name, $class)
{
$this->_classMap[$name] = $class;
$this->_classMap[$name] = Snowflake::createObject($class);
}
/**
* @param $path
* @param $namespace
* @param $class
* @throws ReflectionException
*/
public function registration_notes($path, $namespace, $class)
public function registration_notes($path, $namespace)
{
$this->scanning(rtrim($path, '/'), $namespace, $class);
}
/**
* @return string|Http
* @throws
*/
public function getHttp()
{
if (is_object($this->_classMap['http'])) {
return $this->_classMap['http'];
}
return $this->_classMap['http'] = Snowflake::createObject($this->_classMap['http']);
}
/**
* @return string|Websocket
* @throws
*/
public function getWebsocket()
{
if (is_object($this->_classMap['websocket'])) {
return $this->_classMap['websocket'];
}
return $this->_classMap['websocket'] = Snowflake::createObject($this->_classMap['websocket']);
return make($this->_classMap['websocket'], $this->_classMap['websocket']);
$this->scanning(rtrim($path, '/'), $namespace, get_called_class());
}
/**
@@ -107,8 +62,9 @@ class Annotation extends BaseAnnotation
* @throws ReflectionException
* @throws Exception
* @Message(updatePosition)
* 注入注解
*/
public function resolve(ReflectionClass $reflect, string $className)
private function resolve(ReflectionClass $reflect, string $className)
{
$controller = $reflect->newInstance();
@@ -139,6 +95,20 @@ class Annotation extends BaseAnnotation
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function get($name)
{
if (!isset($this->_Scan_directory[$name])) {
throw new Exception('Undefined analytic function.');
}
return $this->_Scan_directory[$name];
}
/**
* @param $events
* @throws Exception
@@ -196,16 +166,17 @@ class Annotation extends BaseAnnotation
/**
* @param $path
* @param array $params
* @return bool|mixed
*/
public function runWith($path)
public function runWith($path, $params = [])
{
if (!$this->has($path)) {
return null;
}
$callback = $this->_Scan_directory[$path];
if (!isset($this->params[$path])) {
return $callback();
return $callback(...$params);
}
return $callback(...$this->params[$path]);
}
@@ -225,6 +196,19 @@ class Annotation extends BaseAnnotation
}
/**
* @param $name
* @return array|null[]
*/
public function pop($name)
{
if (!isset($this->_Scan_directory[$name])) {
return [$this->_Scan_directory[$name], $this->params[$name] ?? []];
}
return [null, null];
}
/**
* @param $path
* @return bool|mixed
@@ -253,5 +237,32 @@ class Annotation extends BaseAnnotation
return $this->_classMap[$name];
}
/**
* @param ReflectionClass $reflect
* @return array
*/
protected function getPrivates(ReflectionClass $reflect)
{
$arrays = [];
$properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE);
foreach ($properties as $property) {
$arrays[] = $property->getName();
}
return $arrays;
}
/**
* @param string $class
* @return string[]
* @throws ReflectionException
*/
public function getAnnotation(string $class)
{
$reflect = Snowflake::getDi()->getReflect($class);
return $this->getPrivates($reflect);
}
}
-69
View File
@@ -1,69 +0,0 @@
<?php
namespace Snowflake\Annotation\Definition;
use Closure;
use ReflectionClass;
use ReflectionException;
use Snowflake\Annotation\Annotation;
use Snowflake\Snowflake;
/**
* Class Http
* @package Snowflake\Annotation
*/
class Http extends Annotation
{
const HTTP_EVENT = 'http:event:';
/**
* @var string
* @Interceptor(LoginInterceptor)
*/
private $Interceptor = 'required|not empty';
/**
* @var string
*/
private $Limits = 'required|not empty';
protected $_annotations = [];
/**
* @param $events
* @return bool
*/
public function isLegitimate($events)
{
return isset($events[2]) && !empty($events[2]);
}
/**
* @param $name
* @param $events
* @return false|string
*/
public function getName($name, $events)
{
return self::HTTP_EVENT . $name . ':' . $events[2];
}
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
}