Files
kiri-core/Annotation/Route/Route.php
T

53 lines
1001 B
PHP
Raw Normal View History

2020-12-14 19:03:05 +08:00
<?php
2020-12-15 12:24:38 +08:00
namespace Annotation\Route;
2020-12-14 19:03:05 +08:00
2021-03-03 18:35:04 +08:00
use Annotation\Attribute;
2021-02-22 17:44:24 +08:00
use HttpServer\Route\Router;
2021-03-03 18:35:04 +08:00
use ReflectionException;
2020-12-14 19:03:05 +08:00
use Snowflake\Exception\ComponentException;
2020-12-15 12:07:46 +08:00
use Snowflake\Exception\ConfigException;
2021-03-03 18:35:04 +08:00
use Snowflake\Exception\NotFindClassException;
2020-12-14 19:03:05 +08:00
use Snowflake\Snowflake;
2021-03-03 18:35:04 +08:00
#[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute
2020-12-14 19:03:05 +08:00
{
/**
* Route constructor.
* @param string $uri
* @param string $method
2021-03-03 18:35:04 +08:00
* @param string $version
2020-12-14 19:03:05 +08:00
*/
public function __construct(
public string $uri,
2021-03-03 18:35:04 +08:00
public string $method,
public string $version = 'v.1.0'
2020-12-14 19:03:05 +08:00
)
{
}
/**
2021-02-22 17:44:24 +08:00
* @param array $handler
* @return Router
2020-12-14 19:03:05 +08:00
* @throws ComponentException
2020-12-15 12:07:46 +08:00
* @throws ConfigException
2021-03-03 18:35:04 +08:00
* @throws ReflectionException
* @throws NotFindClassException
2020-12-14 19:03:05 +08:00
*/
2021-02-22 17:44:24 +08:00
public function execute(array $handler): Router
2020-12-14 19:03:05 +08:00
{
// TODO: Implement setHandler() method.
2021-02-22 17:44:24 +08:00
$router = Snowflake::app()->getRouter();
2021-03-03 18:35:04 +08:00
$router->addRoute($this->uri . $this->version, $handler, $this->method);
2020-12-14 19:03:05 +08:00
2021-02-22 17:44:24 +08:00
return $router;
2020-12-14 19:03:05 +08:00
}
}