1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\View\Middleware; |
4
|
|
|
|
5
|
|
|
use Bone\View\ViewEngineInterface; |
6
|
|
|
use Laminas\Diactoros\Response; |
|
|
|
|
7
|
|
|
use Laminas\Diactoros\Response\HtmlResponse; |
|
|
|
|
8
|
|
|
use Laminas\Diactoros\Stream; |
|
|
|
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
|
|
|
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
|
|
|
13
|
|
|
|
14
|
|
|
class LayoutMiddleware implements MiddlewareInterface |
15
|
|
|
{ |
16
|
|
|
/** @var ViewEngineInterface $viewEngine */ |
17
|
|
|
private $viewEngine; |
18
|
|
|
|
19
|
|
|
/** @var string $defaultLayout\ */ |
20
|
|
|
private $defaultLayout; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* LayoutMiddleware constructor. |
24
|
|
|
* @param ViewEngineInterface $viewEngine |
25
|
|
|
*/ |
26
|
|
|
public function __construct(ViewEngineInterface $viewEngine, string $defaultLayout) |
27
|
|
|
{ |
28
|
|
|
$this->viewEngine = $viewEngine; |
29
|
|
|
$this->defaultLayout = $defaultLayout; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ServerRequestInterface $request |
34
|
|
|
* @param RequestHandlerInterface $handler |
35
|
|
|
* @return ResponseInterface |
36
|
|
|
*/ |
37
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
38
|
|
|
{ |
39
|
|
|
$response = $handler->handle($request); |
40
|
|
|
$layout = $this->defaultLayout; |
41
|
|
|
|
42
|
|
|
if ($response->hasHeader('layout')) { |
43
|
|
|
$layout = $response->getHeader('layout')[0]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$response = $response->withoutHeader('header'); |
47
|
|
|
$contentType = $response->getHeader('Content-Type'); |
48
|
|
|
$isHtmlResponse = $response instanceof HtmlResponse; |
49
|
|
|
$hasHtmlContent = count($contentType) ? strstr($contentType[0], 'text/html') : true; |
50
|
|
|
|
51
|
|
|
if ((!$isHtmlResponse || !$hasHtmlContent || !$layout)) { |
52
|
|
|
return $response; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$body = ['content' => $response->getBody()->getContents()]; |
56
|
|
|
$body = $this->viewEngine->render($layout, $body); |
57
|
|
|
|
58
|
|
|
return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Response $response |
64
|
|
|
* @param string $body |
65
|
|
|
* @param int $status |
66
|
|
|
* @return ResponseInterface |
67
|
|
|
*/ |
68
|
|
|
private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200): ResponseInterface |
69
|
|
|
{ |
70
|
|
|
$stream = new Stream('php://memory', 'r+'); |
71
|
|
|
$stream->write($body); |
72
|
|
|
$response = $response->withStatus($status)->withBody($stream); |
73
|
|
|
|
74
|
|
|
return $response; |
75
|
|
|
} |
76
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths