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 \League\Route\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
|
44 |
|
public function __construct(string $method, string $path, $handler) |
48
|
|
|
{ |
49
|
44 |
|
$this->method = $method; |
50
|
44 |
|
$this->path = $path; |
51
|
44 |
|
$this->handler = $handler; |
52
|
44 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
14 |
|
public function process( |
58
|
|
|
ServerRequestInterface $request, |
59
|
|
|
RequestHandlerInterface $requestHandler |
60
|
|
|
) : ResponseInterface { |
61
|
14 |
|
return $this->getStrategy()->invokeRouteCallable($this, $request); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the controller callable |
66
|
|
|
* |
67
|
|
|
* @param \Psr\Container\ContainerInterface|null $container |
68
|
|
|
* |
69
|
|
|
* @throws \InvalidArgumentException |
70
|
|
|
* |
71
|
|
|
* @return callable |
72
|
|
|
*/ |
73
|
28 |
|
public function getCallable(?ContainerInterface $container = null) : callable |
74
|
|
|
{ |
75
|
28 |
|
$callable = $this->handler; |
76
|
|
|
|
77
|
28 |
|
if (is_string($callable) && strpos($callable, '::') !== false) { |
78
|
4 |
|
$callable = explode('::', $callable); |
79
|
|
|
} |
80
|
|
|
|
81
|
28 |
|
if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) { |
82
|
2 |
|
$callable = [$callable[0], $callable[1]]; |
83
|
|
|
} |
84
|
|
|
|
85
|
28 |
|
if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) { |
86
|
4 |
|
$callable = [$this->resolveClass($container, $callable[0]), $callable[1]]; |
87
|
2 |
|
} |
88
|
4 |
|
|
89
|
|
|
if (is_string($callable) && method_exists($callable, '__invoke')) { |
90
|
|
|
$callable = $this->resolveClass($container, $callable); |
91
|
4 |
|
} |
92
|
|
|
|
93
|
|
|
if (! is_callable($callable)) { |
94
|
28 |
|
throw new InvalidArgumentException('Could not resolve a callable for this route'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $callable; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
28 |
|
* Get an object instance from a class name |
102
|
2 |
|
* |
103
|
|
|
* @param \Psr\Container\ContainerInterface|null $container |
104
|
|
|
* @param string $class |
105
|
26 |
|
* |
106
|
|
|
* @return object |
107
|
|
|
*/ |
108
|
|
|
protected function resolveClass(?ContainerInterface $container = null, string $class) |
109
|
|
|
{ |
110
|
|
|
if ($container instanceof ContainerInterface && $container->has($class)) { |
111
|
|
|
return $container->get($class); |
112
|
|
|
} |
113
|
16 |
|
|
114
|
|
|
return new $class(); |
115
|
16 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return variables to be passed to route callable |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getVars() : array |
123
|
|
|
{ |
124
|
|
|
return $this->vars; |
125
|
16 |
|
} |
126
|
|
|
|
127
|
16 |
|
/** |
128
|
|
|
* Set variables to be passed to route callable |
129
|
16 |
|
* |
130
|
|
|
* @param array $vars |
131
|
|
|
* |
132
|
|
|
* @return \League\Route\Route |
133
|
|
|
*/ |
134
|
|
|
public function setVars(array $vars) : self |
135
|
|
|
{ |
136
|
|
|
$this->vars = $vars; |
137
|
16 |
|
|
138
|
|
|
return $this; |
139
|
16 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the parent group |
143
|
|
|
* |
144
|
|
|
* @return \League\Route\RouteGroup |
145
|
|
|
*/ |
146
|
|
|
public function getParentGroup() : ?RouteGroup |
147
|
|
|
{ |
148
|
|
|
return $this->group; |
149
|
8 |
|
} |
150
|
|
|
|
151
|
8 |
|
/** |
152
|
|
|
* Set the parent group |
153
|
8 |
|
* |
154
|
|
|
* @param \League\Route\RouteGroup $group |
155
|
|
|
* |
156
|
|
|
* @return \League\Route\Route |
157
|
|
|
*/ |
158
|
|
|
public function setParentGroup(RouteGroup $group) : self |
159
|
|
|
{ |
160
|
|
|
$this->group = $group; |
161
|
24 |
|
|
162
|
|
|
return $this; |
163
|
24 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the path |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function getPath() : string |
171
|
24 |
|
{ |
172
|
|
|
return $this->path; |
173
|
24 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the HTTP method |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getMethod() : string |
181
|
|
|
{ |
182
|
|
|
return $this->method; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|