1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\Router; |
4
|
|
|
|
5
|
|
|
use Bone\Router\Decorator\ExceptionDecorator; |
6
|
|
|
use Bone\Router\Decorator\NotAllowedDecorator; |
7
|
|
|
use Bone\Router\Decorator\NotFoundDecorator; |
8
|
|
|
use Bone\View\ViewEngine; |
9
|
|
|
use Bone\Router\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 ViewEngine $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
|
|
|
/** |
40
|
|
|
* PlatesStrategy constructor. |
41
|
|
|
* @param ViewEngine $viewEngine |
42
|
|
|
* @param NotFoundDecorator $notFound |
43
|
|
|
* @param NotAllowedDecorator $notAllowed |
44
|
|
|
* @param string $layout |
45
|
|
|
*/ |
46
|
|
|
public function __construct(ViewEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator) |
47
|
|
|
{ |
48
|
|
|
$this->viewEngine = $viewEngine; |
49
|
|
|
$this->notFoundDecorator = $notFound; |
50
|
|
|
$this->notAllowedDecorator = $notAllowed; |
51
|
|
|
$this->exceptionDecorator = $exceptionDecorator; |
52
|
|
|
$this->setLayout($layout); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Invoke the route callable based on the strategy. |
57
|
|
|
* |
58
|
|
|
* @param \League\Route\Route $route |
59
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
60
|
|
|
* |
61
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
62
|
|
|
*/ |
63
|
|
|
public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface |
64
|
|
|
{ |
65
|
|
|
$response = parent::invokeRouteCallable($route, $request); |
66
|
|
|
$contentType = $response->getHeader('Content-Type'); |
67
|
|
|
$isHtmlResponse = $response instanceof HtmlResponse; |
68
|
|
|
$hasHtmlContent = strstr($contentType[0], 'text/html'); |
69
|
|
|
|
70
|
|
|
if (!$isHtmlResponse || !$hasHtmlContent) { |
71
|
|
|
return $response; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$body = ['content' => $response->getBody()->getContents()]; |
75
|
|
|
$body = $this->viewEngine->render($this->layout, $body); |
76
|
|
|
|
77
|
|
|
return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ResponseInterface $response |
82
|
|
|
* @param string $body |
83
|
|
|
* @param int $status |
84
|
|
|
* @return \Psr\Http\Message\MessageInterface|Response |
85
|
|
|
*/ |
86
|
|
|
private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200) |
87
|
|
|
{ |
88
|
|
|
$stream = new Stream('php://memory', 'r+'); |
89
|
|
|
$stream->write($body); |
90
|
|
|
$response = $response->withStatus($status)->withBody($stream); |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a middleware that will decorate a NotFoundException |
97
|
|
|
* |
98
|
|
|
* @param \League\Route\Http\Exception\NotFoundException $exception |
99
|
|
|
* |
100
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
101
|
|
|
*/ |
102
|
|
|
public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface |
103
|
|
|
{ |
104
|
|
|
return $this->notFoundDecorator; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get a middleware that will decorate a NotAllowedException |
109
|
|
|
* |
110
|
|
|
* @param \League\Route\Http\Exception\NotFoundException $e |
111
|
|
|
* |
112
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
113
|
|
|
*/ |
114
|
|
|
public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface |
115
|
|
|
{ |
116
|
|
|
return $this->notAllowedDecorator; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return MiddlewareInterface |
121
|
|
|
*/ |
122
|
|
|
public function getExceptionHandler(): MiddlewareInterface |
123
|
|
|
{ |
124
|
|
|
return $this->exceptionDecorator; |
125
|
|
|
} |
126
|
|
|
} |