1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\View\Middleware; |
4
|
|
|
|
5
|
|
|
use Bone\Server\SiteConfig; |
6
|
|
|
use Bone\View\ViewEngineInterface; |
7
|
|
|
use Laminas\Diactoros\Response; |
|
|
|
|
8
|
|
|
use Laminas\Diactoros\Response\HtmlResponse; |
|
|
|
|
9
|
|
|
use Laminas\Diactoros\Stream; |
|
|
|
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
14
|
|
|
|
15
|
|
|
class LayoutMiddleware implements MiddlewareInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ViewEngineInterface $viewEngine */ |
18
|
|
|
private $viewEngine; |
19
|
|
|
|
20
|
|
|
/** @var string $defaultLayout\ */ |
21
|
|
|
private $defaultLayout; |
22
|
|
|
|
23
|
|
|
/** @var SiteConfig $config\ */ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* LayoutMiddleware constructor. |
28
|
|
|
* @param ViewEngineInterface $viewEngine |
29
|
|
|
* @param string $defaultLayout |
30
|
|
|
* @param SiteConfig $config |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ViewEngineInterface $viewEngine, string $defaultLayout, SiteConfig $config) |
33
|
|
|
{ |
34
|
|
|
$this->viewEngine = $viewEngine; |
35
|
|
|
$this->defaultLayout = $defaultLayout; |
36
|
|
|
$this->config = $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ServerRequestInterface $request |
41
|
|
|
* @param RequestHandlerInterface $handler |
42
|
|
|
* @return ResponseInterface |
43
|
|
|
*/ |
44
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
$response = $handler->handle($request); |
47
|
|
|
$layout = $this->defaultLayout; |
48
|
|
|
|
49
|
|
|
if ($response->hasHeader('layout')) { |
50
|
|
|
$layout = $response->getHeader('layout')[0]; |
51
|
|
|
$layout = $layout === 'none' ? false : $layout; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$response = $response->withoutHeader('header'); |
55
|
|
|
$contentType = $response->getHeader('Content-Type'); |
56
|
|
|
$isHtmlResponse = $response instanceof HtmlResponse; |
57
|
|
|
$hasHtmlContent = count($contentType) ? strstr($contentType[0], 'text/html') : true; |
58
|
|
|
|
59
|
|
|
if ((!$isHtmlResponse || !$hasHtmlContent || !$layout)) { |
60
|
|
|
return $response; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$body = [ |
64
|
|
|
'content' => $response->getBody()->getContents(), |
65
|
|
|
'config' => $this->config |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$body['user'] = $response->hasHeader('user') ? json_decode($response->getHeaderLine('user') ): null; |
69
|
|
|
|
70
|
|
|
$body = $this->viewEngine->render($layout, $body); |
71
|
|
|
|
72
|
|
|
return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Response $response |
78
|
|
|
* @param string $body |
79
|
|
|
* @param int $status |
80
|
|
|
* @return ResponseInterface |
81
|
|
|
*/ |
82
|
|
|
private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200): ResponseInterface |
83
|
|
|
{ |
84
|
|
|
$stream = new Stream('php://memory', 'r+'); |
85
|
|
|
$stream->write($body); |
86
|
|
|
$response = $response->withStatus($status)->withBody($stream); |
87
|
|
|
|
88
|
|
|
return $response; |
89
|
|
|
} |
90
|
|
|
} |
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