1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Route; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; |
7
|
|
|
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait}; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
10
|
|
|
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
11
|
|
|
|
12
|
|
|
class Route implements |
13
|
|
|
MiddlewareInterface, |
14
|
|
|
MiddlewareAwareInterface, |
15
|
|
|
RouteConditionHandlerInterface, |
16
|
|
|
StrategyAwareInterface |
17
|
|
|
{ |
18
|
|
|
use MiddlewareAwareTrait; |
19
|
|
|
use RouteConditionHandlerTrait; |
20
|
|
|
use StrategyAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var callable|string |
24
|
|
|
*/ |
25
|
|
|
protected $handler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RouteGroup |
29
|
|
|
*/ |
30
|
|
|
protected $group; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $method; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $path; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $vars = []; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Construct. |
50
|
|
|
* |
51
|
|
|
* @param string $method |
52
|
|
|
* @param string $path |
53
|
|
|
* @param callable|string $handler |
54
|
|
|
*/ |
55
|
72 |
|
public function __construct(string $method, string $path, $handler) |
56
|
|
|
{ |
57
|
72 |
|
$this->method = $method; |
58
|
72 |
|
$this->path = $path; |
59
|
72 |
|
$this->handler = $handler; |
60
|
72 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
24 |
|
public function process( |
66
|
|
|
ServerRequestInterface $request, |
67
|
|
|
RequestHandlerInterface $requestHandler |
68
|
|
|
): ResponseInterface { |
69
|
24 |
|
return $this->getStrategy()->invokeRouteCallable($this, $request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the controller callable |
74
|
|
|
* |
75
|
|
|
* @param ContainerInterface|null $container |
76
|
|
|
* |
77
|
|
|
* @return callable |
78
|
|
|
* |
79
|
|
|
* @throws InvalidArgumentException |
80
|
|
|
*/ |
81
|
45 |
|
public function getCallable(?ContainerInterface $container = null): callable |
82
|
|
|
{ |
83
|
45 |
|
$callable = $this->handler; |
84
|
|
|
|
85
|
45 |
|
if (is_string($callable) && strpos($callable, '::') !== false) { |
86
|
6 |
|
$callable = explode('::', $callable); |
87
|
|
|
} |
88
|
|
|
|
89
|
45 |
|
if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) { |
90
|
3 |
|
$callable = [$callable[0], $callable[1]]; |
91
|
|
|
} |
92
|
|
|
|
93
|
45 |
|
if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) { |
94
|
6 |
|
$callable = [$this->resolveClass($container, $callable[0]), $callable[1]]; |
95
|
|
|
} |
96
|
|
|
|
97
|
45 |
|
if (is_string($callable) && method_exists($callable, '__invoke')) { |
98
|
|
|
$callable = $this->resolveClass($container, $callable); |
99
|
|
|
} |
100
|
|
|
|
101
|
45 |
|
if (! is_callable($callable)) { |
102
|
3 |
|
throw new InvalidArgumentException('Could not resolve a callable for this route'); |
103
|
|
|
} |
104
|
|
|
|
105
|
42 |
|
return $callable; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get an object instance from a class name |
110
|
|
|
* |
111
|
|
|
* @param ContainerInterface|null $container |
112
|
|
|
* @param string $class |
113
|
|
|
* |
114
|
|
|
* @return object |
115
|
|
|
*/ |
116
|
6 |
|
protected function resolveClass(?ContainerInterface $container = null, string $class) |
117
|
|
|
{ |
118
|
6 |
|
if ($container instanceof ContainerInterface && $container->has($class)) { |
119
|
3 |
|
return $container->get($class); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
return new $class(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return variables to be passed to route callable |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
27 |
|
public function getVars(): array |
131
|
|
|
{ |
132
|
27 |
|
return $this->vars; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set variables to be passed to route callable |
137
|
|
|
* |
138
|
|
|
* @param array $vars |
139
|
|
|
* |
140
|
|
|
* @return Route |
141
|
|
|
*/ |
142
|
27 |
|
public function setVars(array $vars): self |
143
|
|
|
{ |
144
|
27 |
|
$this->vars = $vars; |
145
|
|
|
|
146
|
27 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the parent group |
151
|
|
|
* |
152
|
|
|
* @return RouteGroup |
153
|
|
|
*/ |
154
|
27 |
|
public function getParentGroup(): ?RouteGroup |
155
|
|
|
{ |
156
|
27 |
|
return $this->group; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set the parent group |
161
|
|
|
* |
162
|
|
|
* @param RouteGroup $group |
163
|
|
|
* |
164
|
|
|
* @return Route |
165
|
|
|
*/ |
166
|
12 |
|
public function setParentGroup(RouteGroup $group): self |
167
|
|
|
{ |
168
|
12 |
|
$this->group = $group; |
169
|
12 |
|
$prefix = $this->group->getPrefix(); |
170
|
12 |
|
$path = $this->getPath(); |
171
|
|
|
|
172
|
12 |
|
if (strcmp($prefix, substr($path, 0, strlen($prefix))) !== 0) { |
173
|
|
|
$path = $prefix . $path; |
174
|
|
|
$this->path = $path; |
175
|
|
|
} |
176
|
|
|
|
177
|
12 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the path |
182
|
|
|
* |
183
|
|
|
* @param array $replacements |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
39 |
|
public function getPath(array $replacements = []): string |
188
|
|
|
{ |
189
|
39 |
|
$toReplace = []; |
190
|
|
|
|
191
|
39 |
|
foreach ($replacements as $wildcard => $actual) { |
192
|
3 |
|
$toReplace['/{' . preg_quote($wildcard, '/') . '(:.*?)?}/'] = $actual; |
193
|
|
|
} |
194
|
|
|
|
195
|
39 |
|
return preg_replace(array_keys($toReplace), array_values($toReplace), $this->path); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the HTTP method |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
36 |
|
public function getMethod(): string |
204
|
|
|
{ |
205
|
36 |
|
return $this->method; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|