Completed
Push — development ( 82cfa5...f93c97 )
by Alexander
03:15
created

Route::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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