|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Venta\Contracts\Routing\MiddlewarePipelineFactory as MiddlewarePipelineFactoryContract; |
|
8
|
|
|
use Venta\Contracts\Routing\RequestRouteCollectionFactory; |
|
9
|
|
|
use Venta\Contracts\Routing\RouteCollection as RouteCollectionContract; |
|
10
|
|
|
use Venta\Contracts\Routing\RouteDispatcherFactory as RouteDispatcherFactoryContract; |
|
11
|
|
|
use Venta\Contracts\Routing\RouteMatcher as RouteMatcherContract; |
|
12
|
|
|
use Venta\Contracts\Routing\Router as RouterContract; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Router |
|
16
|
|
|
* |
|
17
|
|
|
* @package Venta\Routing |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Router implements RouterContract |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var RouteDispatcherFactoryContract |
|
23
|
|
|
*/ |
|
24
|
|
|
private $dispatcherFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var RouteMatcherContract |
|
28
|
|
|
*/ |
|
29
|
|
|
private $matcher; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var MiddlewarePipelineFactoryContract |
|
33
|
|
|
*/ |
|
34
|
|
|
private $pipelineFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RequestRouteCollectionFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
private $routeCollectionFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var RouteCollectionContract |
|
43
|
|
|
*/ |
|
44
|
|
|
private $routes; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Router constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param RouteDispatcherFactoryContract $dispatcherFactory |
|
50
|
|
|
* @param RouteMatcherContract $matcher |
|
51
|
|
|
* @param MiddlewarePipelineFactoryContract $pipelineFactory |
|
52
|
|
|
* @param RouteCollectionContract $routes |
|
53
|
|
|
* @param RequestRouteCollectionFactory $routeCollectionFactory |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function __construct( |
|
56
|
|
|
RouteDispatcherFactoryContract $dispatcherFactory, |
|
57
|
|
|
RouteMatcherContract $matcher, |
|
58
|
|
|
MiddlewarePipelineFactoryContract $pipelineFactory, |
|
59
|
|
|
RouteCollectionContract $routes, |
|
60
|
|
|
RequestRouteCollectionFactory $routeCollectionFactory |
|
61
|
|
|
) { |
|
62
|
2 |
|
$this->dispatcherFactory = $dispatcherFactory; |
|
|
|
|
|
|
63
|
2 |
|
$this->matcher = $matcher; |
|
|
|
|
|
|
64
|
2 |
|
$this->pipelineFactory = $pipelineFactory; |
|
|
|
|
|
|
65
|
2 |
|
$this->routes = $routes; |
|
|
|
|
|
|
66
|
2 |
|
$this->routeCollectionFactory = $routeCollectionFactory; |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritDoc |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function next(ServerRequestInterface $request): ResponseInterface |
|
73
|
|
|
{ |
|
74
|
1 |
|
$requestRouteCollection = $this->routeCollectionFactory->create($this->routes, $request); |
|
75
|
|
|
// Find matching route against provided request. |
|
76
|
1 |
|
$route = $this->matcher->match($request, $requestRouteCollection); |
|
77
|
|
|
|
|
78
|
|
|
// Create route middleware pipeline. |
|
79
|
1 |
|
$pipeline = $this->pipelineFactory->create($route->getMiddlewares()); |
|
80
|
|
|
// Create the last delegate, which calls route handler. |
|
81
|
1 |
|
$routeDispatcher = $this->dispatcherFactory->create($route); |
|
82
|
|
|
|
|
83
|
1 |
|
return $pipeline->process($request, $routeDispatcher); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.