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

56 lines
1.2 KiB
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-03-30 11:34:10 +08:00
use Exception;
2021-02-22 17:44:24 +08:00
use HttpServer\Route\Router;
2020-12-15 12:07:46 +08:00
use Snowflake\Exception\ConfigException;
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
{
2021-07-27 03:30:55 +08:00
/**
* Route constructor.
* @param string $uri
* @param string $method
* @param string $version
*/
public function __construct(
public string $uri,
public string $method,
public string $version = 'v.1.0'
)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
2021-05-03 03:48:52 +08:00
public function execute(mixed $class, mixed $method = null): Router
2021-07-27 03:30:55 +08:00
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$node = $router->addRoute($this->uri, [$class, $method], $this->method);
if ($node !== null) {
$attribute = Snowflake::getDi()->getMethodAttribute($class::class, $method);
foreach ($attribute as $item) {
if ($item instanceof Route) {
continue;
}
$item->execute($class, $method);
}
}
return $router;
}
2020-12-14 19:03:05 +08:00
}