5
5
6
6
use DebugBar \JavascriptRenderer as DebugBarRenderer ;
7
7
use Psr \Http \Message \MessageInterface ;
8
- use Psr \Http \Message \ResponseInterface ;
9
- use Psr \Http \Message \ServerRequestInterface ;
8
+ use Psr \Http \Message \ResponseInterface as Response ;
9
+ use Psr \Http \Message \ServerRequestInterface as ServerRequest ;
10
10
use Psr \Http \Message \UriInterface ;
11
11
use Psr \Http \Server \MiddlewareInterface ;
12
- use Psr \Http \Server \RequestHandlerInterface ;
13
- use Slim \Http \Uri ;
14
- use Zend \Diactoros \Response ;
12
+ use Psr \Http \Server \RequestHandlerInterface as RequestHandler ;
13
+ use Slim \Http \Uri as SlimUri ;
14
+ use Zend \Diactoros \Response as DiactorosResponse ;
15
15
use Zend \Diactoros \Response \HtmlResponse ;
16
16
use Zend \Diactoros \Response \Serializer ;
17
17
use Zend \Diactoros \Stream ;
23
23
*/
24
24
final class PhpDebugBarMiddleware implements MiddlewareInterface
25
25
{
26
+ public const FORCE_KEY = 'X-Enable-Debug-Bar ' ;
27
+
26
28
protected $ debugBarRenderer ;
27
29
28
30
public function __construct (DebugBarRenderer $ debugbarRenderer )
@@ -33,15 +35,21 @@ public function __construct(DebugBarRenderer $debugbarRenderer)
33
35
/**
34
36
* @inheritDoc
35
37
*/
36
- public function process (ServerRequestInterface $ request , RequestHandlerInterface $ handler ): ResponseInterface
38
+ public function process (ServerRequest $ request , RequestHandler $ handler ): Response
37
39
{
38
40
if ($ staticFile = $ this ->getStaticFile ($ request ->getUri ())) {
39
41
return $ staticFile ;
40
42
}
41
43
42
44
$ response = $ handler ->handle ($ request );
43
45
44
- if (!$ this ->isHtmlAccepted ($ request )) {
46
+ $ forceHeaderValue = $ request ->getHeaderLine (self ::FORCE_KEY );
47
+ $ forceCookieValue = $ request ->getCookieParams ()[self ::FORCE_KEY ] ?? '' ;
48
+ $ forceAttibuteValue = $ request ->getAttribute (self ::FORCE_KEY , '' );
49
+ $ isForceEnable = in_array ('true ' , [$ forceHeaderValue , $ forceCookieValue , $ forceAttibuteValue ], true );
50
+ $ isForceDisable = in_array ('false ' , [$ forceHeaderValue , $ forceCookieValue , $ forceAttibuteValue ], true );
51
+
52
+ if ($ isForceDisable || (!$ isForceEnable && ($ this ->isRedirect ($ response ) || !$ this ->isHtmlAccepted ($ request )))) {
45
53
return $ response ;
46
54
}
47
55
@@ -51,30 +59,27 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
51
59
return $ this ->prepareHtmlResponseWithDebugBar ($ response );
52
60
}
53
61
54
- public function __invoke (ServerRequestInterface $ request , ResponseInterface $ response , callable $ next ): ResponseInterface
62
+ public function __invoke (ServerRequest $ request , Response $ response , callable $ next ): Response
55
63
{
56
- $ handler = new class ($ next , $ response ) implements RequestHandlerInterface {
64
+ $ handler = new class ($ next , $ response ) implements RequestHandler {
57
65
private $ next ;
58
66
private $ response ;
59
67
60
- public function __construct (callable $ next , ResponseInterface $ response )
68
+ public function __construct (callable $ next , Response $ response )
61
69
{
62
70
$ this ->next = $ next ;
63
71
$ this ->response = $ response ;
64
72
}
65
73
66
- public function handle (ServerRequestInterface $ request ): ResponseInterface
74
+ public function handle (ServerRequest $ request ): Response
67
75
{
68
76
return ($ this ->next )($ request , $ this ->response );
69
77
}
70
78
};
71
79
return $ this ->process ($ request , $ handler );
72
80
}
73
81
74
- /**
75
- * @return HtmlResponse
76
- */
77
- private function prepareHtmlResponseWithDebugBar (ResponseInterface $ response )
82
+ private function prepareHtmlResponseWithDebugBar (Response $ response ): HtmlResponse
78
83
{
79
84
$ head = $ this ->debugBarRenderer ->renderHead ();
80
85
$ body = $ this ->debugBarRenderer ->render ();
@@ -86,10 +91,7 @@ private function prepareHtmlResponseWithDebugBar(ResponseInterface $response)
86
91
return new HtmlResponse ($ result );
87
92
}
88
93
89
- /**
90
- * @return ResponseInterface
91
- */
92
- private function attachDebugBarToResponse (ResponseInterface $ response )
94
+ private function attachDebugBarToResponse (Response $ response ): Response
93
95
{
94
96
$ head = $ this ->debugBarRenderer ->renderHead ();
95
97
$ body = $ this ->debugBarRenderer ->render ();
@@ -103,42 +105,34 @@ private function attachDebugBarToResponse(ResponseInterface $response)
103
105
return $ response ;
104
106
}
105
107
106
- /**
107
- * @return ResponseInterface|null
108
- */
109
- private function getStaticFile (UriInterface $ uri )
108
+ private function getStaticFile (UriInterface $ uri ): ?Response
110
109
{
111
110
$ path = $ this ->extractPath ($ uri );
112
111
113
112
if (strpos ($ path , $ this ->debugBarRenderer ->getBaseUrl ()) !== 0 ) {
114
- return ;
113
+ return null ;
115
114
}
116
115
117
116
$ pathToFile = substr ($ path , strlen ($ this ->debugBarRenderer ->getBaseUrl ()));
118
117
119
118
$ fullPathToFile = $ this ->debugBarRenderer ->getBasePath () . $ pathToFile ;
120
119
121
120
if (!file_exists ($ fullPathToFile )) {
122
- return ;
121
+ return null ;
123
122
}
124
123
125
124
$ contentType = $ this ->getContentTypeByFileName ($ fullPathToFile );
126
125
$ stream = new Stream ($ fullPathToFile , 'r ' );
127
126
128
- return new Response ($ stream , 200 , [
127
+ return new DiactorosResponse ($ stream , 200 , [
129
128
'Content-type ' => $ contentType ,
130
129
]);
131
130
}
132
131
133
- /**
134
- * @param UriInterface $uri
135
- *
136
- * @return string
137
- */
138
- private function extractPath (UriInterface $ uri )
132
+ private function extractPath (UriInterface $ uri ): string
139
133
{
140
134
// Slim3 compatibility
141
- if ($ uri instanceof Uri ) {
135
+ if ($ uri instanceof SlimUri ) {
142
136
$ basePath = $ uri ->getBasePath ();
143
137
if (!empty ($ basePath )) {
144
138
return $ basePath ;
@@ -147,12 +141,7 @@ private function extractPath(UriInterface $uri)
147
141
return $ uri ->getPath ();
148
142
}
149
143
150
- /**
151
- * @param string $filename
152
- *
153
- * @return string
154
- */
155
- private function getContentTypeByFileName ($ filename )
144
+ private function getContentTypeByFileName (string $ filename ): string
156
145
{
157
146
$ ext = pathinfo ($ filename , PATHINFO_EXTENSION );
158
147
@@ -170,35 +159,25 @@ private function getContentTypeByFileName($filename)
170
159
return isset ($ map [$ ext ]) ? $ map [$ ext ] : 'text/plain ' ;
171
160
}
172
161
173
- /**
174
- * @param ResponseInterface $response
175
- *
176
- * @return bool
177
- */
178
- private function isHtmlResponse (ResponseInterface $ response )
162
+ private function isHtmlResponse (Response $ response ): bool
179
163
{
180
164
return $ this ->hasHeaderContains ($ response , 'Content-Type ' , 'text/html ' );
181
165
}
182
166
183
- /**
184
- * @param ServerRequestInterface $request
185
- *
186
- * @return bool
187
- */
188
- private function isHtmlAccepted (ServerRequestInterface $ request )
167
+ private function isHtmlAccepted (ServerRequest $ request ): bool
189
168
{
190
169
return $ this ->hasHeaderContains ($ request , 'Accept ' , 'text/html ' );
191
170
}
192
171
193
- /**
194
- * @param MessageInterface $message
195
- * @param string $headerName
196
- * @param string $value
197
- *
198
- * @return bool
199
- */
200
- private function hasHeaderContains (MessageInterface $ message , $ headerName , $ value )
172
+ private function hasHeaderContains (MessageInterface $ message , string $ headerName , string $ value ): bool
201
173
{
202
174
return strpos ($ message ->getHeaderLine ($ headerName ), $ value ) !== false ;
203
175
}
176
+
177
+ private function isRedirect (Response $ response ): bool
178
+ {
179
+ $ statusCode = $ response ->getStatusCode ();
180
+
181
+ return ($ statusCode >= 300 || $ statusCode < 400 ) && $ response ->getHeaderLine ('Location ' ) !== '' ;
182
+ }
204
183
}
0 commit comments