1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace inroutephp\inroute\Runtime\Aura; |
6
|
|
|
|
7
|
|
|
use inroutephp\inroute\Runtime\DispatchingMiddleware; |
8
|
|
|
use inroutephp\inroute\Runtime\Environment; |
9
|
|
|
use inroutephp\inroute\Runtime\NaiveContainer; |
10
|
|
|
use inroutephp\inroute\Runtime\Exception\RouteNotFoundException; |
11
|
|
|
use inroutephp\inroute\Runtime\Exception\MethodNotAllowedException; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
17
|
|
|
use Aura\Router\RouterContainer; |
18
|
|
|
use Aura\Router\Map; |
19
|
|
|
use mindplay\middleman\Dispatcher; |
20
|
|
|
use mindplay\middleman\ContainerResolver; |
21
|
|
|
|
22
|
|
|
trait HttpRouterTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->container = new NaiveContainer; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
abstract protected function loadRoutes(Map $map): void; |
35
|
|
|
|
36
|
|
|
public function setContainer(ContainerInterface $container): void |
37
|
|
|
{ |
38
|
|
|
$this->container = $container; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$router = new RouterContainer; |
44
|
|
|
|
45
|
|
|
$this->loadRoutes($router->getMap()); |
46
|
|
|
|
47
|
|
|
$matcher = $router->getMatcher(); |
48
|
|
|
|
49
|
|
|
$match = $matcher->match($request); |
50
|
|
|
|
51
|
|
|
if (!$match) { |
52
|
|
|
$failedRoute = $matcher->getFailedRoute(); |
53
|
|
|
|
54
|
|
|
if ($failedRoute && $failedRoute->failedRule == 'Aura\Router\Rule\Allows') { |
55
|
|
|
if ($this->container->has(ResponseFactoryInterface::CLASS)) { |
|
|
|
|
56
|
|
|
return $this->container->get(ResponseFactoryInterface::CLASS) |
57
|
|
|
->createResponse(405) |
58
|
|
|
->withHeader('Allow', implode(', ', $failedRoute->allows)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new MethodNotAllowedException($request, ['allows' => $failedRoute->allows]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->container->has(ResponseFactoryInterface::CLASS)) { |
65
|
|
|
return $this->container->get(ResponseFactoryInterface::CLASS)->createResponse(404); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw new RouteNotFoundException($request); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var RouteInterface $route */ |
72
|
|
|
$route = $match->handler; |
73
|
|
|
|
74
|
|
|
foreach ($route->getAttributes() as $name => $val) { |
75
|
|
|
$request = $request->withAttribute($name, $val); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($match->attributes as $name => $val) { |
79
|
|
|
$request = $request->withAttribute($name, $val); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$middlewares = $route->getMiddlewareServiceIds(); |
83
|
|
|
|
84
|
|
|
$middlewares[] = new DispatchingMiddleware( |
85
|
|
|
[$this->container->get($route->getServiceId()), $route->getServiceMethod()], |
86
|
|
|
new Environment($route, new UrlGenerator($router->getGenerator())) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return (new Dispatcher($middlewares, new ContainerResolver($this->container)))->dispatch($request); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.