1
|
|
|
<?php |
2
|
|
|
namespace Acelaya\Expressive\Router; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
5
|
|
|
use Slim\Router; |
6
|
|
|
use Zend\Expressive\Router\Exception; |
7
|
|
|
use Zend\Expressive\Router\Route; |
8
|
|
|
use Zend\Expressive\Router\RouteResult; |
9
|
|
|
use Zend\Expressive\Router\RouterInterface; |
10
|
|
|
|
11
|
|
|
class SlimRouter implements RouterInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Router |
15
|
|
|
*/ |
16
|
|
|
private $router; |
17
|
|
|
/** |
18
|
|
|
* @var Route[] |
19
|
|
|
*/ |
20
|
|
|
private $routes; |
21
|
|
|
|
22
|
9 |
|
public function __construct(Router $router = null) |
23
|
|
|
{ |
24
|
9 |
|
if (null === $router) { |
25
|
9 |
|
$router = $this->createRouter(); |
26
|
|
|
} |
27
|
|
|
|
28
|
9 |
|
$this->router = $router; |
29
|
9 |
|
$this->routes = []; |
30
|
9 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a default Aura router instance |
34
|
|
|
* |
35
|
|
|
* @return Router |
36
|
|
|
*/ |
37
|
9 |
|
private function createRouter() |
38
|
|
|
{ |
39
|
9 |
|
return new Router(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Route $route |
44
|
|
|
*/ |
45
|
6 |
|
public function addRoute(Route $route): void |
46
|
|
|
{ |
47
|
6 |
|
$this->routes[] = $route; |
48
|
6 |
|
} |
49
|
|
|
|
50
|
1 |
|
public function dummyCallable() |
51
|
|
|
{ |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @return RouteResult |
57
|
|
|
*/ |
58
|
2 |
|
public function match(Request $request): RouteResult |
59
|
|
|
{ |
60
|
2 |
|
$this->injectRoutes(); |
61
|
|
|
|
62
|
2 |
|
$matchedRoutes = $this->router->getMatchedRoutes($request->getMethod(), $request->getUri()->getPath()); |
63
|
2 |
|
if (count($matchedRoutes) === 0) { |
64
|
1 |
|
return RouteResult::fromRouteFailure(null); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @var \Slim\Route $matchedRoute */ |
68
|
1 |
|
$matchedRoute = array_shift($matchedRoutes); |
69
|
1 |
|
$params = $matchedRoute->getParams(); |
70
|
|
|
|
71
|
|
|
// Get the middleware from the route params and remove it |
72
|
1 |
|
$middleware = $params['middleware']; |
73
|
1 |
|
unset($params['middleware']); |
74
|
|
|
|
75
|
1 |
|
$route = new Route( |
76
|
1 |
|
$matchedRoute->getPattern(), |
77
|
1 |
|
$middleware, |
78
|
1 |
|
$matchedRoute->getHttpMethods(), |
79
|
1 |
|
$matchedRoute->getName() |
80
|
|
|
); |
81
|
1 |
|
return RouteResult::fromRoute($route, $params); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generate a URI from the named route. |
86
|
|
|
* |
87
|
|
|
* Takes the named route and any substitutions, and attempts to generate a |
88
|
|
|
* URI from it. Additional router-dependent options may be passed. |
89
|
|
|
* |
90
|
|
|
* The URI generated MUST NOT be escaped. If you wish to escape any part of |
91
|
|
|
* the URI, this should be performed afterwards; consider passing the URI |
92
|
|
|
* to league/uri to encode it. |
93
|
|
|
* |
94
|
|
|
* @see https://github.com/auraphp/Aura.Router#generating-a-route-path |
95
|
|
|
* @see http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html |
96
|
|
|
* @param string $name |
97
|
|
|
* @param array $substitutions |
98
|
|
|
* @param array $options |
99
|
|
|
* @return string |
100
|
|
|
* @throws Exception\RuntimeException if unable to generate the given URI. |
101
|
|
|
*/ |
102
|
2 |
|
public function generateUri(string $name, array $substitutions = [], array $options = []): string |
103
|
|
|
{ |
104
|
2 |
|
$this->injectRoutes(); |
105
|
|
|
|
106
|
2 |
|
if (! $this->router->hasNamedRoute($name)) { |
107
|
1 |
|
throw new Exception\RuntimeException(sprintf( |
108
|
1 |
|
'Cannot generate URI based on route "%s"; route not found', |
109
|
1 |
|
$name |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return $this->router->urlFor($name, $substitutions); |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
private function injectRoutes(): void |
117
|
|
|
{ |
118
|
7 |
|
foreach ($this->routes as $key => $route) { |
119
|
6 |
|
$this->injectRoute($route); |
120
|
6 |
|
unset($this->routes[$key]); |
121
|
|
|
} |
122
|
7 |
|
} |
123
|
|
|
|
124
|
6 |
|
private function injectRoute(Route $route): void |
125
|
|
|
{ |
126
|
6 |
|
$slimRoute = new \Slim\Route($route->getPath(), [$this, 'dummyCallable']); |
127
|
6 |
|
$slimRoute->setName($route->getName()); |
128
|
|
|
|
129
|
6 |
|
$allowedMethods = $route->getAllowedMethods(); |
130
|
6 |
|
$slimRoute->via($allowedMethods === Route::HTTP_METHOD_ANY ? 'ANY' : $allowedMethods); |
131
|
|
|
|
132
|
|
|
// Process options |
133
|
6 |
|
$options = $route->getOptions(); |
134
|
6 |
|
if (isset($options['conditions']) && is_array($options['conditions'])) { |
135
|
1 |
|
$slimRoute->setConditions($options['conditions']); |
136
|
|
|
} |
137
|
|
|
// The middleware is merged with the rest of the route params |
138
|
|
|
$params = [ |
139
|
6 |
|
'middleware' => $route->getMiddleware() |
140
|
|
|
]; |
141
|
6 |
|
if (isset($options['defaults']) && is_array($options['defaults'])) { |
142
|
1 |
|
$params = array_merge($options['defaults'], $params); |
143
|
|
|
} |
144
|
6 |
|
$slimRoute->setParams($params); |
145
|
|
|
|
146
|
6 |
|
$this->router->map($slimRoute); |
147
|
6 |
|
} |
148
|
|
|
} |
149
|
|
|
|