1 | <?php |
||
14 | class Route implements |
||
15 | MiddlewareInterface, |
||
16 | MiddlewareAwareInterface, |
||
17 | RouteConditionHandlerInterface, |
||
18 | StrategyAwareInterface |
||
19 | { |
||
20 | use MiddlewareAwareTrait; |
||
21 | use RouteConditionHandlerTrait; |
||
22 | use StrategyAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * @var callable|string |
||
26 | */ |
||
27 | protected $handler; |
||
28 | |||
29 | /** |
||
30 | * @var RouteGroup |
||
31 | */ |
||
32 | protected $group; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $method; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $path; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $vars = []; |
||
48 | |||
49 | 78 | public function __construct(string $method, string $path, $handler) |
|
50 | { |
||
51 | 78 | $this->method = $method; |
|
52 | 78 | $this->path = $path; |
|
53 | 78 | $this->handler = $handler; |
|
54 | 78 | } |
|
55 | |||
56 | 48 | public function getCallable(?ContainerInterface $container = null): callable |
|
57 | { |
||
58 | 48 | $callable = $this->handler; |
|
59 | |||
60 | 48 | if (is_string($callable) && strpos($callable, '::') !== false) { |
|
61 | 6 | $callable = explode('::', $callable); |
|
62 | } |
||
63 | |||
64 | 48 | if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) { |
|
65 | 3 | $callable = [$callable[0], $callable[1]]; |
|
66 | } |
||
67 | |||
68 | 48 | if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) { |
|
69 | 6 | $callable = [$this->resolve($callable[0], $container), $callable[1]]; |
|
70 | } |
||
71 | |||
72 | 48 | if (is_string($callable)) { |
|
73 | 3 | $callable = $this->resolve($callable, $container); |
|
74 | } |
||
75 | |||
76 | 48 | if (!is_callable($callable)) { |
|
77 | 3 | throw new RuntimeException('Could not resolve a callable for this route'); |
|
78 | } |
||
79 | |||
80 | 45 | return $callable; |
|
81 | } |
||
82 | |||
83 | 51 | public function getMethod(): string |
|
84 | { |
||
85 | 51 | return $this->method; |
|
86 | } |
||
87 | |||
88 | 30 | public function getParentGroup(): ?RouteGroup |
|
92 | |||
93 | 54 | public function getPath(array $replacements = []): string |
|
94 | { |
||
95 | 54 | $toReplace = []; |
|
103 | |||
104 | 30 | public function getVars(): array |
|
108 | |||
109 | 30 | public function process( |
|
121 | |||
122 | 12 | public function setParentGroup(RouteGroup $group): self |
|
135 | |||
136 | 39 | public function setVars(array $vars): self |
|
141 | |||
142 | 9 | protected function resolve(string $class, ?ContainerInterface $container = null) |
|
154 | } |
||
155 |