|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Mvc\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Bone\Mvc\Router\Decorator\NotAllowedDecorator; |
|
6
|
|
|
use Bone\Mvc\Router\Decorator\NotFoundDecorator; |
|
7
|
|
|
use Bone\Mvc\View\PlatesEngine; |
|
8
|
|
|
use Bone\Traits\LayoutAwareTrait; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException}; |
|
11
|
|
|
use League\Route\Route; |
|
12
|
|
|
use League\Route\Strategy\ApplicationStrategy; |
|
13
|
|
|
use League\Route\Strategy\StrategyInterface; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
16
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
17
|
|
|
use Zend\Diactoros\Response; |
|
18
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
19
|
|
|
use Zend\Diactoros\Stream; |
|
20
|
|
|
|
|
21
|
|
|
class PlatesStrategy extends ApplicationStrategy implements StrategyInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use LayoutAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** @var PlatesEngine $viewEngine */ |
|
26
|
|
|
private $viewEngine; |
|
27
|
|
|
|
|
28
|
|
|
/** @var NotFoundDecorator $notFoundDecorator */ |
|
29
|
|
|
private $notFoundDecorator; |
|
30
|
|
|
|
|
31
|
|
|
/** @var NotAllowedDecorator $notAllowedDecorator */ |
|
32
|
|
|
private $notAllowedDecorator; |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
private $i18nEnabled = false; |
|
36
|
|
|
|
|
37
|
|
|
/** @var array */ |
|
38
|
|
|
private $supportedLocales = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* PlatesStrategy constructor. |
|
42
|
|
|
* @param PlatesEngine $viewEngine |
|
43
|
|
|
* @param NotFoundDecorator $notFound |
|
44
|
|
|
* @param NotAllowedDecorator $notAllowed |
|
45
|
|
|
* @param string $layout |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout) |
|
48
|
|
|
{ |
|
49
|
3 |
|
$this->viewEngine = $viewEngine; |
|
50
|
3 |
|
$this->notFoundDecorator = $notFound; |
|
51
|
3 |
|
$this->notAllowedDecorator = $notAllowed; |
|
52
|
3 |
|
$this->setLayout($layout); |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param bool $i18nEnabled |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function setI18nEnabled(bool $i18nEnabled): void |
|
59
|
|
|
{ |
|
60
|
3 |
|
$this->i18nEnabled = $i18nEnabled; |
|
61
|
3 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $locales |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function setSupportedLocales(array $locales): void |
|
67
|
|
|
{ |
|
68
|
3 |
|
$this->supportedLocales = $locales; |
|
69
|
3 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Invoke the route callable based on the strategy. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \League\Route\Route $route |
|
76
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
|
|
84
|
1 |
|
$response = parent::invokeRouteCallable($route, $request); |
|
85
|
1 |
|
$body = $this->getBody($response); |
|
86
|
1 |
|
$body = $this->viewEngine->render($this->layout, $body); |
|
87
|
|
|
|
|
88
|
1 |
|
return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode()); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
} catch (Exception $e) { |
|
91
|
|
|
$body = $this->viewEngine->render('error/error', [ |
|
92
|
|
|
'message' => $e->getMessage(), |
|
93
|
|
|
'code' => $e->getCode(), |
|
94
|
|
|
'trace' => $e->getTrace(), |
|
95
|
|
|
]); |
|
96
|
|
|
$body = $this->viewEngine->render($this->layout, [ |
|
97
|
|
|
'content' => $body, |
|
98
|
|
|
]); |
|
99
|
|
|
$status = ($e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; |
|
100
|
|
|
|
|
101
|
|
|
return $this->getResponseWithBodyAndStatus(new HtmlResponse($body), $body, $status); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param ResponseInterface $response |
|
108
|
|
|
* @return array|mixed |
|
109
|
|
|
*/ |
|
110
|
1 |
|
private function getBody(ResponseInterface $response) |
|
111
|
|
|
{ |
|
112
|
|
|
switch (true) { |
|
113
|
1 |
|
case $response instanceof Response\JsonResponse: |
|
114
|
|
|
$contents = $response->getBody()->getContents(); |
|
115
|
|
|
$body = json_decode($contents, true); |
|
116
|
|
|
$body = ($body === null) ? [] : $body; |
|
117
|
|
|
break; |
|
118
|
|
|
|
|
119
|
1 |
|
case $response instanceof Response: |
|
120
|
1 |
|
$body = ['content' => $response->getBody()->getContents()]; |
|
121
|
1 |
|
break; |
|
122
|
|
|
default: |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
return $body; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param ResponseInterface $response |
|
131
|
|
|
* @param string $body |
|
132
|
|
|
* @param int $status |
|
133
|
|
|
* @return \Psr\Http\Message\MessageInterface|Response |
|
134
|
|
|
*/ |
|
135
|
1 |
|
private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200) |
|
136
|
|
|
{ |
|
137
|
1 |
|
$stream = new Stream('php://memory', 'r+'); |
|
138
|
1 |
|
$stream->write($body); |
|
139
|
1 |
|
$response = $response->withStatus($status)->withBody($stream); |
|
140
|
|
|
|
|
141
|
1 |
|
return $response; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get a middleware that will decorate a NotFoundException |
|
146
|
|
|
* |
|
147
|
|
|
* @param \League\Route\Http\Exception\NotFoundException $exception |
|
|
|
|
|
|
148
|
|
|
* |
|
149
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface |
|
152
|
|
|
{ |
|
153
|
1 |
|
return $this->notFoundDecorator; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get a middleware that will decorate a NotAllowedException |
|
158
|
|
|
* |
|
159
|
|
|
* @param \League\Route\Http\Exception\NotFoundException $e |
|
160
|
|
|
* |
|
161
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->notAllowedDecorator; |
|
166
|
|
|
} |
|
167
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.