Skip to content

Commit 6c06189

Browse files
authored
Merge pull request #20 from prolic/expressive3
Support Expressive 3
2 parents adeb04d + 48bcd9c commit 6c06189

18 files changed

+250
-105
lines changed

.travis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
cache:
2+
directories:
3+
- $HOME/.composer/cache/files
4+
15
language: php
26

37
php:
4-
- 5.6
5-
- 7.0
68
- 7.1
9+
- 7.2
710

811
env:
912
- DEPS=lowest
1013
- DEPS=latest
1114

1215
before_script:
1316
- phpenv config-rm xdebug.ini
14-
- composer self-update
1517
- if [[ $DEPS == 'lowest' ]]; then composer update --prefer-stable --no-interaction --prefer-lowest ; fi
1618
- if [[ $DEPS == 'latest' ]]; then composer update --prefer-stable --no-interaction ; fi
1719

README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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 PSR-15 middleware with PSR-7
2+
PHP Debug bar [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware with [PSR-7](https://www.php-fig.org/psr/psr-7/). Also supports [PSR-11](https://www.php-fig.org/psr/psr-11/)
33

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

@@ -30,17 +30,13 @@ You don't need to copy any static assets from phpdebugbar vendor!
3030

3131
### How to install on Zend Expressive?
3232

33-
Use [mtymek/expressive-config-manager](https://github.com/mtymek/expressive-config-manager) and add
34-
`PhpMiddleware\PhpDebugBar\ConfigProvider` class name:
33+
You need to register ConfigProvider and pipe provided middleware:
3534

3635
```php
37-
$configManager = new \Zend\Expressive\ConfigManager\ConfigManager([
38-
\PhpMiddleware\PhpDebugBar\ConfigProvider::class,
39-
new \Zend\Expressive\ConfigManager\PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
40-
]);
36+
$app->pipe(PhpDebugBarMiddleware::class);
4137
```
4238

43-
more [about config manager](https://zendframework.github.io/zend-expressive/cookbook/modular-layout/).
39+
For more follow Zend Expressive [documentation](https://docs.zendframework.com/zend-expressive/v3/features/modular-applications/).
4440

4541
### How to install on Slim 3?
4642

composer.json

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@
88
"middleware",
99
"psr",
1010
"psr-7",
11-
"psr-15"
11+
"psr-15",
12+
"psr-11"
1213
],
1314
"require": {
14-
"php": ">=5.6",
15-
"http-interop/http-middleware": "^0.4.1",
15+
"php": "^7.1",
1616
"maximebf/debugbar": "^1.4",
17-
"php-middleware/double-pass-compatibility": "^1.0",
17+
"psr/http-server-handler": "^1.0",
18+
"psr/http-server-middleware": "^1.0",
1819
"psr/container": "^1.0",
19-
"psr/http-message": "^1.0",
20+
"psr/http-message": "^1.0.1",
2021
"zendframework/zend-diactoros": "^1.1.3"
2122
},
2223
"require-dev": {
23-
"phpunit/phpunit": "^5.7.19 || ^6.1.3",
24+
"phpunit/phpunit": "^7.1.2",
2425
"mikey179/vfsStream": "^1.6.4",
2526
"slim/slim": "^3.0",
26-
"zendframework/zend-expressive": "^1.0 || ^2.0",
27-
"zendframework/zend-expressive-fastroute": "^1.0 || ^2.0",
28-
"zendframework/zend-servicemanager": "^3.3"
27+
"zendframework/zend-expressive": "^3.0",
28+
"zendframework/zend-expressive-fastroute": "^3.0.1",
29+
"zendframework/zend-servicemanager": "^3.3.2"
2930
},
3031
"autoload": {
3132
"psr-4": {

src/ConfigCollectorFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

@@ -7,7 +8,7 @@
78

89
final class ConfigCollectorFactory
910
{
10-
public function __invoke(ContainerInterface $container)
11+
public function __invoke(ContainerInterface $container): ConfigCollector
1112
{
1213
$data = $container->get('config');
1314

src/ConfigProvider.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

56
final class ConfigProvider
67
{
7-
public static function getConfig()
8+
public static function getConfig(): array
89
{
910
$self = new self();
1011
return $self();
1112
}
1213

13-
public function __invoke()
14+
public function __invoke(): array
1415
{
1516
$config = include __DIR__ . '/../config/phpdebugbar.config.php';
1617
$config['dependencies'] = include __DIR__ . '/../config/dependency.config.php';

src/JavascriptRendererFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

@@ -8,7 +9,7 @@
89

910
final class JavascriptRendererFactory
1011
{
11-
public function __invoke(ContainerInterface $container = null)
12+
public function __invoke(ContainerInterface $container = null): JavascriptRenderer
1213
{
1314
if ($container === null || !$container->has(DebugBar::class)) {
1415
$standardDebugBarFactory = new StandardDebugBarFactory();

src/PhpDebugBarMiddleware.php

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

56
use DebugBar\JavascriptRenderer as DebugBarRenderer;
6-
use Interop\Http\ServerMiddleware\DelegateInterface;
7-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
8-
use PhpMiddleware\DoublePassCompatibilityTrait;
97
use Psr\Http\Message\MessageInterface;
108
use Psr\Http\Message\ResponseInterface;
119
use Psr\Http\Message\ServerRequestInterface;
1210
use Psr\Http\Message\UriInterface;
11+
use Psr\Http\Server\MiddlewareInterface;
12+
use Psr\Http\Server\RequestHandlerInterface;
1313
use Slim\Http\Uri;
1414
use Zend\Diactoros\Response;
1515
use Zend\Diactoros\Response\HtmlResponse;
@@ -21,10 +21,8 @@
2121
*
2222
* @author Witold Wasiczko <[email protected]>
2323
*/
24-
class PhpDebugBarMiddleware implements MiddlewareInterface
24+
final class PhpDebugBarMiddleware implements MiddlewareInterface
2525
{
26-
use DoublePassCompatibilityTrait;
27-
2826
protected $debugBarRenderer;
2927

3028
public function __construct(DebugBarRenderer $debugbarRenderer)
@@ -35,13 +33,13 @@ public function __construct(DebugBarRenderer $debugbarRenderer)
3533
/**
3634
* @inheritDoc
3735
*/
38-
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
36+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
3937
{
4038
if ($staticFile = $this->getStaticFile($request->getUri())) {
4139
return $staticFile;
4240
}
4341

44-
$response = $delegate->process($request);
42+
$response = $handler->handle($request);
4543

4644
if (!$this->isHtmlAccepted($request)) {
4745
return $response;
@@ -53,6 +51,26 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
5351
return $this->prepareHtmlResponseWithDebugBar($response);
5452
}
5553

54+
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
55+
{
56+
$handler = new class($next, $response) implements RequestHandlerInterface {
57+
private $next;
58+
private $response;
59+
60+
public function __construct(callable $next, ResponseInterface $response)
61+
{
62+
$this->next = $next;
63+
$this->response = $response;
64+
}
65+
66+
public function handle(ServerRequestInterface $request): ResponseInterface
67+
{
68+
return ($this->next)($request, $this->response);
69+
}
70+
};
71+
return $this->process($request, $handler);
72+
}
73+
5674
/**
5775
* @return HtmlResponse
5876
*/

src/PhpDebugBarMiddlewareFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

@@ -7,7 +8,7 @@
78

89
final class PhpDebugBarMiddlewareFactory
910
{
10-
public function __invoke(ContainerInterface $container = null)
11+
public function __invoke(ContainerInterface $container = null): PhpDebugBarMiddleware
1112
{
1213
if ($container === null || !$container->has(JavascriptRenderer::class)) {
1314
$rendererFactory = new JavascriptRendererFactory();
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare (strict_types=1);
3+
4+
namespace PhpMiddleware\PhpDebugBar\ResponseInjector;
5+
6+
use DebugBar\JavascriptRenderer;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Zend\Diactoros\Response\HtmlResponse;
9+
use Zend\Diactoros\Response\Serializer;
10+
11+
final class AlwaysInjector implements ResponseInjectorInterface
12+
{
13+
public function injectPhpDebugBar(ResponseInterface $response, JavascriptRenderer $debugBarRenderer): ResponseInterface
14+
{
15+
$debugBarHead = $debugBarRenderer->renderHead();
16+
$debugBarBody = $debugBarRenderer->render();
17+
18+
if ($this->isHtmlResponse($outResponse)) {
19+
$body = $outResponse->getBody();
20+
if (! $body->eof() && $body->isSeekable()) {
21+
$body->seek(0, SEEK_END);
22+
}
23+
$body->write($debugBarHead . $debugBarBody);
24+
25+
return $outResponse;
26+
}
27+
28+
$outResponseBody = Serializer::toString($outResponse);
29+
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
30+
$escapedOutResponseBody = htmlspecialchars($outResponseBody);
31+
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody);
32+
33+
return new HtmlResponse($result);
34+
}
35+
36+
private function isHtmlResponse(ResponseInterface $response): bool
37+
{
38+
return $this->hasHeaderContains($response, 'Content-Type', 'text/html');
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare (strict_types=1);
3+
4+
namespace PhpMiddleware\PhpDebugBar\ResponseInjector;
5+
6+
use DebugBar\JavascriptRenderer;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* @author Witold Wasiczko <[email protected]>
11+
*/
12+
interface ResponseInjectorInterface
13+
{
14+
public function injectPhpDebugBar(ResponseInterface $response, JavascriptRenderer $debugBar): ResponseInterface;
15+
}

src/StandardDebugBarFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

@@ -7,7 +8,7 @@
78

89
final class StandardDebugBarFactory
910
{
10-
public function __invoke(ContainerInterface $container = null)
11+
public function __invoke(ContainerInterface $container = null): StandardDebugBar
1112
{
1213
$debugBar = new StandardDebugBar();
1314

test/AbstractMiddlewareRunnerTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddlewareTest\PhpDebugBar;
45

56
use PHPUnit\Framework\TestCase;
67
use Psr\Http\Message\ResponseInterface;
78
use Psr\Http\Message\ServerRequestInterface;
9+
use Zend\Diactoros\Response;
810

911
abstract class AbstractMiddlewareRunnerTest extends TestCase
1012
{
1113

12-
final public function testAppendJsIntoHtmlContent()
14+
final public function testAppendJsIntoHtmlContent(): void
1315
{
1416
$response = $this->dispatchApplication([
1517
'REQUEST_URI' => '/hello',
1618
'REQUEST_METHOD' => 'GET',
1719
'HTTP_ACCEPT' => 'text/html',
1820
], [
19-
'/hello' => function (ServerRequestInterface $request, ResponseInterface $response, $next) {
21+
'/hello' => function (ServerRequestInterface $request) {
22+
$response = new Response();
2023
$response->getBody()->write('Hello!');
2124
return $response;
2225
},
@@ -29,7 +32,7 @@ final public function testAppendJsIntoHtmlContent()
2932
$this->assertContains('"/phpdebugbar/debugbar.js"', $responseBody);
3033
}
3134

32-
final public function testGetStatics()
35+
final public function testGetStatics(): void
3336
{
3437
$response = $this->dispatchApplication([
3538
'DOCUMENT_ROOT' => __DIR__,
@@ -53,8 +56,5 @@ final public function testGetStatics()
5356
$this->assertContains('text/javascript', $contentType);
5457
}
5558

56-
/**
57-
* @return ResponseInterface
58-
*/
59-
abstract protected function dispatchApplication(array $server, array $pipe = []);
59+
abstract protected function dispatchApplication(array $server, array $pipe = []): ResponseInterface;
6060
}

test/PhpDebugBarMiddlewareFactoryTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddlewareTest\PhpDebugBar;
45

@@ -11,7 +12,7 @@
1112
*/
1213
class PhpDebugBarMiddlewareFactoryTest extends TestCase
1314
{
14-
public function testFactory()
15+
public function testFactory(): void
1516
{
1617
$factory = new PhpDebugBarMiddlewareFactory();
1718

0 commit comments

Comments
 (0)