|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Routing class |
|
4
|
|
|
* |
|
5
|
|
|
* @file Route.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 5.4+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2015 Alexander Yancharuk <alex at itvault at info> |
|
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
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Route |
|
22
|
|
|
* |
|
23
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
24
|
|
|
* @TODO Decrease class responsibility by creating separate class for request |
|
25
|
|
|
*/ |
|
26
|
|
|
class Route extends RouteBase |
|
27
|
|
|
{ |
|
28
|
|
|
protected $page_name; |
|
29
|
|
|
/** @var array Current route config */ |
|
30
|
|
|
protected $config; |
|
31
|
|
|
protected $template; |
|
32
|
|
|
protected $params = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Config parser and controller vars initialisation |
|
36
|
|
|
* |
|
37
|
|
|
* @throws Exception |
|
38
|
|
|
*/ |
|
39
|
11 |
|
public function init() |
|
40
|
|
|
{ |
|
41
|
11 |
|
$routes = $this->getConfigHandler()->getData(); |
|
42
|
11 |
|
$uri = $this->getUri(); |
|
43
|
|
|
|
|
44
|
11 |
|
foreach ($routes as $name => $route) { |
|
45
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
46
|
11 |
|
if (!$route['class']::check($route['route'], $uri)) { |
|
47
|
11 |
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
10 |
|
$this->config = $route; |
|
51
|
10 |
|
$this->page_name = $name; |
|
52
|
|
|
|
|
53
|
10 |
|
if (isset($route['tpl'])) { |
|
54
|
7 |
|
$this->template = $route['tpl']; |
|
55
|
7 |
|
} |
|
56
|
|
|
|
|
57
|
10 |
|
$this->checkAjax(); |
|
58
|
|
|
|
|
59
|
10 |
|
if ('Veles\Routing\RouteRegex' === $route['class']) { |
|
60
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
61
|
7 |
|
$this->params = $route['class']::getParams(); |
|
62
|
7 |
|
} |
|
63
|
11 |
|
} |
|
64
|
|
|
|
|
65
|
11 |
|
return $this->execNotFoundHandler();; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Safe way to get uri |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
1 |
|
protected function getUri() |
|
74
|
|
|
{ |
|
75
|
1 |
|
$uri = parse_url( |
|
76
|
1 |
|
filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH |
|
77
|
1 |
|
); |
|
78
|
|
|
|
|
79
|
1 |
|
return $uri; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Not found exception handler |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
4 |
|
protected function execNotFoundHandler() |
|
88
|
|
|
{ |
|
89
|
4 |
|
if (null === $this->config && null !== $this->ex404) { |
|
90
|
1 |
|
throw new $this->ex404; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check for request is ajax |
|
99
|
|
|
*/ |
|
100
|
4 |
|
protected function checkAjax() |
|
101
|
|
|
{ |
|
102
|
4 |
|
if (!$this->isAjax()) |
|
103
|
4 |
|
return; |
|
104
|
|
|
|
|
105
|
1 |
|
$ajax_header = (filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')); |
|
106
|
|
|
|
|
107
|
1 |
|
if ('XMLHttpRequest' === $ajax_header) |
|
108
|
1 |
|
return; |
|
109
|
|
|
|
|
110
|
1 |
|
throw new Exception('AJAX-route got non-AJAX request!'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Getting ajax-flag |
|
115
|
|
|
* |
|
116
|
|
|
* @throws Exception |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
5 |
|
public function isAjax() |
|
120
|
|
|
{ |
|
121
|
5 |
|
return isset($this->config['ajax']) ? true : false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Access to controller |
|
126
|
|
|
* |
|
127
|
|
|
* @throws Exception |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
2 |
|
public function getController() |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
2 |
|
if (!isset($this->config['controller'])) { |
|
133
|
1 |
|
throw new Exception('Не указан контроллер!'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
$controller = 'Controllers\\' . $this->config['controller']; |
|
137
|
|
|
|
|
138
|
1 |
|
return new $controller($this); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get controller method name |
|
143
|
|
|
* |
|
144
|
|
|
* @throws Exception |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
2 |
|
public function getActionName() |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
2 |
|
if (!isset($this->config['action'])) { |
|
150
|
1 |
|
throw new Exception('Не указан экшен!'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
return $this->config['action']; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get View adapter class |
|
158
|
|
|
* |
|
159
|
|
|
* @return \Veles\View\Adapters\ViewAdapterAbstract |
|
160
|
|
|
* @throws \Exception |
|
161
|
|
|
*/ |
|
162
|
2 |
|
public function getAdapter() |
|
163
|
|
|
{ |
|
164
|
2 |
|
if (!isset($this->config['view'])) { |
|
165
|
1 |
|
throw new Exception('Не указан адаптер!'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** @var \Veles\View\Adapters\ViewAdapterAbstract $adapter_name */ |
|
169
|
1 |
|
$adapter_name = $this->config['view']; |
|
170
|
1 |
|
return $adapter_name::instance(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Getting page name |
|
175
|
|
|
* |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function getPageName() |
|
179
|
|
|
{ |
|
180
|
1 |
|
return $this->page_name; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Getting URL-params |
|
185
|
|
|
* |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
6 |
|
public function getParams() |
|
189
|
|
|
{ |
|
190
|
6 |
|
return $this->params; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Return template path |
|
195
|
|
|
*/ |
|
196
|
1 |
|
public function getTemplate() |
|
197
|
|
|
{ |
|
198
|
1 |
|
return $this->template; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.