1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Route\Strategy; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use League\Route\{ContainerAwareInterface, ContainerAwareTrait}; |
7
|
|
|
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException}; |
8
|
|
|
use League\Route\Route; |
9
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
10
|
|
|
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
11
|
|
|
|
12
|
|
|
class ApplicationStrategy implements ContainerAwareInterface, StrategyInterface |
13
|
|
|
{ |
14
|
|
|
use ContainerAwareTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
9 |
|
public function invokeRouteCallable(Route $route, ServerRequestInterface $request) : ResponseInterface |
20
|
|
|
{ |
21
|
9 |
|
return call_user_func_array($route->getCallable($this->getContainer()), [$request, $route->getVars()]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
6 |
|
public function getNotFoundDecorator(NotFoundException $exception) : MiddlewareInterface |
28
|
|
|
{ |
29
|
6 |
|
return $this->throwExceptionMiddleware($exception); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
6 |
|
public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception) : MiddlewareInterface |
36
|
|
|
{ |
37
|
6 |
|
return $this->throwExceptionMiddleware($exception); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return a middleware that simply throws and exception. |
42
|
|
|
* |
43
|
|
|
* @param \Exception $exception |
44
|
|
|
* |
45
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
46
|
|
|
*/ |
47
|
|
|
protected function throwExceptionMiddleware(Exception $exception) : MiddlewareInterface |
48
|
|
|
{ |
49
|
|
|
return new class($exception) implements MiddlewareInterface |
50
|
|
|
{ |
51
|
|
|
protected $exception; |
52
|
|
|
|
53
|
12 |
|
public function __construct(Exception $exception) |
54
|
|
|
{ |
55
|
12 |
|
$this->exception = $exception; |
56
|
12 |
|
} |
57
|
|
|
|
58
|
12 |
|
public function process( |
59
|
|
|
ServerRequestInterface $request, |
60
|
|
|
RequestHandlerInterface $requestHandler |
61
|
|
|
) : ResponseInterface { |
62
|
12 |
|
throw $this->exception; |
63
|
|
|
} |
64
|
|
|
}; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getExceptionHandler() : MiddlewareInterface |
71
|
|
|
{ |
72
|
|
|
return new class implements MiddlewareInterface |
73
|
|
|
{ |
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
9 |
|
public function process( |
78
|
|
|
ServerRequestInterface $request, |
79
|
|
|
RequestHandlerInterface $requestHandler |
80
|
|
|
) : ResponseInterface { |
81
|
|
|
try { |
82
|
9 |
|
return $requestHandler->handle($request); |
83
|
6 |
|
} catch (Exception $e) { |
84
|
6 |
|
throw $e; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|