-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRouter.php
264 lines (231 loc) · 6.69 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace MiladRahimi\PhpRouter;
use Closure;
use MiladRahimi\PhpContainer\Container;
use MiladRahimi\PhpContainer\Exceptions\ContainerException;
use MiladRahimi\PhpRouter\Dispatching\Caller;
use MiladRahimi\PhpRouter\Dispatching\Matcher;
use MiladRahimi\PhpRouter\Exceptions\InvalidCallableException;
use MiladRahimi\PhpRouter\Exceptions\RouteNotFoundException;
use MiladRahimi\PhpRouter\Routing\Route;
use MiladRahimi\PhpRouter\Routing\Storekeeper;
use MiladRahimi\PhpRouter\Routing\Repository;
use MiladRahimi\PhpRouter\Publisher\HttpPublisher;
use MiladRahimi\PhpRouter\View\PhpView;
use MiladRahimi\PhpRouter\Publisher\Publisher;
use MiladRahimi\PhpRouter\View\View;
use Psr\Container\ContainerInterface;
use Laminas\Diactoros\ServerRequestFactory;
/**
* It defines the application routes and dispatches them (runs the application).
*/
class Router
{
private Container $container;
private Storekeeper $storekeeper;
private Matcher $matcher;
private Caller $caller;
private Publisher $publisher;
/**
* List of defined parameter patterns with `pattern()` method
*
* @var string[]
*/
private array $patterns = [];
public function __construct(
Container $container,
Storekeeper $storekeeper,
Matcher $matcher,
Caller $caller,
Publisher $publisher
)
{
$this->container = $container;
$this->storekeeper = $storekeeper;
$this->matcher = $matcher;
$this->caller = $caller;
$this->publisher = $publisher;
}
/**
* Create a new Router instance
*
* @return static
*/
public static function create(): self
{
$container = new Container();
$container->singleton(Container::class, $container);
$container->singleton(ContainerInterface::class, $container);
$container->singleton(Repository::class, new Repository());
$container->singleton(Publisher::class, HttpPublisher::class);
return $container->instantiate(Router::class);
}
/**
* Setup (enable) View
* @param string $directory
* @link View
*
*/
public function setupView(string $directory): void
{
$this->container->singleton(View::class, function () use ($directory) {
return new PhpView($directory);
});
}
/**
* Group routes with the given common attributes
*
* @param array $attributes
* @param Closure $body
*/
public function group(array $attributes, Closure $body): void
{
$oldState = clone $this->storekeeper->getState();
$this->storekeeper->getState()->append($attributes);
call_user_func($body, $this);
$this->storekeeper->setState($oldState);
}
/**
* Dispatch routes (and run the application)
*
* @throws ContainerException
* @throws InvalidCallableException
* @throws RouteNotFoundException
*/
public function dispatch()
{
$request = ServerRequestFactory::fromGlobals();
$route = $this->matcher->find($request, $this->patterns);
$this->container->singleton(Route::class, $route);
foreach ($route->getParameters() as $key => $value) {
$this->container->singleton('$' . $key, $value);
}
$stack = array_merge($route->getMiddleware(), [$route->getController()]);
$this->publisher->publish($this->caller->stack($stack, $request));
}
/**
* Define a parameter pattern
*
* @param string $name
* @param string $pattern
*/
public function pattern(string $name, string $pattern)
{
$this->patterns[$name] = $pattern;
}
/**
* Index all the defined routes
*
* @return Route[]
*/
public function all(): array
{
return $this->storekeeper->getRepository()->all();
}
/**
* Define a new route
*
* @param string $method
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function define(string $method, string $path, $controller, ?string $name = null): void
{
$this->storekeeper->add($method, $path, $controller, $name);
}
/**
* Map a controller to given route for all the http methods
*
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function any(string $path, $controller, ?string $name = null): void
{
$this->define('*', $path, $controller, $name);
}
/**
* Map a controller to given GET route
*
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function get(string $path, $controller, ?string $name = null): void
{
$this->define('GET', $path, $controller, $name);
}
/**
* Map a controller to given POST route
*
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function post(string $path, $controller, ?string $name = null): void
{
$this->define('POST', $path, $controller, $name);
}
/**
* Map a controller to given PUT route
*
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function put(string $path, $controller, ?string $name = null): void
{
$this->define('PUT', $path, $controller, $name);
}
/**
* Map a controller to given PATCH route
*
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function patch(string $path, $controller, ?string $name = null): void
{
$this->define('PATCH', $path, $controller, $name);
}
/**
* Map a controller to given DELETE route
*
* @param string $path
* @param Closure|string|array $controller
* @param string|null $name
*/
public function delete(string $path, $controller, ?string $name = null): void
{
$this->define('DELETE', $path, $controller, $name);
}
/**
* @return Container
*/
public function getContainer(): Container
{
return $this->container;
}
/**
* @param Container $container
*/
public function setContainer(Container $container): void
{
$this->container = $container;
}
/**
* @return Publisher
*/
public function getPublisher(): Publisher
{
return $this->publisher;
}
/**
* @param Publisher $publisher
*/
public function setPublisher(Publisher $publisher): void
{
$this->publisher = $publisher;
}
}