1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Route; |
4
|
|
|
|
5
|
|
|
use League\Container\ImmutableContainerAwareInterface; |
6
|
|
|
use League\Container\ImmutableContainerAwareTrait; |
7
|
|
|
use League\Route\Http\RequestAwareInterface; |
8
|
|
|
use League\Route\Http\ResponseAwareInterface; |
9
|
|
|
use League\Route\Middleware\MiddlewareAwareInterface; |
10
|
|
|
use League\Route\Middleware\MiddlewareAwareTrait; |
11
|
|
|
use League\Route\Strategy\StrategyAwareInterface; |
12
|
|
|
use League\Route\Strategy\StrategyAwareTrait; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
class Route implements ImmutableContainerAwareInterface, MiddlewareAwareInterface, StrategyAwareInterface |
18
|
|
|
{ |
19
|
|
|
use ImmutableContainerAwareTrait; |
20
|
|
|
use MiddlewareAwareTrait; |
21
|
|
|
use RouteConditionTrait; |
22
|
|
|
use StrategyAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|callable |
26
|
|
|
*/ |
27
|
|
|
protected $callable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \League\Route\RouteGroup |
31
|
|
|
*/ |
32
|
|
|
protected $group; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
protected $methods = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $path; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Dispatch the route via the attached strategy. |
46
|
|
|
* |
47
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
48
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
49
|
|
|
* @param array $vars |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
* |
53
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
54
|
|
|
*/ |
55
|
24 |
|
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, array $vars) |
56
|
|
|
{ |
57
|
24 |
|
$callable = $this->getCallable(); |
58
|
|
|
|
59
|
24 |
|
if (is_string($callable) && strpos($callable, '::') !== false) { |
60
|
6 |
|
$callable = explode('::', $callable); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
24 |
|
if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) { |
64
|
3 |
|
$callable = [$callable[0], $callable[1]]; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
24 |
|
if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) { |
68
|
9 |
|
$class = ($this->getContainer()->has($callable[0])) |
69
|
9 |
|
? $this->getContainer()->get($callable[0]) |
70
|
9 |
|
: new $callable[0]; |
71
|
|
|
|
72
|
9 |
|
$callable = [$class, $callable[1]]; |
73
|
9 |
|
} |
74
|
|
|
|
75
|
24 |
|
if (! is_callable($callable)) { |
76
|
3 |
|
throw new RuntimeException( |
77
|
3 |
|
sprintf( |
78
|
3 |
|
'Invalid class method provided for: %s::%s', |
79
|
3 |
|
get_class($class), |
80
|
3 |
|
$callable[1] |
81
|
3 |
|
) |
82
|
3 |
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
21 |
|
$strategy = $this->getStrategy(); |
86
|
|
|
|
87
|
21 |
|
if ($strategy instanceof RequestAwareInterface) { |
88
|
3 |
|
$strategy->setRequest($request); |
89
|
3 |
|
} |
90
|
|
|
|
91
|
21 |
|
if ($strategy instanceof ResponseAwareInterface) { |
92
|
3 |
|
$strategy->setResponse($response); |
93
|
3 |
|
} |
94
|
|
|
|
95
|
21 |
|
foreach ($this->getMiddlewareBeforeQueue() as $middleware) { |
96
|
3 |
|
$this->getMiddlewareRunner()->before($middleware); |
97
|
21 |
|
} |
98
|
|
|
|
99
|
21 |
|
foreach ($this->getMiddlewareAfterQueue() as $middleware) { |
100
|
3 |
|
$this->getMiddlewareRunner()->after($middleware); |
101
|
21 |
|
} |
102
|
|
|
|
103
|
21 |
|
return $strategy->dispatch($callable, $vars, $this); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the callable. |
108
|
|
|
* |
109
|
|
|
* @return string|callable |
110
|
|
|
*/ |
111
|
33 |
|
public function getCallable() |
112
|
|
|
{ |
113
|
33 |
|
return $this->callable; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the callable. |
118
|
|
|
* |
119
|
|
|
* @param string|callable $callable |
120
|
|
|
* |
121
|
|
|
* @return \League\Route\Route |
122
|
|
|
*/ |
123
|
42 |
|
public function setCallable($callable) |
124
|
|
|
{ |
125
|
42 |
|
$this->callable = $callable; |
126
|
|
|
|
127
|
42 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the parent group. |
132
|
|
|
* |
133
|
|
|
* @return \League\Route\RouteGroup |
134
|
|
|
*/ |
135
|
6 |
|
public function getParentGroup() |
136
|
|
|
{ |
137
|
6 |
|
return $this->group; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the parent group. |
142
|
|
|
* |
143
|
|
|
* @param \League\Route\RouteGroup $group |
144
|
|
|
* |
145
|
|
|
* @return \League\Route\Route |
146
|
|
|
*/ |
147
|
6 |
|
public function setParentGroup(RouteGroup $group) |
148
|
|
|
{ |
149
|
6 |
|
$this->group = $group; |
150
|
|
|
|
151
|
6 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the path. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
21 |
|
public function getPath() |
160
|
|
|
{ |
161
|
21 |
|
return $this->path; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set the path. |
166
|
|
|
* |
167
|
|
|
* @param string $path |
168
|
|
|
* |
169
|
|
|
* @return \League\Route\Route |
170
|
|
|
*/ |
171
|
21 |
|
public function setPath($path) |
172
|
|
|
{ |
173
|
21 |
|
$this->path = $path; |
174
|
|
|
|
175
|
21 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the methods. |
180
|
|
|
* |
181
|
|
|
* @return string[] |
182
|
|
|
*/ |
183
|
15 |
|
public function getMethods() |
184
|
|
|
{ |
185
|
15 |
|
return $this->methods; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the methods. |
190
|
|
|
* |
191
|
|
|
* @param string[] $methods |
192
|
|
|
* |
193
|
|
|
* @return \League\Route\Route |
194
|
|
|
*/ |
195
|
21 |
|
public function setMethods(array $methods) |
196
|
|
|
{ |
197
|
21 |
|
$this->methods = $methods; |
198
|
|
|
|
199
|
21 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|