Completed
Push — development ( 1c3d48...61edbf )
by Alexander
03:31
created

Route::getParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
		$routes = $this->getConfigHandler()->getData();
43 11
		$uri    = $this->getUri();
44
45 11
		foreach ($routes as $name => $route) {
46
			/** @noinspection PhpUndefinedMethodInspection */
47 11
			if (!$route['class']::check($route['route'], $uri)) {
48 11
				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 8
				$this->params = $route['class']::getParams();
63 8
			}
64 11
		}
65
66 11
		return $this->execNotFoundHandler();
67
	}
68
69
	/**
70
	 * Safe way to get uri
71
	 *
72
	 * @return string
73
	 */
74 1
	protected function getUri()
75
	{
76 1
		$uri = parse_url(
77 1
			filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH
78 1
		);
79
80 1
		return $uri;
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 4
			return;
105
106 1
		$ajax_header = (filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH'));
107
108 1
		if ('XMLHttpRequest' === $ajax_header)
109 1
			return;
110
111 1
		throw new Exception('AJAX-route got non-AJAX request!');
112
	}
113
114
	/**
115
	 * Getting ajax-flag
116
	 *
117
	 * @throws Exception
118
	 * @return bool
119
	 */
120 5
	public function isAjax()
121
	{
122 5
		return isset($this->config['ajax']) ? true : false;
123
	}
124
125
	/**
126
	 * Build and return controller object
127
	 *
128
	 * @param Application $application
129
	 *
130
	 * @return object
131
	 * @throws Exception
132
	 */
133 2
	public function getController(Application $application)
134
	{
135 2
		if (!isset($this->config['controller'])) {
136 1
			throw new Exception('Controller name not set!');
137
		}
138
139 1
		$controller = 'Controllers\\' . $this->config['controller'];
140
141 1
		return new $controller($application);
142
	}
143
144
	/**
145
	 * Get controller method name
146
	 *
147
	 * @throws Exception
148
	 * @return string
149
	 */
150 2
	public function getActionName()
151
	{
152 2
		if (!isset($this->config['action'])) {
153 1
			throw new Exception('Action not set!');
154
		}
155
156 1
		return $this->config['action'];
157
	}
158
159
	/**
160
	 * Get View adapter class
161
	 *
162
	 * @return \Veles\View\Adapters\ViewAdapterAbstract
163
	 * @throws \Exception
164
	 */
165 2
	public function getAdapter()
166
	{
167 2
		if (!isset($this->config['view'])) {
168 1
			throw new Exception('Route adapter not set!');
169
		}
170
171
		/** @var \Veles\View\Adapters\ViewAdapterAbstract $adapter_name */
172 1
		$adapter_name = $this->config['view'];
173 1
		return $adapter_name::instance();
174
	}
175
176
	/**
177
	 * Getting route name
178
	 *
179
	 * @return string
180
	 */
181 1
	public function getName()
182
	{
183 1
		return $this->name;
184
	}
185
186
	/**
187
	 * Getting URL-params
188
	 *
189
	 * @return array
190
	 */
191 6
	public function getParams()
192
	{
193 6
		return $this->params;
194
	}
195
196
	/**
197
	 * Return template path
198
	 */
199 1
	public function getTemplate()
200
	{
201 1
		return $this->template;
202
	}
203
}
204