Skip to content

Commit 6a1f20f

Browse files
committed
Add psr-15 support, drop 5.5 support
1 parent 95a1c4d commit 6a1f20f

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.5
54
- 5.6
65
- 7.0
76
- 7.1
@@ -13,8 +12,8 @@ env:
1312

1413
before_script:
1514
- composer self-update
16-
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable --no-interaction ; fi
17-
- travis_retry composer install --no-interaction
15+
- if [[ $DEPS == 'lowest' ]]; then composer update --prefer-stable --no-interaction --prefer-lowest ; fi
16+
- if [[ $DEPS == 'latest' ]]; then composer update --prefer-stable --no-interaction ; fi
1817

1918
script:
2019
- ./vendor/bin/phpunit

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
"debug",
77
"middleware",
88
"psr",
9-
"psr-7"
9+
"psr-7",
10+
"psr-15"
1011
],
1112
"require": {
12-
"php": "^5.5 || ^7.0",
13+
"php": ">=5.6",
1314
"maximebf/debugbar": "^1.10",
1415
"psr/http-message": "^1.0",
1516
"container-interop/container-interop": "^1.1",
16-
"zendframework/zend-diactoros": "^1.1.3"
17+
"zendframework/zend-diactoros": "^1.1.3",
18+
"http-interop/http-middleware": "^0.4.1",
19+
"php-middleware/double-pass-compatibility": "^1.0"
1720
},
1821
"require-dev": {
19-
"phpunit/phpunit": "^4.8.6",
22+
"phpunit/phpunit": "^5.7.19",
2023
"mikey179/vfsStream": "^1.6",
2124
"slim/slim": "^3.0",
2225
"zendframework/zend-expressive": "^1.0",

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<phpunit bootstrap="./vendor/autoload.php" colors="true">
44
<testsuites>
5-
<testsuite name="PhpMiddleware\\PhpDebugBar Tests">
5+
<testsuite>
66
<directory>./test</directory>
77
</testsuite>
88
</testsuites>

src/PhpDebugBarMiddleware.php

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace PhpMiddleware\PhpDebugBar;
44

55
use DebugBar\JavascriptRenderer as DebugBarRenderer;
6+
use Interop\Http\ServerMiddleware\DelegateInterface;
7+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
8+
use PhpMiddleware\DoublePassCompatibilityTrait;
69
use Psr\Http\Message\MessageInterface;
710
use Psr\Http\Message\ResponseInterface;
811
use Psr\Http\Message\ServerRequestInterface;
@@ -18,64 +21,71 @@
1821
*
1922
* @author Witold Wasiczko <witold@wasiczko.pl>
2023
*/
21-
class PhpDebugBarMiddleware
24+
class PhpDebugBarMiddleware implements MiddlewareInterface
2225
{
23-
/**
24-
* @var DebugBarRenderer
25-
*/
26+
use DoublePassCompatibilityTrait;
27+
2628
protected $debugBarRenderer;
2729

28-
/**
29-
* @param DebugBarRenderer $debugbarRenderer
30-
*/
3130
public function __construct(DebugBarRenderer $debugbarRenderer)
3231
{
3332
$this->debugBarRenderer = $debugbarRenderer;
3433
}
3534

3635
/**
37-
* @param ServerRequestInterface $request
38-
* @param ResponseInterface $response
39-
* @param callable $next
40-
*
41-
* @return ResponseInterface
36+
* @inheritDoc
4237
*/
43-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
38+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
4439
{
4540
if ($staticFile = $this->getStaticFile($request->getUri())) {
4641
return $staticFile;
4742
}
4843

49-
$outResponse = $next($request, $response);
44+
$response = $delegate->process($request);
5045

5146
if (!$this->isHtmlAccepted($request)) {
52-
return $outResponse;
47+
return $response;
5348
}
5449

55-
$debugBarHead = $this->debugBarRenderer->renderHead();
56-
$debugBarBody = $this->debugBarRenderer->render();
57-
58-
if ($this->isHtmlResponse($outResponse)) {
59-
$body = $outResponse->getBody();
60-
if (! $body->eof() && $body->isSeekable()) {
61-
$body->seek(0, SEEK_END);
62-
}
63-
$body->write($debugBarHead . $debugBarBody);
64-
65-
return $outResponse;
50+
if ($this->isHtmlResponse($response)) {
51+
return $this->attachDebugBarToResponse($response);
6652
}
53+
return $this->prepareHtmlResponseWithDebugBar($response);
54+
}
6755

68-
$outResponseBody = Serializer::toString($outResponse);
56+
/**
57+
* @return HtmlResponse
58+
*/
59+
private function prepareHtmlResponseWithDebugBar(ResponseInterface $response)
60+
{
61+
$head = $this->debugBarRenderer->renderHead();
62+
$body = $this->debugBarRenderer->render();
63+
$outResponseBody = Serializer::toString($response);
6964
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
7065
$escapedOutResponseBody = htmlspecialchars($outResponseBody);
71-
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody);
66+
$result = sprintf($template, $head, $escapedOutResponseBody, $body);
7267

7368
return new HtmlResponse($result);
7469
}
7570

7671
/**
77-
* @param UriInterface $uri
78-
*
72+
* @return ResponseInterface
73+
*/
74+
private function attachDebugBarToResponse(ResponseInterface $response)
75+
{
76+
$head = $this->debugBarRenderer->renderHead();
77+
$body = $this->debugBarRenderer->render();
78+
$responseBody = $response->getBody();
79+
80+
if (! $responseBody->eof() && $responseBody->isSeekable()) {
81+
$responseBody->seek(0, SEEK_END);
82+
}
83+
$responseBody->write($head . $body);
84+
85+
return $response;
86+
}
87+
88+
/**
7989
* @return ResponseInterface|null
8090
*/
8191
private function getStaticFile(UriInterface $uri)
@@ -139,11 +149,7 @@ private function getContentTypeByFileName($filename)
139149
'woff2' => 'application/font-woff2',
140150
];
141151

142-
if (isset($map[$ext])) {
143-
return $map[$ext];
144-
}
145-
146-
return 'text/plain';
152+
return isset($map[$ext]) ? $map[$ext] : 'text/plain';
147153
}
148154

149155
/**

0 commit comments

Comments
 (0)