1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BrenoRoosevelt\RouteAttributeProvider\League; |
5
|
|
|
|
6
|
|
|
use Jerowork\RouteAttributeProvider\Api\Route; |
7
|
|
|
use Jerowork\RouteAttributeProvider\RouteAttributeConfigurator; |
8
|
|
|
use Jerowork\RouteAttributeProvider\RouteAttributeProviderInterface; |
9
|
|
|
use League\Route\ContainerAwareInterface; |
10
|
|
|
use League\Route\ContainerAwareTrait; |
11
|
|
|
use League\Route\Router; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
use Psr\SimpleCache\CacheInterface; |
14
|
|
|
|
15
|
|
|
class LeagueRouterAttributeProvider implements RouteAttributeProviderInterface, ContainerAwareInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerAwareTrait; |
18
|
|
|
|
19
|
|
|
final public function __construct(private Router $router, ?ContainerInterface $container = null) |
20
|
|
|
{ |
21
|
|
|
if ($container instanceof ContainerInterface) { |
22
|
|
|
$this->setContainer($container); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function configure(string $className, string $methodName, Route $route): void |
30
|
|
|
{ |
31
|
|
|
foreach ($route->getMethods() as $httpMethod) { |
32
|
|
|
$leagueRoute = $this->router->map($httpMethod, $route->getPattern(), [$className, $methodName]); |
33
|
|
|
|
34
|
|
|
if (null !== ($name = $route->getName())) { |
35
|
|
|
$leagueRoute->setName($name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (null !== ($host = $route->getHost())) { |
39
|
|
|
$leagueRoute->setHost($host); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->parsePorts($leagueRoute, $route); |
43
|
|
|
$this->parseSchemes($leagueRoute, $route); |
44
|
|
|
$this->parseStrategy($leagueRoute, $route); |
45
|
|
|
$this->parseMiddlewares($leagueRoute, $route); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function parsePorts(\League\Route\Route $leagueRoute, Route $route): void |
50
|
|
|
{ |
51
|
|
|
$httpPort = $route->getHttpPort(); |
52
|
|
|
if (null !== $httpPort && in_array('http', $route->getSchemes())) { |
53
|
|
|
$leagueRoute->setPort($httpPort); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$httpsPort = $route->getHttpsPort(); |
57
|
|
|
if (null !== $httpsPort && in_array('https', $route->getSchemes())) { |
58
|
|
|
$leagueRoute->setPort($httpsPort); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function parseStrategy(\League\Route\Route $leagueRoute, Route $route): void |
63
|
|
|
{ |
64
|
|
|
$strategy = $route->getOptions()['strategy'] ?? null; |
65
|
|
|
$container = $this->getContainer(); |
66
|
|
|
if (is_string($strategy) && |
67
|
|
|
$container instanceof ContainerInterface && |
68
|
|
|
$container->has($strategy) |
69
|
|
|
) { |
70
|
|
|
$strategyInstance = $container->get($strategy); |
71
|
|
|
if ($strategyInstance instanceof ContainerAwareInterface) { |
72
|
|
|
$strategyInstance->setContainer($container); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$leagueRoute->setStrategy($strategyInstance); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function parseMiddlewares(\League\Route\Route $leagueRoute, Route $route): void |
80
|
|
|
{ |
81
|
|
|
foreach ($route->getMiddleware() as $middleware) { |
82
|
|
|
$leagueRoute->lazyMiddleware($middleware); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function parseSchemes(\League\Route\Route $leagueRoute, Route $route): void |
87
|
|
|
{ |
88
|
|
|
foreach ($route->getSchemes() as $scheme) { |
89
|
|
|
$leagueRoute->setScheme($scheme); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Helper to apply routes with the default configuration |
95
|
|
|
* |
96
|
|
|
* @param Router $router |
97
|
|
|
* @param array $directories |
98
|
|
|
* @param CacheInterface|null $cache |
99
|
|
|
* @param ContainerInterface|null $container |
100
|
|
|
*/ |
101
|
|
|
public static function apply( |
102
|
|
|
Router $router, |
103
|
|
|
array $directories, |
104
|
|
|
?CacheInterface $cache = null, |
105
|
|
|
?ContainerInterface $container = null |
106
|
|
|
): void { |
107
|
|
|
$configurator = new RouteAttributeConfigurator(new self($router, $container)); |
108
|
|
|
$configurator->addDirectory(...$directories); |
109
|
|
|
if ($cache instanceof CacheInterface) { |
110
|
|
|
$configurator->enableCache($cache); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$configurator->configure(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|