|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Routing class |
|
4
|
|
|
* |
|
5
|
|
|
* @file Route.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date Сбт Июн 23 08:52:41 2012 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Routing; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use Veles\Controllers\BaseController; |
|
20
|
|
|
use Veles\Controllers\RestApiController; |
|
21
|
|
|
use Veles\Request\HttpRequestAbstract; |
|
22
|
|
|
use Veles\Request\Validator\ValidatorInterface; |
|
23
|
|
|
use Veles\View\Adapters\ViewAdapterAbstract; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Route |
|
27
|
|
|
* |
|
28
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Route extends RouteBase |
|
31
|
|
|
{ |
|
32
|
|
|
protected $name; |
|
33
|
|
|
/** @var array Current route config */ |
|
34
|
|
|
protected $config; |
|
35
|
|
|
protected $template; |
|
36
|
|
|
protected $params = []; |
|
37
|
|
|
/** @var ValidatorInterface */ |
|
38
|
|
|
protected $validator; |
|
39
|
|
|
/** @var HttpRequestAbstract */ |
|
40
|
|
|
protected $request; |
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
protected $uri = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Config parser and controller vars initialisation |
|
46
|
|
|
* |
|
47
|
|
|
* @throws Exception |
|
48
|
|
|
*/ |
|
49
|
24 |
|
public function init(): self |
|
50
|
|
|
{ |
|
51
|
24 |
|
$path = parse_url( |
|
52
|
24 |
|
filter_input(INPUT_SERVER, 'REQUEST_URI'), |
|
53
|
24 |
|
PHP_URL_PATH |
|
54
|
24 |
|
); |
|
55
|
|
|
|
|
56
|
24 |
|
if (!is_string($path)) { |
|
|
|
|
|
|
57
|
1 |
|
return $this->execNotFoundHandler(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
23 |
|
[$uri, $section] = $this->parseUri($path); |
|
61
|
23 |
|
$this->uri = $uri; |
|
62
|
23 |
|
$routes = $this->getConfigHandler()->getSection($section); |
|
63
|
|
|
|
|
64
|
23 |
|
foreach ($routes as $name => $route) { |
|
65
|
21 |
|
if (!$route['class']::check($route['route'], $uri)) { |
|
66
|
8 |
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
21 |
|
$this->process($name, $route); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
23 |
|
return $this->execNotFoundHandler(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get current URI |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function getUri(): string |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->uri; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Process route |
|
87
|
|
|
* |
|
88
|
|
|
* @param $name |
|
89
|
|
|
* @param array $config |
|
90
|
|
|
*/ |
|
91
|
21 |
|
protected function process($name, array $config): void |
|
92
|
|
|
{ |
|
93
|
21 |
|
$this->config = $config; |
|
94
|
21 |
|
$this->name = $name; |
|
95
|
|
|
|
|
96
|
21 |
|
if (isset($config['tpl'])) { |
|
97
|
18 |
|
$this->template = $config['tpl']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
21 |
|
if (RouteRegex::class === $config['class']) { |
|
101
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
102
|
10 |
|
$this->params = $config['class']::getParams(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Safe way to get uri |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $uri |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
* @codeCoverageIgnore |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function parseUri(string $uri): array |
|
115
|
|
|
{ |
|
116
|
|
|
$parts = explode('/', $uri); |
|
117
|
|
|
$section = isset($parts[2]) ? $parts[1] : ''; |
|
118
|
|
|
|
|
119
|
|
|
return [$uri, $section]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Not found exception handler |
|
124
|
|
|
* |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
24 |
|
protected function execNotFoundHandler(): self |
|
128
|
|
|
{ |
|
129
|
24 |
|
if (null === $this->config && null !== $this->ex404) { |
|
130
|
3 |
|
throw new $this->ex404; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
21 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Getting ajax-flag |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
* @throws Exception |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function isAjax(): bool |
|
143
|
|
|
{ |
|
144
|
2 |
|
return isset($this->config['ajax']); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Build and return controller object |
|
149
|
|
|
* |
|
150
|
|
|
* @return BaseController|RestApiController |
|
151
|
|
|
* @throws Exception |
|
152
|
|
|
*/ |
|
153
|
4 |
|
public function getController() |
|
154
|
|
|
{ |
|
155
|
4 |
|
if (!isset($this->config['controller'])) { |
|
156
|
1 |
|
throw new Exception('Controller name not set!'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
3 |
|
$controller = 'Controllers\\' . $this->config['controller']; |
|
160
|
|
|
|
|
161
|
3 |
|
return new $controller; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get controller method name |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
* @throws Exception |
|
169
|
|
|
*/ |
|
170
|
3 |
|
public function getActionName(): string |
|
171
|
|
|
{ |
|
172
|
3 |
|
if (!isset($this->config['action'])) { |
|
173
|
1 |
|
throw new Exception('Action not set!'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
2 |
|
return $this->config['action']; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get View adapter class |
|
181
|
|
|
* |
|
182
|
|
|
* @return ViewAdapterAbstract |
|
183
|
|
|
* @throws Exception |
|
184
|
|
|
*/ |
|
185
|
2 |
|
public function getAdapter() |
|
186
|
|
|
{ |
|
187
|
2 |
|
if (!isset($this->config['view'])) { |
|
188
|
1 |
|
throw new Exception('Route adapter not set!'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** @var ViewAdapterAbstract $adapter_name */ |
|
192
|
1 |
|
$adapter_name = $this->config['view']; |
|
193
|
|
|
|
|
194
|
1 |
|
return $adapter_name::instance(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Getting route name |
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
1 |
|
public function getName(): string |
|
203
|
|
|
{ |
|
204
|
1 |
|
return $this->name; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Getting URL-params |
|
209
|
|
|
* |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
10 |
|
public function getParams(): array |
|
213
|
|
|
{ |
|
214
|
10 |
|
return $this->params; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Return template path |
|
219
|
|
|
* |
|
220
|
|
|
* @return string|null |
|
221
|
|
|
*/ |
|
222
|
3 |
|
public function getTemplate(): ?string |
|
223
|
|
|
{ |
|
224
|
3 |
|
return $this->template; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|