2023-04-16 20:25:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
2023-04-16 20:36:47 +08:00
|
|
|
namespace Kiri\Router\Annotate;
|
2023-04-16 20:25:37 +08:00
|
|
|
|
|
|
|
|
use Kiri\Router\Constrict\RequestMethod;
|
|
|
|
|
use Kiri\Router\Router;
|
2023-12-18 22:23:17 +08:00
|
|
|
use Kiri\Di\Interface\InjectMethodInterface;
|
2023-04-16 20:25:37 +08:00
|
|
|
|
2023-12-06 14:32:47 +08:00
|
|
|
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
|
2023-12-18 22:23:17 +08:00
|
|
|
class Route extends AbstractRequestMethod implements InjectMethodInterface
|
2023-04-16 20:25:37 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2023-07-25 10:09:06 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param RequestMethod $method
|
|
|
|
|
* @param string $version
|
|
|
|
|
*/
|
2023-11-14 00:09:52 +08:00
|
|
|
public function __construct(readonly public string $path, readonly public RequestMethod $method, readonly public string $version = '')
|
|
|
|
|
{
|
|
|
|
|
}
|
2023-04-16 20:25:37 +08:00
|
|
|
|
|
|
|
|
|
2023-11-14 00:09:52 +08:00
|
|
|
/**
|
2023-12-18 22:23:17 +08:00
|
|
|
* @param string $class
|
2023-11-14 00:09:52 +08:00
|
|
|
* @param string $method
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-12-18 22:23:17 +08:00
|
|
|
public function dispatch(string $class, string $method): void
|
2023-11-14 00:09:52 +08:00
|
|
|
{
|
|
|
|
|
$path = '/' . ltrim($this->path, '/');
|
|
|
|
|
if (!empty($this->version)) {
|
|
|
|
|
$path = '/' . trim($this->version) . $path;
|
|
|
|
|
}
|
2024-08-29 15:56:30 +08:00
|
|
|
Router::addRoute([$this->method], $path, [$class, $method]);
|
2023-11-14 00:09:52 +08:00
|
|
|
}
|
2023-04-16 20:25:37 +08:00
|
|
|
}
|