Router   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDestination() 0 17 3
A add() 0 4 2
1
<?php
2
3
namespace Win\Request;
4
5
use Win\Application;
6
use Win\HttpException;
7
8
/**
9
 * Rota de URL
10
 *
11
 * Define o Controller@action a ser executado baseado na URL
12
 * @see "config/routes.php"
13
 */
14
class Router
15
{
16
	/** @var string[] */
17
	protected static $routes = [];
18
19
	/**
20
	 * Adiciona as rotas
21
	 * @param string $namespace
22
	 * @param string[] $routes
23
	 */
24
	public static function add($namespace, $routes)
25
	{
26
		foreach ($routes as $request => $destination) {
27
			static::$routes[$request] = $namespace . $destination;
28
		}
29
	}
30
31
	/**
32
	 * Percorre todas as rotas e retorna o destino final
33
	 * @return mixed Destino
34
	 * @example return [Controller, action, [..$args]]
35
	 */
36
	public static function getDestination()
37
	{
38
		$url = Url::$path;
39
		$matches = [];
40
41
		foreach (static::$routes as $request => $destination) {
42
			$pattern = '@^' . $request . '$@';
43
			$match = preg_match($pattern, $url, $matches);
44
			if ($match) {
45
				$destination = array_pad(explode('@', $destination), 2, '');
46
				$destination[] = (array) array_splice($matches, 1);
47
48
				return $destination;
49
			}
50
		}
51
52
		throw new HttpException('Route not found', 404);
53
	}
54
}
55