This commit is contained in:
2020-12-15 12:24:38 +08:00
parent cc848807f7
commit c2d9250be9
10 changed files with 134 additions and 196 deletions
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace Annotation\Route;
trait Node
{
/**
* @param $node
* @return mixed
*/
public function add($node): mixed
{
if (!empty($this->middleware)) {
$node->addMiddleware($this->middleware);
}
if (!empty($this->interceptor)) {
$node->addInterceptor($this->interceptor);
}
if (!empty($this->limits)) {
$node->addLimits($this->limits);
}
if (!empty($this->after)) {
$node->addAfter($this->after);
}
return $node;
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace Annotation\Route;
use Closure;
use Exception;
use HttpServer\Route\Node;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Annotation\IAnnotation;
#[\Attribute(\Attribute::TARGET_METHOD)] class Route implements IAnnotation
{
use \Annotation\Route\Node;
/**
* Route constructor.
* @param string $uri
* @param string $method
* @param array|null $middleware
* @param array|null $interceptor
* @param array|null $limits
* @param array|null $after
*/
public function __construct(
public string $uri,
public string $method,
public ?array $middleware = null,
public ?array $interceptor = null,
public ?array $limits = null,
public ?array $after = null
)
{
}
/**
* @param array|Closure $handler
* @param array $attributes
* @return ?Node
* @throws ComponentException
* @throws ConfigException
* @throws Exception
*/
public function setHandler(array|Closure $handler, array $attributes): ?Node
{
$router = Snowflake::app()->getRouter();
// TODO: Implement setHandler() method.
$node = $router->addRoute($this->uri, $handler, $this->method);
return $this->add($node);
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace Annotation\Route;
use Closure;
use Exception;
use HttpServer\Route\Node;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Socket
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket
{
const CLOSE = 'CLOSE';
const MESSAGE = 'MESSAGE';
const HANDSHAKE = 'HANDSHAKE';
use \Annotation\Route\Node;
/**
* Socket constructor.
* @param string $event
* @param string|null $uri
* @param array|null $middleware
* @param array|null $interceptor
* @param array|null $limits
* @param array|null $after
*/
public function __construct(
public string $event,
public ?string $uri,
public ?array $middleware = null,
public ?array $interceptor = null,
public ?array $limits = null,
public ?array $after = null
)
{
}
/**
* @param array|Closure $handler
* @param array $attributes
* @return Node|null
* @throws ComponentException
* @throws ConfigException
* @throws Exception
*/
public function setHandler(array|Closure $handler, array $attributes): ?Node
{
$router = Snowflake::app()->getRouter();
// TODO: Implement setHandler() method.
$method = $this->event . '::' . ($this->uri ?? 'event');
$node = $router->addRoute($method, $handler, 'sw::socket');
return $this->add($node);
}
}