-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPhpDebugBarMiddleware.php
More file actions
159 lines (134 loc) · 4.32 KB
/
PhpDebugBarMiddleware.php
File metadata and controls
159 lines (134 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace PhpMiddleware\PhpDebugBar;
use DebugBar\JavascriptRenderer as DebugBarRenderer;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\Serializer;
use Zend\Diactoros\Stream;
/**
* PhpDebugBarMiddleware
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class PhpDebugBarMiddleware
{
/**
* @var DebugBarRenderer
*/
protected $debugBarRenderer;
/**
* @param DebugBarRenderer $debugbarRenderer
*/
public function __construct(DebugBarRenderer $debugbarRenderer)
{
$this->debugBarRenderer = $debugbarRenderer;
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
if ($staticFile = $this->getStaticFile($request->getUri())) {
return $staticFile;
}
$outResponse = $next($request, $response);
if (!$this->isHtmlAccepted($request)) {
return $outResponse;
}
$debugBarHead = $this->debugBarRenderer->renderHead();
$debugBarBody = $this->debugBarRenderer->render();
if ($this->isHtmlResponse($outResponse)) {
$body = $outResponse->getBody();
if (! $body->eof() && $body->isSeekable()) {
$body->seek(0, SEEK_END);
}
$body->write($debugBarHead . $debugBarBody);
return $outResponse;
}
$outResponseBody = Serializer::toString($outResponse);
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
$escapedOutResponseBody = htmlspecialchars($outResponseBody);
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody);
return new HtmlResponse($result);
}
/**
* @param UriInterface $uri
*
* @return ResponseInterface|null
*/
private function getStaticFile(UriInterface $uri)
{
if (strpos($uri->getPath(), $this->debugBarRenderer->getBaseUrl()) !== 0) {
return;
}
$pathToFile = substr($uri->getPath(), strlen($this->debugBarRenderer->getBaseUrl()));
$fullPathToFile = $this->debugBarRenderer->getBasePath() . $pathToFile;
if (!file_exists($fullPathToFile)) {
return;
}
$stream = new Stream($fullPathToFile, 'r');
$staticResponse = new Response($stream);
$contentType = $this->getContentTypeByFileName($fullPathToFile);
return $staticResponse->withHeader('Content-type', $contentType);
}
/**
* @param string $filename
*
* @return string
*/
private function getContentTypeByFileName($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$map = [
'css' => 'text/css',
'js' => 'text/javascript',
'otf' => 'font/opentype',
'eot' => 'application/vnd.ms-fontobject',
'svg' => 'image/svg+xml',
'ttf' => 'application/font-sfnt',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
];
if (isset($map[$ext])) {
return $map[$ext];
}
return 'text/plain';
}
/**
* @param ResponseInterface $response
*
* @return bool
*/
private function isHtmlResponse(ResponseInterface $response)
{
return $this->hasHeaderContains($response, 'Content-Type', 'text/html');
}
/**
* @param ServerRequestInterface $request
*
* @return bool
*/
private function isHtmlAccepted(ServerRequestInterface $request)
{
return $this->hasHeaderContains($request, 'Accept', 'text/html');
}
/**
* @param MessageInterface $message
* @param string $headerName
* @param string $value
*
* @return bool
*/
private function hasHeaderContains(MessageInterface $message, $headerName, $value)
{
return strpos($message->getHeaderLine($headerName), $value) !== false;
}
}