|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Equip\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Equip\Directory; |
|
6
|
|
|
use Equip\Exception\HttpException; |
|
7
|
|
|
use Equip\Handler\ActionHandler; |
|
8
|
|
|
use FastRoute; |
|
9
|
|
|
use FastRoute\Dispatcher; |
|
10
|
|
|
use FastRoute\RouteCollector; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
|
|
14
|
|
|
class DispatchHandler |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Directory |
|
18
|
|
|
*/ |
|
19
|
|
|
private $directory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Directory $directory |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public function __construct(Directory $directory) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$this->directory = $directory; |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function __invoke( |
|
33
|
|
|
ServerRequestInterface $request, |
|
34
|
|
|
ResponseInterface $response, |
|
35
|
|
|
callable $next |
|
36
|
|
|
) { |
|
37
|
|
|
/** |
|
38
|
|
|
* @var $action Equip\Action |
|
39
|
|
|
*/ |
|
40
|
4 |
|
list($action, $args) = $this->dispatch( |
|
41
|
4 |
|
$this->dispatcher(), |
|
42
|
4 |
|
$request->getMethod(), |
|
43
|
4 |
|
$request->getUri()->getPath() |
|
44
|
4 |
|
); |
|
45
|
|
|
|
|
46
|
2 |
|
$request = $request->withAttribute(ActionHandler::ACTION_ATTRIBUTE, $action); |
|
47
|
|
|
|
|
48
|
2 |
|
foreach ($args as $key => $value) { |
|
49
|
2 |
|
$request = $request->withAttribute($key, $value); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
return $next($request, $response); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return Dispatcher |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function dispatcher() |
|
59
|
|
|
{ |
|
60
|
4 |
|
return FastRoute\simpleDispatcher(function (RouteCollector $collector) { |
|
61
|
4 |
|
foreach ($this->directory as $request => $action) { |
|
62
|
3 |
|
list($method, $path) = explode(' ', $request, 2); |
|
63
|
|
|
|
|
64
|
3 |
|
$collector->addRoute( |
|
65
|
3 |
|
$method, |
|
66
|
3 |
|
$this->directory->prefix($path), |
|
67
|
|
|
$action |
|
68
|
3 |
|
); |
|
69
|
4 |
|
} |
|
70
|
4 |
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @throws HttpNotFound |
|
75
|
|
|
* @throws HttpMethodNotAllowed |
|
76
|
|
|
* |
|
77
|
|
|
* @param Dispatcher $dispatcher |
|
78
|
|
|
* @param string $method |
|
79
|
|
|
* @param string $path |
|
80
|
|
|
* |
|
81
|
|
|
* @return array [Action, $arguments] |
|
82
|
|
|
*/ |
|
83
|
4 |
|
private function dispatch(Dispatcher $dispatcher, $method, $path) |
|
84
|
|
|
{ |
|
85
|
4 |
|
$route = $dispatcher->dispatch($method, $path); |
|
86
|
4 |
|
$status = array_shift($route); |
|
87
|
|
|
|
|
88
|
4 |
|
if (Dispatcher::FOUND === $status) { |
|
89
|
2 |
|
return $route; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
if (Dispatcher::METHOD_NOT_ALLOWED === $status) { |
|
93
|
1 |
|
$allowed = array_shift($route); |
|
94
|
1 |
|
throw HttpException::methodNotAllowed($path, $method, $allowed); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
throw HttpException::notFound($path); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|