|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Component\Http; |
|
4
|
|
|
|
|
5
|
|
|
use App\Component\Http\Interfaces\IRoutes; |
|
6
|
|
|
use App\Component\Http\Route; |
|
7
|
|
|
|
|
8
|
|
|
class Routes implements IRoutes |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* routes config collection |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $routesConfig = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* route list as array |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $routes = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* __construct |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $routesConfig |
|
29
|
|
|
* @return Routes |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public function __construct(array $routesConfig = []) |
|
32
|
|
|
{ |
|
33
|
4 |
|
if (!empty($routesConfig)) { |
|
34
|
4 |
|
$this->set($routesConfig); |
|
35
|
|
|
} |
|
36
|
4 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* returns routes as array |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function get(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->routes; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* returns routes as array |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function getExpr(): array |
|
55
|
|
|
{ |
|
56
|
2 |
|
$patterns = array_map( |
|
57
|
2 |
|
function (Route $i) { |
|
58
|
2 |
|
return $i->getExpr(); |
|
59
|
2 |
|
}, |
|
60
|
2 |
|
$this->routes |
|
61
|
|
|
); |
|
62
|
2 |
|
return $patterns; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* set routes as array and stack Route collection |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $routesConfig |
|
69
|
|
|
* @return Routes |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function set(array $routesConfig): Routes |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->routes = []; |
|
74
|
4 |
|
$this->routesConfig = $routesConfig; |
|
75
|
4 |
|
$this->prepare(); |
|
76
|
4 |
|
$this->validate(); |
|
77
|
4 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* validate routes to be an array of regexp string |
|
82
|
|
|
* |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function prepare() |
|
86
|
|
|
{ |
|
87
|
|
|
$count = count($this->routesConfig); |
|
88
|
|
|
for ($c = 0; $c < $count; $c++) { |
|
89
|
|
|
$this->routes[] = new Route($this->routesConfig[$c]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* validate routes to be an array of regexp string |
|
95
|
|
|
* |
|
96
|
|
|
* @throws Exception |
|
97
|
|
|
*/ |
|
98
|
2 |
|
protected function validate() |
|
99
|
|
|
{ |
|
100
|
2 |
|
$count = count($this->routes); |
|
101
|
2 |
|
for ($c = 0; $c < $count; $c++) { |
|
102
|
2 |
|
$route = $this->routes[$c]->getExpr(); |
|
103
|
2 |
|
if (@preg_match($route, null) === false) { |
|
104
|
1 |
|
throw new \Exception('Route invalid expr ' . $route); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|