This commit is contained in:
2020-08-31 22:33:50 +08:00
parent 32913fa3e1
commit 8b583f2a4d
14 changed files with 450 additions and 102 deletions
@@ -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
* @package Snowflake\Snowflake\Route
*/
interface IMiddleware
interface Middleware
{
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];
}
}
+6 -6
View File
@@ -4,27 +4,27 @@
namespace HttpServer\Route;
use Closure;
use Exception;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\Abstracts\MiddlewareHandler;
use Snowflake\Snowflake;
/**
* Class CoreMiddleware
* @package Snowflake\Snowflake\Route
* 跨域中间件
*/
class CoreMiddleware extends MiddlewareHandler
class CoreMiddleware implements \HttpServer\IInterface\Middleware
{
/**
* @param Request $request
* @param \Closure $next
* @param Closure $next
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function handler(Request $request,\Closure $next)
public function handler(Request $request, Closure $next)
{
$header = $request->headers;
+3 -5
View File
@@ -61,12 +61,10 @@ class Dispatch
*/
protected function bindParam()
{
/** @var Controller $controller */
if (is_array($this->handler)) {
$controller = $this->handler[0];
} else {
$controller = $this->handler;
if ($this->handler instanceof \Closure) {
return;
}
$controller = $this->handler[0];
$controller->request = Context::getContext('request');
$controller->headers = $controller->request->headers;
$controller->input = $controller->request->params;
+1 -2
View File
@@ -10,7 +10,6 @@ namespace HttpServer\Route;
use Closure;
use Exception;
use HttpServer\IInterface\IMiddleware;
use HttpServer\Route\Dispatch\Dispatch;
/**
@@ -65,7 +64,7 @@ class Middleware
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof IMiddleware) {
if ($pipe instanceof \HttpServer\IInterface\Middleware) {
return $pipe->handler($passable, $stack);
} else {
return $pipe($passable, $stack);
+13 -1
View File
@@ -7,6 +7,7 @@ namespace HttpServer\Route;
use HttpServer\Http\Request;
use Exception;
use HttpServer\Application;
use HttpServer\Route\Annotation\Annotation;
use Snowflake\Snowflake;
/**
@@ -36,6 +37,8 @@ class Node extends Application
public $middleware = [];
public $callback = [];
private $_interceptors = [];
/**
* @param $handler
* @return Node
@@ -118,7 +121,7 @@ class Node extends Application
private function getReflect(string $controller, string $action)
{
try {
$reflect = new \ReflectionClass($controller);
$reflect = Snowflake::getDi()->getReflect($controller);
if (!$reflect->isInstantiable()) {
throw new Exception($controller . ' Class is con\'t Instantiable.');
}
@@ -126,6 +129,15 @@ class Node extends Application
if (!empty($action) && !$reflect->hasMethod($action)) {
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];
} catch (Exception $exception) {
$this->_error = $exception->getMessage();
+13
View File
@@ -9,9 +9,12 @@ use HttpServer\Http\Context;
use HttpServer\Controller;
use HttpServer\IInterface\RouterInterface;
use HttpServer\Application;
use HttpServer\Route\Annotation\Annotation;
use ReflectionException;
use Snowflake\Config;
use Snowflake\Core\JSON;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
@@ -498,10 +501,20 @@ class Router extends Application implements RouterInterface
/**
* @param $file
* @throws ReflectionException
* @throws NotFindClassException
*/
private function loadFile($file)
{
$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";
}
+11 -5
View File
@@ -93,10 +93,9 @@ return [
],
'events' => [
Event::SERVER_WORKER_START => function () {
$path = APP_PATH . 'app/Websocket';
$websocket = Snowflake::get()->annotation->websocket;
// $websocket->path = $this->socketControllers;
$websocket->namespace = 'App\\Sockets\\';
$websocket->registration_notes();
$websocket->registration_notes($path, 'App\\Sockets\\');
},
Event::SERVER_HANDSHAKE => function (Request $request, Response $response) {
$this->error($request->fd . ' connect.');
@@ -105,8 +104,15 @@ return [
},
Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) {
$this->error('websocket SERVER_MESSAGE.');
return $server->push($frame->fd, 'hello word~');
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~');
}
},
Event::SERVER_CLOSE => function (int $fd) {
$this->error($fd . ' disconnect.');