Skip to content

Commit 393ef44

Browse files
committed
Fix tests, update implementation to use php 7.1 and update docs
1 parent 6a36fb6 commit 393ef44

17 files changed

+232
-94
lines changed

README.md

Lines changed: 2 additions & 12 deletions
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]([PSR-7](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,7 @@ 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:
35-
36-
```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-
]);
41-
```
42-
43-
more [about config manager](https://zendframework.github.io/zend-expressive/cookbook/modular-layout/).
33+
[Follow Zend Expressive documentation](https://docs.zendframework.com/zend-expressive/v3/features/modular-applications/).
4434

4535
### How to install on Slim 3?
4636

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
],
1313
"require": {
1414
"php": "^7.1",
15+
"maximebf/debugbar": "^1.4",
1516
"psr/http-server-handler": "^1.0",
1617
"psr/http-server-middleware": "^1.0",
17-
"maximebf/debugbar": "^1.15",
1818
"psr/container": "^1.0",
1919
"psr/http-message": "^1.0.1",
20-
"zendframework/zend-diactoros": "^1.7.1"
20+
"zendframework/zend-diactoros": "^1.1.3"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "^6.5",
23+
"phpunit/phpunit": "^7.1.2",
2424
"mikey179/vfsStream": "^1.6.4",
2525
"slim/slim": "^3.0",
2626
"zendframework/zend-expressive": "^3.0",

src/ConfigCollectorFactory.php

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
declare (strict_types=1);
23

34
namespace PhpMiddleware\PhpDebugBar;
45

56
use DebugBar\JavascriptRenderer as DebugBarRenderer;
6-
use PhpMiddleware\DoublePassCompatibilityTrait;
77
use Psr\Http\Message\MessageInterface;
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Http\Message\ServerRequestInterface;
@@ -21,7 +21,7 @@
2121
*
2222
* @author Witold Wasiczko <[email protected]>
2323
*/
24-
class PhpDebugBarMiddleware implements MiddlewareInterface
24+
final class PhpDebugBarMiddleware implements MiddlewareInterface
2525
{
2626
protected $debugBarRenderer;
2727

@@ -51,6 +51,26 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5151
return $this->prepareHtmlResponseWithDebugBar($response);
5252
}
5353

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+
5474
/**
5575
* @return HtmlResponse
5676
*/

src/PhpDebugBarMiddlewareFactory.php

Lines changed: 2 additions & 1 deletion
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();
Lines changed: 40 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 2 additions & 1 deletion
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)