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