-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Router.php
53 lines (43 loc) · 1.19 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
<?php declare(strict_types = 1);
/**
* Router.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:WebServerPlugin!
* @subpackage Middleware
* @since 1.0.0
*
* @date 08.05.21
*/
namespace FastyBird\Plugin\WebServer\Middleware;
use FastyBird\Plugin\WebServer\Events;
use IPub\SlimRouter\Routing;
use Psr\EventDispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Application router middleware
*
* @package FastyBird:WebServerPlugin!
* @subpackage Middleware
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
final readonly class Router
{
public function __construct(
private Routing\IRouter $router,
private EventDispatcher\EventDispatcherInterface|null $dispatcher = null,
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
$this->dispatcher?->dispatch(new Events\Request($request));
$response = $this->router->handle($request);
$this->dispatcher?->dispatch(new Events\Response($request, $response));
return $response;
}
}