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
|
|
|
* This class is used when it is used as stand alone router. |
18
|
|
|
*/ |
19
|
|
|
final readonly class Router |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
// ---------------------------------------------------------------// |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Stores the RouterCollection object. |
25
|
|
|
*/ |
26
|
|
|
private RouteCollection $collection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Stores the Engine Instance. |
30
|
|
|
*/ |
31
|
|
|
private RouterEngine $engine; |
32
|
|
|
|
33
|
|
|
public const NOT_FOUND = 0; |
34
|
|
|
public const FOUND = 1; |
35
|
|
|
public const METHOD_NOT_ALLOWED = 2; |
36
|
|
|
|
37
|
|
|
// ---------------------------------------------------------------// |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* constructor overloading for auto routing. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->collection = new RouteCollection(); |
45
|
|
|
$this->engine = new RouterEngine($this->collection); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// ---------------------------------------------------------------// |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* constructor overloading for auto routing. |
52
|
|
|
*/ |
53
|
|
|
public function register(string $dir, string $namespace): void |
54
|
|
|
{ |
55
|
|
|
$this->collection->register($dir, $namespace); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// ---------------------------------------------------------------// |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Enable cache. |
62
|
|
|
*/ |
63
|
|
|
public function enableCache(\Psr\SimpleCache\CacheInterface $cache): void |
64
|
|
|
{ |
65
|
|
|
$this->collection->enableCache($cache); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// ---------------------------------------------------------------// |
69
|
|
|
/** |
70
|
|
|
* Dispatch function. |
71
|
|
|
* |
72
|
|
|
* @return array<int, mixed> |
73
|
|
|
*/ |
74
|
|
|
public function dispatch(string $httpMethod, string $uri): array |
75
|
|
|
{ |
76
|
|
|
$result = $this->engine->route($httpMethod, $uri); |
77
|
|
|
|
78
|
|
|
if (0 == $result[0] || 2 == $result[0]) { |
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (\is_callable($result[1])) { |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
[$class, $method] = explode('::', (string) $result[1], 2); |
87
|
|
|
$result[1] = [new $class(), $method]; |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* register manual get route. |
94
|
|
|
*/ |
95
|
|
|
public function get(string $route, callable $callable): void |
96
|
|
|
{ |
97
|
|
|
$this->collection->get($route, $callable); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Register manual post route. |
102
|
|
|
*/ |
103
|
|
|
public function post(string $route, callable $callable): void |
104
|
|
|
{ |
105
|
|
|
$this->collection->post($route, $callable); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Register manual put route. |
110
|
|
|
*/ |
111
|
|
|
public function put(string $route, callable $callable): void |
112
|
|
|
{ |
113
|
|
|
$this->collection->put($route, $callable); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Register manual delete route. |
118
|
|
|
*/ |
119
|
|
|
public function delete(string $route, callable $callable): void |
120
|
|
|
{ |
121
|
|
|
$this->collection->delete($route, $callable); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Register manual all route. |
126
|
|
|
*/ |
127
|
|
|
public function all(string $route, callable $callable): void |
128
|
|
|
{ |
129
|
|
|
$this->collection->all($route, $callable); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|