Skip to content

Commit 4021942

Browse files
authored
Merge pull request #17 from php-middleware/psr-15
psr-15
2 parents 95a1c4d + fa78861 commit 4021942

13 files changed

+75
-77
lines changed

Diff for: .travis.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
language: php
22

33
php:
4-
- 5.5
54
- 5.6
65
- 7.0
76
- 7.1
8-
- hhvm
97

108
env:
119
- DEPS=lowest
1210
- DEPS=latest
1311

1412
before_script:
13+
- phpenv config-rm xdebug.ini
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

Diff for: README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# phpdebugbar middleware [![Build Status](https://travis-ci.org/php-middleware/phpdebugbar.svg?branch=master)](https://travis-ci.org/php-middleware/phpdebugbar)
2-
PHP Debug bar middleware with PSR-7
2+
PHP Debug bar PSR-15 middleware with PSR-7
33

4-
This middleware provide framework-agnostic possibility to attach [PHP Debug bar](http://phpdebugbar.com/) to your response (html on non-html!).
4+
This middleware provide framework-agnostic possibility to attach [PHP Debug Bar](http://phpdebugbar.com/) to your response (html on non-html!).
55

66
## Installation
77

@@ -18,7 +18,7 @@ $middleware = new PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware($debugbarRende
1818

1919
// OR
2020

21-
$factory = PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory();
21+
$factory = new PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory();
2222
$middleware = $factory();
2323

2424
$app = new MiddlewareRunner();
@@ -61,7 +61,6 @@ $app->add($app->getContainer()->get('debugbar_middleware'));
6161
Put array with configuration into `config` service in your container:
6262

6363
```php
64-
<?php
6564
return [
6665
'phpmiddleware' => [
6766
'phpdebugbar' => [

Diff for: composer.json

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
{
22
"name": "php-middleware/php-debug-bar",
3-
"description": "PHP Debug bar middleware with PSR-7",
3+
"description": "PHP Debug Bar PSR-15 middleware with PSR-7",
44
"type": "library",
55
"keywords": [
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-
"maximebf/debugbar": "^1.10",
13+
"php": ">=5.6",
14+
"http-interop/http-middleware": "^0.4.1",
15+
"maximebf/debugbar": "^1.4",
16+
"php-middleware/double-pass-compatibility": "^1.0",
17+
"psr/container": "^1.0",
1418
"psr/http-message": "^1.0",
15-
"container-interop/container-interop": "^1.1",
1619
"zendframework/zend-diactoros": "^1.1.3"
1720
},
1821
"require-dev": {
19-
"phpunit/phpunit": "^4.8.6",
20-
"mikey179/vfsStream": "^1.6",
22+
"phpunit/phpunit": "^5.7.19 || ^6.1.3",
23+
"mikey179/vfsStream": "^1.6.4",
2124
"slim/slim": "^3.0",
22-
"zendframework/zend-expressive": "^1.0",
23-
"zendframework/zend-expressive-fastroute": "^1.0",
24-
"zendframework/zend-servicemanager": "^3.0"
25+
"zendframework/zend-expressive": "^1.0 || ^2.0",
26+
"zendframework/zend-expressive-fastroute": "^1.0 || ^2.0",
27+
"zendframework/zend-servicemanager": "^3.3"
2528
},
2629
"autoload": {
2730
"psr-4": {

Diff for: phpunit.xml

+1-1
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>

Diff for: src/ConfigCollectorFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PhpMiddleware\PhpDebugBar;
44

55
use DebugBar\DataCollector\ConfigCollector;
6-
use Interop\Container\ContainerInterface;
6+
use Psr\Container\ContainerInterface;
77

88
final class ConfigCollectorFactory
99
{

Diff for: src/JavascriptRendererFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use DebugBar\DebugBar;
66
use DebugBar\JavascriptRenderer;
7-
use Interop\Container\ContainerInterface;
7+
use Psr\Container\ContainerInterface;
88

99
final class JavascriptRendererFactory
1010
{

Diff for: src/PhpDebugBarMiddleware.php

+41-35
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 <[email protected]>
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
/**

Diff for: src/PhpDebugBarMiddlewareFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PhpMiddleware\PhpDebugBar;
44

55
use DebugBar\JavascriptRenderer;
6-
use Interop\Container\ContainerInterface;
6+
use Psr\Container\ContainerInterface;
77

88
final class PhpDebugBarMiddlewareFactory
99
{

Diff for: src/StandardDebugBarFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PhpMiddleware\PhpDebugBar;
44

55
use DebugBar\StandardDebugBar;
6-
use Interop\Container\ContainerInterface;
6+
use Psr\Container\ContainerInterface;
77

88
final class StandardDebugBarFactory
99
{

Diff for: test/AbstractMiddlewareRunnerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace PhpMiddlewareTest\PhpDebugBar;
44

5-
use PHPUnit_Framework_TestCase;
5+
use PHPUnit\Framework\TestCase;
66
use Psr\Http\Message\ResponseInterface;
77
use Psr\Http\Message\ServerRequestInterface;
88

9-
abstract class AbstractMiddlewareRunnerTest extends PHPUnit_Framework_TestCase
9+
abstract class AbstractMiddlewareRunnerTest extends TestCase
1010
{
1111

1212
final public function testAppendJsIntoHtmlContent()

Diff for: test/PhpDebugBarMiddlewareFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
66
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
7-
use PHPUnit_Framework_TestCase;
7+
use PHPUnit\Framework\TestCase;
88

99
/**
1010
* @author Witold Wasiczko <[email protected]>
1111
*/
12-
class PhpDebugBarMiddlewareFactoryTest extends PHPUnit_Framework_TestCase
12+
class PhpDebugBarMiddlewareFactoryTest extends TestCase
1313
{
1414
public function testFactory()
1515
{

Diff for: test/PhpDebugBarMiddlewareTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DebugBar\JavascriptRenderer;
66
use org\bovigo\vfs\vfsStream;
77
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
8-
use PHPUnit_Framework_TestCase;
8+
use PHPUnit\Framework\TestCase;
99
use Zend\Diactoros\Response;
1010
use Zend\Diactoros\ServerRequest;
1111
use Zend\Diactoros\Uri;
@@ -15,7 +15,7 @@
1515
*
1616
* @author Witold Wasiczko <[email protected]>
1717
*/
18-
class PhpDebugBarMiddlewareTest extends PHPUnit_Framework_TestCase
18+
class PhpDebugBarMiddlewareTest extends TestCase
1919
{
2020
protected $debugbarRenderer;
2121
protected $middleware;

Diff for: test/ZendExpressiveTest.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22

33
namespace PhpMiddlewareTest\PhpDebugBar;
44

5-
use Interop\Container\ContainerInterface;
65
use PhpMiddleware\PhpDebugBar\ConfigProvider;
6+
use Psr\Container\ContainerInterface;
7+
use Psr\Http\Message\ResponseInterface;
78
use Psr\Http\Message\ServerRequestInterface;
89
use Zend\Diactoros\Response\EmitterInterface;
910
use Zend\Diactoros\ServerRequestFactory;
1011
use Zend\Expressive\Container\ApplicationFactory;
1112
use Zend\ServiceManager\ServiceManager;
12-
use Zend\Stratigility\Http\ResponseInterface;
1313

1414
final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
1515
{
16-
private $testEmitter;
17-
18-
protected function setUp()
19-
{
20-
parent::setUp();
21-
22-
$this->testEmitter = new TestEmitter();
23-
}
24-
2516
final public function testContainsConfigCollectorOutput()
2617
{
2718
$response = $this->dispatchApplication([
@@ -58,7 +49,7 @@ protected function dispatchApplication(array $server, array $pipe = [])
5849

5950
$app->run($serverRequest);
6051

61-
return $this->testEmitter->getResponse();
52+
return $container->get(EmitterInterface::class)->getResponse();
6253
}
6354

6455
/**
@@ -71,7 +62,7 @@ private function createContainer()
7162

7263
$serviceManagerConfig = $config['dependencies'];
7364
$serviceManagerConfig['services']['config'] = $config;
74-
$serviceManagerConfig['services'][EmitterInterface::class] = $this->testEmitter;
65+
$serviceManagerConfig['services'][EmitterInterface::class] = new TestEmitter();
7566

7667
return new ServiceManager($serviceManagerConfig);
7768
}

0 commit comments

Comments
 (0)