|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Router\Rules\PatternRule; |
|
6
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
7
|
|
|
use Bavix\Exceptions; |
|
8
|
|
|
|
|
9
|
|
|
class Router |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var PatternRule[] |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $routes; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var iterable |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CacheItemPoolInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $pool; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Router constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param iterable $data |
|
31
|
|
|
* @param CacheItemPoolInterface $pool |
|
32
|
|
|
*/ |
|
33
|
5 |
|
public function __construct($data, CacheItemPoolInterface $pool = null) |
|
34
|
|
|
{ |
|
35
|
5 |
|
$this->config = $data; |
|
36
|
5 |
|
$this->pool = $pool; |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return Route |
|
41
|
|
|
* |
|
42
|
|
|
* @throws Exceptions\NotFound\Data |
|
43
|
|
|
* @throws Exceptions\NotFound\Path |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getCurrentRoute(): Route |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->find(Server::sharedInstance()->path()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $path |
|
52
|
|
|
* @param string $host |
|
53
|
|
|
* @param string $protocol |
|
54
|
|
|
* |
|
55
|
|
|
* @return Route |
|
56
|
|
|
* @throws Exceptions\NotFound\Data |
|
57
|
|
|
* @throws Exceptions\NotFound\Path |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function getRoute(string $path, string $host = null, string $protocol = null): Route |
|
60
|
|
|
{ |
|
61
|
3 |
|
return $this->find(Build::url($path, $host, $protocol)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function hash(): string |
|
68
|
|
|
{ |
|
69
|
|
|
return \sha1(\json_encode($this->config)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array |
|
74
|
|
|
* @throws |
|
75
|
|
|
*/ |
|
76
|
5 |
|
protected function loadingRoutes(): array |
|
77
|
|
|
{ |
|
78
|
5 |
|
$loader = new Loader($this->config); |
|
79
|
5 |
|
$this->routes = $loader->simplify(); |
|
80
|
|
|
|
|
81
|
3 |
|
if ($this->pool) { |
|
82
|
|
|
$item = $this->pool->getItem($this->hash()); |
|
83
|
|
|
$item->set($this->routes); |
|
84
|
|
|
$this->pool->save($item); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
return $this->routes; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return array |
|
92
|
|
|
* @throws |
|
93
|
|
|
*/ |
|
94
|
5 |
|
protected function bootRoutes(): array |
|
95
|
|
|
{ |
|
96
|
5 |
|
if ($this->pool) { |
|
97
|
|
|
$item = $this->pool->getItem($this->hash()); |
|
98
|
|
|
$data = $item->get(); |
|
99
|
|
|
if ($data) { |
|
100
|
|
|
return $data; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
5 |
|
return $this->loadingRoutes(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return Route[] |
|
109
|
|
|
*/ |
|
110
|
5 |
|
public function routes(): array |
|
111
|
|
|
{ |
|
112
|
5 |
|
if (empty($this->routes)) |
|
113
|
|
|
{ |
|
114
|
5 |
|
$this->routes = $this->bootRoutes(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
return $this->routes; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $path |
|
122
|
|
|
* |
|
123
|
|
|
* @return Route |
|
124
|
|
|
* |
|
125
|
|
|
* @throws Exceptions\NotFound\Path |
|
126
|
|
|
*/ |
|
127
|
|
|
public function route(string $path): Route |
|
128
|
|
|
{ |
|
129
|
|
|
$routes = $this->routes(); |
|
130
|
|
|
|
|
131
|
|
|
if (empty($routes[$path])) |
|
132
|
|
|
{ |
|
133
|
|
|
throw new Exceptions\NotFound\Path('Route `' . $path . '` not found'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $routes[$path]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $subject |
|
141
|
|
|
* @return Route |
|
142
|
|
|
*/ |
|
143
|
3 |
|
protected function find(string $subject): Route |
|
144
|
|
|
{ |
|
145
|
3 |
|
foreach ($this->routes() as $name => $patternRule) { |
|
146
|
3 |
|
$match = new Match($patternRule, $subject, Server::sharedInstance()->method()); |
|
147
|
3 |
|
if ($match->isTest()) { |
|
148
|
3 |
|
return new Route($match); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
throw new Exceptions\NotFound\Page('Page `' . $subject . '` not found', 404); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|