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\Adr\Input; |
8
|
|
|
use Venta\Contracts\Adr\Payload; |
9
|
|
|
use Venta\Contracts\Adr\Responder; |
10
|
|
|
use Venta\Contracts\Container\Container; |
11
|
|
|
use Venta\Contracts\Http\Request as RequestContract; |
12
|
|
|
use Venta\Contracts\Routing\Route as RouteContract; |
13
|
|
|
use Venta\Contracts\Routing\RouteDispatcher as RouteDispatcherContract; |
14
|
|
|
use Venta\Http\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RouteDispatcher |
18
|
|
|
* |
19
|
|
|
* @package Venta\Routing |
20
|
|
|
*/ |
21
|
|
|
final class RouteDispatcher implements RouteDispatcherContract |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Container |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RouteContract |
31
|
|
|
*/ |
32
|
|
|
private $route; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* RouteDispatcher constructor. |
36
|
|
|
* |
37
|
|
|
* @param RouteContract $route |
38
|
|
|
* @param Container $container |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(RouteContract $route, Container $container) |
41
|
|
|
{ |
42
|
3 |
|
$this->route = $route; |
43
|
3 |
|
$this->container = $container; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
1 |
|
public function next(ServerRequestInterface $request): ResponseInterface |
50
|
|
|
{ |
51
|
|
|
// Add current route to the request. |
52
|
1 |
|
$request = $request->withAttribute('route', $this->route); |
53
|
|
|
|
54
|
1 |
|
if ($this->container->isCallable($this->route->domain())) { |
55
|
|
|
if ($this->container->has($this->route->input())) { |
56
|
|
|
/** @var Input $input */ |
57
|
|
|
$input = $this->container->get($this->route->input()); |
58
|
|
|
$arguments = $input->process($request); |
59
|
|
|
} |
60
|
|
|
/** @var Payload $payload */ |
61
|
|
|
$payload = $this->container->call($this->route->domain(), $arguments ?? []); |
62
|
|
|
} |
63
|
|
|
/** @var Responder $responder */ |
64
|
1 |
|
$responder = $this->container->get($this->route->responder()); |
65
|
|
|
|
66
|
1 |
|
return $responder->run($request, $payload ?? null); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |