-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutingMiddleware.php
37 lines (29 loc) · 1.04 KB
/
RoutingMiddleware.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
<?php
declare(strict_types=1);
namespace Snicco\Component\HttpRouting\Middleware;
use Psr\Http\Message\ResponseInterface;
use Snicco\Component\HttpRouting\Http\Psr7\Request;
use Snicco\Component\HttpRouting\Routing\Exception\BadRouteConfiguration;
use Snicco\Component\HttpRouting\Routing\Exception\MethodNotAllowed;
use Snicco\Component\HttpRouting\Routing\UrlMatcher\RoutingResult;
use Snicco\Component\HttpRouting\Routing\UrlMatcher\UrlMatcher;
final class RoutingMiddleware extends Middleware
{
private UrlMatcher $url_matcher;
public function __construct(UrlMatcher $url_matcher)
{
$this->url_matcher = $url_matcher;
}
/**
* @throws BadRouteConfiguration
* @throws MethodNotAllowed
*/
protected function handle(Request $request, NextMiddleware $next): ResponseInterface
{
$result = $this->url_matcher->dispatch($request);
if (! $result->isMatch()) {
return $next($request);
}
return $next($request->withAttribute(RoutingResult::class, $result));
}
}