|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Yiisoft\Router\Dispatcher\DispatcherInterface; |
|
10
|
|
|
use Yiisoft\Router\Handler\HandlerAwareTrait; |
|
11
|
|
|
use Yiisoft\Router\Route\RouteCollectionInterface; |
|
12
|
|
|
use Yiisoft\Router\Route\RouteInterface; |
|
13
|
|
|
|
|
14
|
|
|
use function array_merge; |
|
15
|
|
|
|
|
16
|
|
|
final class Router implements RouterInterface, RouteCollectionInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use HandlerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
private RouteCollectionInterface $routeCollection; |
|
21
|
|
|
private MatcherInterface $matcher; |
|
22
|
|
|
private DispatcherInterface $dispatcher; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(RouteCollectionInterface $routeCollection, MatcherInterface $matcher, DispatcherInterface $dispatcher) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->routeCollection = clone $routeCollection; |
|
27
|
|
|
$this->matcher = $matcher; |
|
28
|
|
|
$this->dispatcher = $dispatcher; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getDispatcher(): DispatcherInterface |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->dispatcher; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function withDispatcher(DispatcherInterface $dispatcher): self |
|
37
|
|
|
{ |
|
38
|
|
|
$new = clone $this; |
|
39
|
|
|
$new->dispatcher = $dispatcher; |
|
40
|
|
|
return $new; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$matchingResult = $this->match($request); |
|
46
|
|
|
if ($matchingResult->isSuccess()) { |
|
47
|
|
|
$route = $matchingResult->getRoute(); |
|
48
|
|
|
$routeDispatcher = $route->getDispatcher(); |
|
49
|
|
|
$handlers = array_merge($route->getHandlers(), $this->getHandlers()); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if ($routeDispatcher !== null) { |
|
52
|
|
|
return $routeDispatcher->handlers($handlers)->handle($request); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->dispatcher = $this->dispatcher->handlers($handlers); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->dispatcher->handle($request); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function match(ServerRequestInterface $request): MatchingResult |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->matchForRoutes($this->routeCollection->getRoutes(), $request); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function matchForRoutes(iterable $routes, ServerRequestInterface $request): MatchingResult |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->matcher->matchForRoutes($routes, $request); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function addRoute(RouteInterface $route): self |
|
72
|
|
|
{ |
|
73
|
|
|
$this->routeCollection = $this->routeCollection->addRoute($route); |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function addRoutes(array $routes): self |
|
79
|
|
|
{ |
|
80
|
|
|
// TODO: Implement addRoutes() method. |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
public function addCollection(RouteCollectionInterface $collection): self |
|
84
|
|
|
{ |
|
85
|
|
|
// TODO: Implement addCollection() method. |
|
86
|
|
|
} |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
public function getRoutes(): iterable |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->routeCollection->getRoutes(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|