|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Mvc\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Bone\Mvc\Router\Decorator\ExceptionDecorator; |
|
6
|
|
|
use Bone\Mvc\Router\Decorator\NotAllowedDecorator; |
|
7
|
|
|
use Bone\Mvc\Router\Decorator\NotFoundDecorator; |
|
8
|
|
|
use Bone\Mvc\View\PlatesEngine; |
|
9
|
|
|
use Bone\Traits\HasLayoutTrait; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException}; |
|
|
|
|
|
|
12
|
|
|
use League\Route\Route; |
|
13
|
|
|
use League\Route\Strategy\ApplicationStrategy; |
|
14
|
|
|
use League\Route\Strategy\StrategyInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
18
|
|
|
use Laminas\Diactoros\Response; |
|
19
|
|
|
use Laminas\Diactoros\Response\HtmlResponse; |
|
20
|
|
|
use Laminas\Diactoros\Response\JsonResponse; |
|
21
|
|
|
use Laminas\Diactoros\Stream; |
|
22
|
|
|
|
|
23
|
|
|
class PlatesStrategy extends ApplicationStrategy implements StrategyInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use HasLayoutTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** @var PlatesEngine $viewEngine */ |
|
28
|
|
|
private $viewEngine; |
|
29
|
|
|
|
|
30
|
|
|
/** @var NotFoundDecorator $notFoundDecorator */ |
|
31
|
|
|
private $notFoundDecorator; |
|
32
|
|
|
|
|
33
|
|
|
/** @var NotAllowedDecorator $notAllowedDecorator */ |
|
34
|
|
|
private $notAllowedDecorator; |
|
35
|
|
|
|
|
36
|
|
|
/** @var ExceptionDecorator $exceptionDecorator */ |
|
37
|
|
|
private $exceptionDecorator; |
|
38
|
|
|
|
|
39
|
|
|
/** @var bool */ |
|
40
|
|
|
private $i18nEnabled = false; |
|
41
|
|
|
|
|
42
|
|
|
/** @var array */ |
|
43
|
|
|
private $supportedLocales = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* PlatesStrategy constructor. |
|
47
|
|
|
* @param PlatesEngine $viewEngine |
|
48
|
|
|
* @param NotFoundDecorator $notFound |
|
49
|
|
|
* @param NotAllowedDecorator $notAllowed |
|
50
|
|
|
* @param string $layout |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator) |
|
53
|
|
|
{ |
|
54
|
9 |
|
$this->viewEngine = $viewEngine; |
|
55
|
9 |
|
$this->notFoundDecorator = $notFound; |
|
56
|
9 |
|
$this->notAllowedDecorator = $notAllowed; |
|
57
|
9 |
|
$this->exceptionDecorator = $exceptionDecorator; |
|
58
|
9 |
|
$this->setLayout($layout); |
|
59
|
9 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param bool $i18nEnabled |
|
63
|
|
|
*/ |
|
64
|
8 |
|
public function setI18nEnabled(bool $i18nEnabled): void |
|
65
|
|
|
{ |
|
66
|
8 |
|
$this->i18nEnabled = $i18nEnabled; |
|
67
|
8 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $locales |
|
71
|
|
|
*/ |
|
72
|
8 |
|
public function setSupportedLocales(array $locales): void |
|
73
|
|
|
{ |
|
74
|
8 |
|
$this->supportedLocales = $locales; |
|
75
|
8 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Invoke the route callable based on the strategy. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \League\Route\Route $route |
|
82
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
85
|
|
|
*/ |
|
86
|
5 |
|
public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
|
|
90
|
5 |
|
$response = parent::invokeRouteCallable($route, $request); |
|
91
|
4 |
|
$contentType = $response->getHeader('Content-Type'); |
|
92
|
|
|
|
|
93
|
4 |
|
if ($contentType && strstr($contentType[0], 'application/json')) { |
|
|
|
|
|
|
94
|
2 |
|
return $response; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
$body = ['content' => $response->getBody()->getContents()]; |
|
98
|
2 |
|
$body = $this->viewEngine->render($this->layout, $body); |
|
99
|
|
|
|
|
100
|
2 |
|
return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode()); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
1 |
|
} catch (Exception $e) { |
|
103
|
1 |
|
$body = $this->viewEngine->render('error/error', [ |
|
104
|
1 |
|
'message' => $e->getMessage(), |
|
105
|
1 |
|
'code' => $e->getCode(), |
|
106
|
1 |
|
'trace' => $e->getTrace(), |
|
107
|
|
|
]); |
|
108
|
1 |
|
$body = $this->viewEngine->render($this->layout, [ |
|
109
|
1 |
|
'content' => $body, |
|
110
|
|
|
]); |
|
111
|
1 |
|
$status = ($e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; |
|
112
|
|
|
|
|
113
|
1 |
|
return $this->getResponseWithBodyAndStatus(new HtmlResponse($body), $body, $status); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param ResponseInterface $response |
|
120
|
|
|
* @param string $body |
|
121
|
|
|
* @param int $status |
|
122
|
|
|
* @return \Psr\Http\Message\MessageInterface|Response |
|
123
|
|
|
*/ |
|
124
|
3 |
View Code Duplication |
private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
3 |
|
$stream = new Stream('php://memory', 'r+'); |
|
127
|
3 |
|
$stream->write($body); |
|
128
|
3 |
|
$response = $response->withStatus($status)->withBody($stream); |
|
129
|
|
|
|
|
130
|
3 |
|
return $response; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get a middleware that will decorate a NotFoundException |
|
135
|
|
|
* |
|
136
|
|
|
* @param \League\Route\Http\Exception\NotFoundException $exception |
|
|
|
|
|
|
137
|
|
|
* |
|
138
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->notFoundDecorator; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get a middleware that will decorate a NotAllowedException |
|
147
|
|
|
* |
|
148
|
|
|
* @param \League\Route\Http\Exception\NotFoundException $e |
|
149
|
|
|
* |
|
150
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface |
|
153
|
|
|
{ |
|
154
|
1 |
|
return $this->notAllowedDecorator; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return MiddlewareInterface |
|
159
|
|
|
*/ |
|
160
|
5 |
|
public function getExceptionHandler(): MiddlewareInterface |
|
161
|
|
|
{ |
|
162
|
5 |
|
return $this->exceptionDecorator; |
|
163
|
|
|
} |
|
164
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: