|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Scrawler package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Pranjal Pandey <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Scrawler\Router; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Collection of all available controllers. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class RouteCollection |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Stores all controller and corrosponding route. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array<mixed> |
|
25
|
|
|
*/ |
|
26
|
|
|
private array $controllers = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Stores all manual route. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array<mixed> |
|
32
|
|
|
*/ |
|
33
|
|
|
private array $route = []; |
|
34
|
|
|
|
|
35
|
|
|
/* |
|
36
|
|
|
* Stores the path of dirctory containing controllers |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private string $directory; |
|
40
|
|
|
|
|
41
|
|
|
/* |
|
42
|
|
|
* Stores the namespace of controllers |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private string $namespace; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Stores list of directories. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array<mixed> |
|
51
|
|
|
*/ |
|
52
|
|
|
private array $dir = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Stores caching engine. |
|
56
|
|
|
*/ |
|
57
|
|
|
private \Psr\SimpleCache\CacheInterface $cache; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check if caches is enable. |
|
61
|
|
|
*/ |
|
62
|
|
|
private bool $enableCache = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check if auto register is enable. |
|
66
|
|
|
*/ |
|
67
|
|
|
private bool $autoRegistered = false; |
|
68
|
|
|
|
|
69
|
|
|
public function register(string $directory, string $namespace): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->autoRegistered = true; |
|
72
|
|
|
$this->directory = $directory; |
|
73
|
|
|
$this->namespace = $namespace; |
|
74
|
|
|
|
|
75
|
|
|
$this->autoRegister(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Function returns the class of corrosponding controller. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getController(string $controller): false|string |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->enableCache && $this->cache->has(str_replace('/', '_', $controller))) { |
|
84
|
|
|
return $this->cache->get(str_replace('/', '_', $controller)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
foreach ($this->controllers as $key => $value) { |
|
88
|
|
|
if ($key === $controller) { |
|
89
|
|
|
return $value; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns cache engine. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getCache(): \Psr\SimpleCache\CacheInterface |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->cache; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Register controller with route collection. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function registerController(string $name, string $class): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->controllers[$name] = $class; |
|
110
|
|
|
if ($this->enableCache) { |
|
111
|
|
|
$this->cache->set(str_replace('/', '_', $name), $class); |
|
112
|
|
|
$this->cache->set('collection', $this->controllers); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Automatically register all controllers in specified directory. |
|
118
|
|
|
*/ |
|
119
|
|
|
private function autoRegister(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$files = array_slice(\Safe\scandir($this->directory), 2); |
|
122
|
|
|
foreach ($files as $file) { |
|
123
|
|
|
if (is_dir($this->directory.'/'.$file)) { |
|
124
|
|
|
$this->registerDir($file); |
|
125
|
|
|
$dir = $this->directory.'/'.$file; |
|
126
|
|
|
$dir_files = array_slice(\Safe\scandir($dir), 2); |
|
127
|
|
|
foreach ($dir_files as $dir_file) { |
|
128
|
|
|
if ('Main.php' !== $dir_file && !\is_dir($dir.'/'.$dir_file)) { |
|
129
|
|
|
$this->registerController($file.'/'.\basename((string) $dir_file, '.php'), $this->namespace.'\\'.\ucfirst((string) $file).'\\'.\basename((string) $dir_file, '.php')); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
if ('Main.php' !== $file && !\is_dir($this->directory.'/'.$file)) { |
|
134
|
|
|
$this->registerController(\basename((string) $file, '.php'), $this->namespace.'\\'.\basename((string) $file, '.php')); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Function to get the namespace of controllers. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getNamespace(): string |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->namespace; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Function to return list of all controller currently registerd with route collction. |
|
149
|
|
|
* |
|
150
|
|
|
* @return array<mixed> |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getControllers(): array |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->controllers; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Function to add dir to list of dir. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function registerDir(string $dir): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->dir[] = $dir; |
|
163
|
|
|
|
|
164
|
|
|
if ($this->enableCache) { |
|
165
|
|
|
$this->cache->set('dir', $this->dir); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Function to check if cache is enabled. |
|
171
|
|
|
*/ |
|
172
|
|
|
public function isCacheEnabled(): bool |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->enableCache; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Function to check if its a registered dir. |
|
179
|
|
|
*/ |
|
180
|
|
|
public function isDir(string $dir): bool |
|
181
|
|
|
{ |
|
182
|
|
|
if ($this->enableCache && $this->cache->has('dir')) { |
|
183
|
|
|
$this->dir = $this->cache->get('dir'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return in_array($dir, $this->dir); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Enable cache with custom cache engine. |
|
191
|
|
|
*/ |
|
192
|
|
|
public function enableCache(\Psr\SimpleCache\CacheInterface $cache): void |
|
193
|
|
|
{ |
|
194
|
|
|
$this->cache = $cache; |
|
195
|
|
|
$this->enableCache = true; |
|
196
|
|
|
if ($this->cache->has('collection')) { |
|
197
|
|
|
$this->controllers = $this->cache->get('collection'); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Register manual route in route collection. |
|
203
|
|
|
*/ |
|
204
|
|
|
private function registerManual(string $method, string $route, callable $callable): void |
|
205
|
|
|
{ |
|
206
|
|
|
$this->route[$method][$route] = $callable; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* register manual get route. |
|
211
|
|
|
*/ |
|
212
|
|
|
public function get(string $route, callable $callable): void |
|
213
|
|
|
{ |
|
214
|
|
|
$this->registerManual('get', $route, $callable); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* register manual post route. |
|
219
|
|
|
*/ |
|
220
|
|
|
public function post(string $route, callable $callable): void |
|
221
|
|
|
{ |
|
222
|
|
|
$this->registerManual('post', $route, $callable); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* register manual put route. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function put(string $route, callable $callable): void |
|
229
|
|
|
{ |
|
230
|
|
|
$this->registerManual('put', $route, $callable); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* register manual delete route. |
|
235
|
|
|
*/ |
|
236
|
|
|
public function delete(string $route, callable $callable): void |
|
237
|
|
|
{ |
|
238
|
|
|
$this->registerManual('delete', $route, $callable); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* register manual patch route. |
|
243
|
|
|
*/ |
|
244
|
|
|
public function all(string $route, callable $callable): void |
|
245
|
|
|
{ |
|
246
|
|
|
$this->registerManual('all', $route, $callable); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* get callable using route and method. |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getRoute(string $route, string $method): callable|bool |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->route[$method][$route] ?? $this->route['all'][$route] ?? false; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* get all routes. |
|
259
|
|
|
* |
|
260
|
|
|
* @return array<mixed> |
|
261
|
|
|
*/ |
|
262
|
|
|
public function getRoutes(): array |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->route; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* check if auto register is enable. |
|
269
|
|
|
*/ |
|
270
|
|
|
public function isAutoRegistered(): bool |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->autoRegistered; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|