改名
This commit is contained in:
@@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace HttpServer\Abstracts;
|
|
||||||
|
|
||||||
use HttpServer\IInterface\IMiddleware;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class MiddlewareHandler
|
|
||||||
* @package Snowflake\Snowflake\Route
|
|
||||||
*/
|
|
||||||
abstract class MiddlewareHandler implements IMiddleware
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,7 @@ use HttpServer\Http\Request;
|
|||||||
* Interface IMiddleware
|
* Interface IMiddleware
|
||||||
* @package Snowflake\Snowflake\Route
|
* @package Snowflake\Snowflake\Route
|
||||||
*/
|
*/
|
||||||
interface IMiddleware
|
interface Middleware
|
||||||
{
|
{
|
||||||
|
|
||||||
public function handler(Request $request,\Closure $next);
|
public function handler(Request $request,\Closure $next);
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace HttpServer\Route\Annotation;
|
||||||
|
|
||||||
|
use ReflectionException;
|
||||||
|
use Snowflake\Abstracts\BaseAnnotation;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Annotation
|
||||||
|
*/
|
||||||
|
class Annotation extends \Snowflake\Annotation\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];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,27 +4,27 @@
|
|||||||
namespace HttpServer\Route;
|
namespace HttpServer\Route;
|
||||||
|
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Exception;
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
use HttpServer\Http\Response;
|
use HttpServer\Http\Response;
|
||||||
use HttpServer\Abstracts\MiddlewareHandler;
|
|
||||||
use Snowflake\Snowflake;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CoreMiddleware
|
* Class CoreMiddleware
|
||||||
* @package Snowflake\Snowflake\Route
|
* @package Snowflake\Snowflake\Route
|
||||||
* 跨域中间件
|
* 跨域中间件
|
||||||
*/
|
*/
|
||||||
class CoreMiddleware extends MiddlewareHandler
|
class CoreMiddleware implements \HttpServer\IInterface\Middleware
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param \Closure $next
|
* @param Closure $next
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function handler(Request $request,\Closure $next)
|
public function handler(Request $request, Closure $next)
|
||||||
{
|
{
|
||||||
$header = $request->headers;
|
$header = $request->headers;
|
||||||
|
|
||||||
|
|||||||
@@ -61,12 +61,10 @@ class Dispatch
|
|||||||
*/
|
*/
|
||||||
protected function bindParam()
|
protected function bindParam()
|
||||||
{
|
{
|
||||||
/** @var Controller $controller */
|
if ($this->handler instanceof \Closure) {
|
||||||
if (is_array($this->handler)) {
|
return;
|
||||||
$controller = $this->handler[0];
|
|
||||||
} else {
|
|
||||||
$controller = $this->handler;
|
|
||||||
}
|
}
|
||||||
|
$controller = $this->handler[0];
|
||||||
$controller->request = Context::getContext('request');
|
$controller->request = Context::getContext('request');
|
||||||
$controller->headers = $controller->request->headers;
|
$controller->headers = $controller->request->headers;
|
||||||
$controller->input = $controller->request->params;
|
$controller->input = $controller->request->params;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ namespace HttpServer\Route;
|
|||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\IInterface\IMiddleware;
|
|
||||||
use HttpServer\Route\Dispatch\Dispatch;
|
use HttpServer\Route\Dispatch\Dispatch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,7 +64,7 @@ class Middleware
|
|||||||
{
|
{
|
||||||
return function ($stack, $pipe) {
|
return function ($stack, $pipe) {
|
||||||
return function ($passable) use ($stack, $pipe) {
|
return function ($passable) use ($stack, $pipe) {
|
||||||
if ($pipe instanceof IMiddleware) {
|
if ($pipe instanceof \HttpServer\IInterface\Middleware) {
|
||||||
return $pipe->handler($passable, $stack);
|
return $pipe->handler($passable, $stack);
|
||||||
} else {
|
} else {
|
||||||
return $pipe($passable, $stack);
|
return $pipe($passable, $stack);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace HttpServer\Route;
|
|||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Application;
|
use HttpServer\Application;
|
||||||
|
use HttpServer\Route\Annotation\Annotation;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,6 +37,8 @@ class Node extends Application
|
|||||||
public $middleware = [];
|
public $middleware = [];
|
||||||
public $callback = [];
|
public $callback = [];
|
||||||
|
|
||||||
|
private $_interceptors = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $handler
|
* @param $handler
|
||||||
* @return Node
|
* @return Node
|
||||||
@@ -118,7 +121,7 @@ class Node extends Application
|
|||||||
private function getReflect(string $controller, string $action)
|
private function getReflect(string $controller, string $action)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$reflect = new \ReflectionClass($controller);
|
$reflect = Snowflake::getDi()->getReflect($controller);
|
||||||
if (!$reflect->isInstantiable()) {
|
if (!$reflect->isInstantiable()) {
|
||||||
throw new Exception($controller . ' Class is con\'t Instantiable.');
|
throw new Exception($controller . ' Class is con\'t Instantiable.');
|
||||||
}
|
}
|
||||||
@@ -126,6 +129,15 @@ class Node extends Application
|
|||||||
if (!empty($action) && !$reflect->hasMethod($action)) {
|
if (!empty($action) && !$reflect->hasMethod($action)) {
|
||||||
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
|
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var Annotation $annotation */
|
||||||
|
$annotation = Snowflake::createObject(Annotation::class);
|
||||||
|
if (!empty($methods)) {
|
||||||
|
$annotations = $annotation->instance($reflect);
|
||||||
|
if (isset($annotations['Interceptor'])) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
return [$reflect->newInstance(), $action];
|
return [$reflect->newInstance(), $action];
|
||||||
} catch (Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
$this->_error = $exception->getMessage();
|
$this->_error = $exception->getMessage();
|
||||||
|
|||||||
@@ -9,9 +9,12 @@ use HttpServer\Http\Context;
|
|||||||
use HttpServer\Controller;
|
use HttpServer\Controller;
|
||||||
use HttpServer\IInterface\RouterInterface;
|
use HttpServer\IInterface\RouterInterface;
|
||||||
use HttpServer\Application;
|
use HttpServer\Application;
|
||||||
|
use HttpServer\Route\Annotation\Annotation;
|
||||||
|
use ReflectionException;
|
||||||
use Snowflake\Config;
|
use Snowflake\Config;
|
||||||
use Snowflake\Core\JSON;
|
use Snowflake\Core\JSON;
|
||||||
use Snowflake\Exception\ConfigException;
|
use Snowflake\Exception\ConfigException;
|
||||||
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -498,10 +501,20 @@ class Router extends Application implements RouterInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $file
|
* @param $file
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @throws NotFindClassException
|
||||||
*/
|
*/
|
||||||
private function loadFile($file)
|
private function loadFile($file)
|
||||||
{
|
{
|
||||||
$router = $this;
|
$router = $this;
|
||||||
|
|
||||||
|
$prefix = APP_PATH . 'app/Http/';
|
||||||
|
|
||||||
|
/** @var Annotation $annotation */
|
||||||
|
$annotation = Snowflake::createObject(Annotation::class);
|
||||||
|
$annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor');
|
||||||
|
$annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits');
|
||||||
|
|
||||||
include_once "$file";
|
include_once "$file";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-4
@@ -93,10 +93,9 @@ return [
|
|||||||
],
|
],
|
||||||
'events' => [
|
'events' => [
|
||||||
Event::SERVER_WORKER_START => function () {
|
Event::SERVER_WORKER_START => function () {
|
||||||
|
$path = APP_PATH . 'app/Websocket';
|
||||||
$websocket = Snowflake::get()->annotation->websocket;
|
$websocket = Snowflake::get()->annotation->websocket;
|
||||||
// $websocket->path = $this->socketControllers;
|
$websocket->registration_notes($path, 'App\\Sockets\\');
|
||||||
$websocket->namespace = 'App\\Sockets\\';
|
|
||||||
$websocket->registration_notes();
|
|
||||||
},
|
},
|
||||||
Event::SERVER_HANDSHAKE => function (Request $request, Response $response) {
|
Event::SERVER_HANDSHAKE => function (Request $request, Response $response) {
|
||||||
$this->error($request->fd . ' connect.');
|
$this->error($request->fd . ' connect.');
|
||||||
@@ -105,8 +104,15 @@ return [
|
|||||||
},
|
},
|
||||||
Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) {
|
Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) {
|
||||||
$this->error('websocket SERVER_MESSAGE.');
|
$this->error('websocket SERVER_MESSAGE.');
|
||||||
|
if (is_null($json = json_decode($frame->data, true))) {
|
||||||
|
return $server->push($frame->fd, 'format error~');
|
||||||
|
}
|
||||||
|
$websocket = Snowflake::get()->annotation->websocket;
|
||||||
|
if ($websocket->has($json['path'])) {
|
||||||
|
return $websocket->runWith($json['path'], [$frame->fd, $json]);
|
||||||
|
} else {
|
||||||
return $server->push($frame->fd, 'hello word~');
|
return $server->push($frame->fd, 'hello word~');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Event::SERVER_CLOSE => function (int $fd) {
|
Event::SERVER_CLOSE => function (int $fd) {
|
||||||
$this->error($fd . ' disconnect.');
|
$this->error($fd . ' disconnect.');
|
||||||
|
|||||||
@@ -4,6 +4,12 @@
|
|||||||
namespace Snowflake\Abstracts;
|
namespace Snowflake\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionException;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use Snowflake\Exception\NotFindClassException;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BaseAnnotation
|
* Class BaseAnnotation
|
||||||
@@ -12,9 +18,108 @@ namespace Snowflake\Abstracts;
|
|||||||
abstract class BaseAnnotation extends Component
|
abstract class BaseAnnotation extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public function each()
|
|
||||||
|
/**
|
||||||
|
* @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 ReflectionClass $reflect
|
||||||
|
* @param array $rules
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function instance($reflect, $rules = [])
|
||||||
|
{
|
||||||
|
$annotations = $this->getPrivates($reflect);
|
||||||
|
|
||||||
|
$classMethods = $reflect->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||||
|
if (!$reflect->isInstantiable()) {
|
||||||
|
throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$object = $reflect->newInstance();
|
||||||
|
|
||||||
|
$array = [];
|
||||||
|
foreach ($classMethods as $classMethod) {
|
||||||
|
$array = $this->resolveDocComment($classMethod, $object, $annotations, $array);
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ReflectionMethod $function
|
||||||
|
* @param $object
|
||||||
|
* @param $annotations
|
||||||
|
* @param $array
|
||||||
|
* @return array
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
protected function resolveDocComment($function, $object, $annotations, $array)
|
||||||
|
{
|
||||||
|
$comment = $function->getDocComment();
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $rule
|
||||||
|
* @param $content
|
||||||
|
* @param $rules
|
||||||
|
* @return bool
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @throws NotFindClassException
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function check($rule, $content, $rules)
|
||||||
|
{
|
||||||
|
if (empty($rule)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
$explode = explode('|', $rule);
|
||||||
|
foreach ($explode as $value) {
|
||||||
|
$reflect = array_merge($rules[$value], [
|
||||||
|
'value' => $content
|
||||||
|
]);
|
||||||
|
$validator = Snowflake::createObject($reflect);
|
||||||
|
if (!$validator->check()) {
|
||||||
|
throw new Exception($validator->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
abstract public function runWith($path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,19 +6,24 @@ namespace Snowflake\Annotation;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
use ReflectionMethod;
|
||||||
use Snowflake\Abstracts\BaseAnnotation;
|
use Snowflake\Abstracts\BaseAnnotation;
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
use validator\RequiredValidator;
|
||||||
|
use validator\RequiredValidator as NotEmptyValidator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Annotation
|
* Class Annotation
|
||||||
* @package Snowflake\Snowflake\Annotation
|
* @package Snowflake\Snowflake\Annotation
|
||||||
* @property Websocket $websocket
|
* @property Websocket $websocket
|
||||||
|
* @property Http $http
|
||||||
*/
|
*/
|
||||||
class Annotation extends BaseAnnotation
|
class Annotation extends BaseAnnotation
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $_Scan_directory = [];
|
protected $_Scan_directory = [];
|
||||||
|
protected $params = [];
|
||||||
|
|
||||||
public $namespace = '';
|
public $namespace = '';
|
||||||
|
|
||||||
@@ -27,8 +32,19 @@ class Annotation extends BaseAnnotation
|
|||||||
public $path = '';
|
public $path = '';
|
||||||
|
|
||||||
|
|
||||||
|
private $rules = [
|
||||||
|
'required' => [
|
||||||
|
'class' => RequiredValidator::class
|
||||||
|
],
|
||||||
|
'not empty' => [
|
||||||
|
'class' => NotEmptyValidator::class
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
private $_classMap = [
|
private $_classMap = [
|
||||||
'websocket' => Websocket::class
|
'websocket' => Websocket::class,
|
||||||
|
'http' => Http::class
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
@@ -43,59 +59,126 @@ class Annotation extends BaseAnnotation
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param $namespace
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function registration_notes()
|
public function registration_notes($path, $namespace)
|
||||||
{
|
{
|
||||||
if (!is_array($this->path)) {
|
foreach ($path as $item) {
|
||||||
return;
|
$this->scanning($item, $namespace);
|
||||||
}
|
|
||||||
foreach ($this->path as $item) {
|
|
||||||
$this->scanning($item);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function getHttp()
|
||||||
|
{
|
||||||
|
return Snowflake::createObject($this->_classMap['http']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function getWebsocket()
|
||||||
|
{
|
||||||
|
return Snowflake::createObject($this->_classMap['websocket']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ReflectionClass $reflect
|
||||||
|
* @Message(updatePosition)
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function resolve(ReflectionClass $reflect)
|
||||||
|
{
|
||||||
|
$controller = $reflect->newInstance();
|
||||||
|
|
||||||
|
$methods = $this->getPrivates($reflect);
|
||||||
|
|
||||||
|
foreach ($methods as $function) {
|
||||||
|
$comment = $function->getDocComment();
|
||||||
|
$methodName = $function->getName();
|
||||||
|
|
||||||
|
preg_match('/@(' . $function . ')\((.*?)\)/', $comment, $events);
|
||||||
|
if (!isset($events[1])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!$this->isLegitimate($events)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$_key = $this->getName($function, $events);
|
||||||
|
if (empty($events[2])) {
|
||||||
|
$this->push($_key, [$controller, $methodName]);
|
||||||
|
} else {
|
||||||
|
$handler = $this->createHandler($controller, $methodName, $events[2]);
|
||||||
|
|
||||||
|
$this->push($_key, $handler, [request(), [$controller, $methodName]]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $events
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function isLegitimate($events)
|
||||||
|
{
|
||||||
|
throw new Exception('Undefined analytic function.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $function
|
||||||
|
* @param $events
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function getName($function, $events)
|
||||||
|
{
|
||||||
|
throw new Exception('Undefined analytic function.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $controller
|
||||||
|
* @param $methodName
|
||||||
|
* @param $events
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function createHandler($controller, $methodName, $events)
|
||||||
|
{
|
||||||
|
throw new Exception('Undefined analytic function.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @param $namespace
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function scanning($path = '')
|
protected function scanning($path, $namespace)
|
||||||
{
|
{
|
||||||
if (empty($path)) {
|
$di = Snowflake::getDi();
|
||||||
$path = rtrim($this->path, '/');
|
|
||||||
}
|
|
||||||
foreach (glob($path . '/*') as $file) {
|
foreach (glob($path . '/*') as $file) {
|
||||||
if (is_dir($file)) {
|
if (is_dir($file)) {
|
||||||
$this->scanning($path);
|
$this->scanning($path, $namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
$explode = explode('/', $file);
|
$explode = explode('/', $file);
|
||||||
|
|
||||||
$class = str_replace('.php', '', end($explode));
|
$class = str_replace('.php', '', end($explode));
|
||||||
|
|
||||||
$reflect = new ReflectionClass($this->namespace . $class);
|
$this->resolve($di->getReflect($namespace . '\\' . $class));
|
||||||
|
|
||||||
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
|
|
||||||
if (empty($methods) || !$reflect->isInstantiable()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->resolve($reflect, $methods);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ReflectionClass $class
|
|
||||||
* @param array $methods
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function resolve(ReflectionClass $class, array $methods)
|
|
||||||
{
|
|
||||||
throw new Exception('Undefined analytic function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param mixed ...$param
|
* @param mixed ...$param
|
||||||
@@ -106,17 +189,25 @@ class Annotation extends BaseAnnotation
|
|||||||
if (!$this->has($path)) {
|
if (!$this->has($path)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return call_user_func($this->_Scan_directory[$path], ...$param);
|
$callback = $this->_Scan_directory[$path];
|
||||||
|
if (!isset($this->params[$path])) {
|
||||||
|
return $callback(...$param);
|
||||||
|
}
|
||||||
|
return $callback(...$this->params[$path]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $callback
|
* @param $callback
|
||||||
|
* @param array $params
|
||||||
*/
|
*/
|
||||||
public function push($name, $callback)
|
public function push($name, $callback, $params = [])
|
||||||
{
|
{
|
||||||
$this->_Scan_directory[$name] = $callback;
|
$this->_Scan_directory[$name] = $callback;
|
||||||
|
if (!empty($params)) {
|
||||||
|
$this->params[$name] = $params;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Snowflake\Annotation;
|
||||||
|
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionException;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Http
|
||||||
|
* @package Snowflake\Annotation
|
||||||
|
*/
|
||||||
|
class Http extends Annotation
|
||||||
|
{
|
||||||
|
|
||||||
|
const HTTP_EVENT = 'http:event:';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
* 拦截器
|
||||||
|
*/
|
||||||
|
private $Interceptor;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
* 限速
|
||||||
|
*/
|
||||||
|
private $Limits;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $controller
|
||||||
|
* @param $methodName
|
||||||
|
* @param $handler
|
||||||
|
* @return array
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function createLimits($controller, $methodName, $handler)
|
||||||
|
{
|
||||||
|
$namespace = 'App\Http\Interceptor\\' . $handler;
|
||||||
|
$class = Snowflake::getDi()->getReflect($namespace);
|
||||||
|
|
||||||
|
$object = $class->newInstance();
|
||||||
|
|
||||||
|
|
||||||
|
$method = $class->getMethod('Interceptor');
|
||||||
|
|
||||||
|
|
||||||
|
return [$object, 'Interceptor'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $controller
|
||||||
|
* @param $methodName
|
||||||
|
* @param $handler
|
||||||
|
* @return array
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function createInterceptor($controller, $methodName, $handler)
|
||||||
|
{
|
||||||
|
$namespace = 'App\Interceptor\\' . $handler;
|
||||||
|
$class = Snowflake::getDi()->getReflect($namespace);
|
||||||
|
|
||||||
|
$object = $class->newInstance();
|
||||||
|
|
||||||
|
return [$object, 'Interceptor', [request(), [$controller, $methodName]]];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @param $events
|
||||||
|
* @return false|string
|
||||||
|
*/
|
||||||
|
public function getName($name, $events)
|
||||||
|
{
|
||||||
|
return self::HTTP_EVENT . $name . ':' . $events[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -12,55 +12,41 @@ use ReflectionClass;
|
|||||||
class Websocket extends Annotation
|
class Websocket extends Annotation
|
||||||
{
|
{
|
||||||
|
|
||||||
const MESSAGE = 'WEBSOCKET:MESSAGE:';
|
const WEBSOCKET_ANNOTATION = 'WEBSOCKET:ANNOTATION:';
|
||||||
const EVENT = 'WEBSOCKET:EVENT:';
|
|
||||||
|
|
||||||
public $Message;
|
private $Message = 'required|not empty';
|
||||||
|
|
||||||
|
|
||||||
public $Event;
|
private $Handshake;
|
||||||
|
|
||||||
|
|
||||||
|
private $Close;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionClass $reflect
|
* @param $controller
|
||||||
* @param array $methods
|
* @param $methodName
|
||||||
|
* @param $events
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function resolve(ReflectionClass $reflect, array $methods)
|
public function createHandler($controller, $methodName, $events)
|
||||||
{
|
{
|
||||||
$controller = $reflect->newInstance();
|
return [$controller, $methodName];
|
||||||
|
|
||||||
foreach ($methods as $function) {
|
|
||||||
$comment = $function->getDocComment();
|
|
||||||
$methodName = $function->getName();
|
|
||||||
|
|
||||||
preg_match('/@Event\((.*)?\)/', $comment, $events);
|
|
||||||
if (!isset($events[1])) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!($_key = $this->getName($events, $comment))) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->push($_key, [$controller, $methodName]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $events
|
* @param $events
|
||||||
* @param $comment
|
* @param $comment
|
||||||
* @return false|string
|
* @return false|string
|
||||||
*/
|
*/
|
||||||
private function getName($events, $comment)
|
public function getName($events, $comment)
|
||||||
{
|
{
|
||||||
$event = $events[1];
|
$prefix = self::WEBSOCKET_ANNOTATION . $events;
|
||||||
if ($event !== 'message') {
|
if (isset($comment[2])) {
|
||||||
return self::EVENT . $event;
|
return $prefix . ':' . $comment[2];
|
||||||
}
|
}
|
||||||
preg_match('/@Message\((.*)?\)/', $comment, $message);
|
return $prefix;
|
||||||
if (isset($message[1])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return self::MESSAGE . $message[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,6 +182,21 @@ class Container extends BaseObject
|
|||||||
return [$reflection, $dependencies];
|
return [$reflection, $dependencies];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $class
|
||||||
|
* @return mixed
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function getReflect($class): ReflectionClass
|
||||||
|
{
|
||||||
|
if (!isset($this->_reflection[$class])) {
|
||||||
|
$this->resolveDependencies($class);
|
||||||
|
}
|
||||||
|
return $this->_reflection[$class];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user