Completed
Push — master ( 0a5bf7...5f5ea6 )
by Alexander
03:45
created

Route::parseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 4
nop 0
crap 2
1
<?php
2
/**
3
 * Routing class
4
 *
5
 * @file      Route.php
6
 *
7
 * PHP version 5.6+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2016 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\Application\Application;
20
21
/**
22
 * Class Route
23
 *
24
 * @author  Alexander Yancharuk <alex at itvault dot info>
25
 * @TODO Decrease class responsibility by creating separate class for request
26
 */
27
class Route extends RouteBase
28
{
29
	protected $name;
30
	/** @var  array Current route config */
31
	protected $config;
32
	protected $template;
33
	protected $params = [];
34
35
	/**
36
	 * Config parser and controller vars initialisation
37
	 *
38
	 * @throws Exception
39
	 */
40 11
	public function init()
41
	{
42 11
		list($uri, $section) = $this->parseUri();
43 11
		$routes = $this->getConfigHandler()->getSection($section);
44
45 11
		foreach ($routes as $name => $route) {
46
			/** @noinspection PhpUndefinedMethodInspection */
47 10
			if (!$route['class']::check($route['route'], $uri)) {
48 2
				continue;
49
			}
50
51 10
			$this->config = $route;
52 10
			$this->name   = $name;
53
54 10
			if (isset($route['tpl'])) {
55 8
				$this->template = $route['tpl'];
56 8
			}
57
58 10
			$this->checkAjax();
59
60 10
			if ('Veles\Routing\RouteRegex' === $route['class']) {
61
				/** @noinspection PhpUndefinedMethodInspection */
62 6
				$this->params = $route['class']::getParams();
63 6
			}
64 11
		}
65
66 11
		return $this->execNotFoundHandler();
67
	}
68
69
	/**
70
	 * Safe way to get uri
71
	 *
72
	 * @return array
73
	 */
74
	protected function parseUri()
75
	{
76
		$uri = parse_url(
77
			filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH
78
		);
79
80
		return [$uri, explode('/', $uri)[1]];
81
	}
82
83
	/**
84
	 * Not found exception handler
85
	 *
86
	 * @return $this
87
	 */
88 4
	protected function execNotFoundHandler()
89
	{
90 4
		if (null === $this->config && null !== $this->ex404) {
91 1
			throw new $this->ex404;
92
		}
93
94 3
		return $this;
95
	}
96
97
98
	/**
99
	 * Check for request is ajax
100
	 */
101 4
	protected function checkAjax()
102
	{
103 4
		if (!$this->isAjax()) {
104 3
			return;
105
		}
106
107 1
		$ajax_header = (filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH'));
108
109 1
		if ('XMLHttpRequest' === $ajax_header) {
110
			return;
111
		}
112
113 1
		throw new Exception('AJAX-route got non-AJAX request!');
114
	}
115
116
	/**
117
	 * Getting ajax-flag
118
	 *
119
	 * @throws Exception
120
	 * @return bool
121
	 */
122 5
	public function isAjax()
123
	{
124 5
		return isset($this->config['ajax']) ? true : false;
125
	}
126
127
	/**
128
	 * Build and return controller object
129
	 *
130
	 * @param Application $application
131
	 *
132
	 * @return object
133
	 * @throws Exception
134
	 */
135 2
	public function getController(Application $application)
136
	{
137 2
		if (!isset($this->config['controller'])) {
138 1
			throw new Exception('Controller name not set!');
139
		}
140
141 1
		$controller = 'Controllers\\' . $this->config['controller'];
142
143 1
		return new $controller($application);
144
	}
145
146
	/**
147
	 * Get controller method name
148
	 *
149
	 * @throws Exception
150
	 * @return string
151
	 */
152 2
	public function getActionName()
153
	{
154 2
		if (!isset($this->config['action'])) {
155 1
			throw new Exception('Action not set!');
156
		}
157
158 1
		return $this->config['action'];
159
	}
160
161
	/**
162
	 * Get View adapter class
163
	 *
164
	 * @return \Veles\View\Adapters\ViewAdapterAbstract
165
	 * @throws \Exception
166
	 */
167 2
	public function getAdapter()
168
	{
169 2
		if (!isset($this->config['view'])) {
170 1
			throw new Exception('Route adapter not set!');
171
		}
172
173
		/** @var \Veles\View\Adapters\ViewAdapterAbstract $adapter_name */
174 1
		$adapter_name = $this->config['view'];
175 1
		return $adapter_name::instance();
176
	}
177
178
	/**
179
	 * Getting route name
180
	 *
181
	 * @return string
182
	 */
183 1
	public function getName()
184
	{
185 1
		return $this->name;
186
	}
187
188
	/**
189
	 * Getting URL-params
190
	 *
191
	 * @return array
192
	 */
193 6
	public function getParams()
194
	{
195 6
		return $this->params;
196
	}
197
198
	/**
199
	 * Return template path
200
	 */
201 1
	public function getTemplate()
202
	{
203 1
		return $this->template;
204
	}
205
}
206