Skip to content

Commit

Permalink
Merge pull request #20 from prolic/expressive3
Browse files Browse the repository at this point in the history
Support Expressive 3
  • Loading branch information
snapshotpl committed Apr 11, 2018
2 parents adeb04d + 48bcd9c commit 6c06189
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 105 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
cache:
directories:
- $HOME/.composer/cache/files

language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2

env:
- DEPS=lowest
- DEPS=latest

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

Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# phpdebugbar middleware [![Build Status](https://travis-ci.org/php-middleware/phpdebugbar.svg?branch=master)](https://travis-ci.org/php-middleware/phpdebugbar)
PHP Debug bar PSR-15 middleware with PSR-7
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/)

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

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

### How to install on Zend Expressive?

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

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

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

### How to install on Slim 3?

Expand Down
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
"middleware",
"psr",
"psr-7",
"psr-15"
"psr-15",
"psr-11"
],
"require": {
"php": ">=5.6",
"http-interop/http-middleware": "^0.4.1",
"php": "^7.1",
"maximebf/debugbar": "^1.4",
"php-middleware/double-pass-compatibility": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"psr/container": "^1.0",
"psr/http-message": "^1.0",
"psr/http-message": "^1.0.1",
"zendframework/zend-diactoros": "^1.1.3"
},
"require-dev": {
"phpunit/phpunit": "^5.7.19 || ^6.1.3",
"phpunit/phpunit": "^7.1.2",
"mikey179/vfsStream": "^1.6.4",
"slim/slim": "^3.0",
"zendframework/zend-expressive": "^1.0 || ^2.0",
"zendframework/zend-expressive-fastroute": "^1.0 || ^2.0",
"zendframework/zend-servicemanager": "^3.3"
"zendframework/zend-expressive": "^3.0",
"zendframework/zend-expressive-fastroute": "^3.0.1",
"zendframework/zend-servicemanager": "^3.3.2"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion src/ConfigCollectorFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar;

Expand All @@ -7,7 +8,7 @@

final class ConfigCollectorFactory
{
public function __invoke(ContainerInterface $container)
public function __invoke(ContainerInterface $container): ConfigCollector
{
$data = $container->get('config');

Expand Down
5 changes: 3 additions & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar;

final class ConfigProvider
{
public static function getConfig()
public static function getConfig(): array
{
$self = new self();
return $self();
}

public function __invoke()
public function __invoke(): array
{
$config = include __DIR__ . '/../config/phpdebugbar.config.php';
$config['dependencies'] = include __DIR__ . '/../config/dependency.config.php';
Expand Down
3 changes: 2 additions & 1 deletion src/JavascriptRendererFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar;

Expand All @@ -8,7 +9,7 @@

final class JavascriptRendererFactory
{
public function __invoke(ContainerInterface $container = null)
public function __invoke(ContainerInterface $container = null): JavascriptRenderer
{
if ($container === null || !$container->has(DebugBar::class)) {
$standardDebugBarFactory = new StandardDebugBarFactory();
Expand Down
34 changes: 26 additions & 8 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar;

use DebugBar\JavascriptRenderer as DebugBarRenderer;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PhpMiddleware\DoublePassCompatibilityTrait;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Http\Uri;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
Expand All @@ -21,10 +21,8 @@
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class PhpDebugBarMiddleware implements MiddlewareInterface
final class PhpDebugBarMiddleware implements MiddlewareInterface
{
use DoublePassCompatibilityTrait;

protected $debugBarRenderer;

public function __construct(DebugBarRenderer $debugbarRenderer)
Expand All @@ -35,13 +33,13 @@ public function __construct(DebugBarRenderer $debugbarRenderer)
/**
* @inheritDoc
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($staticFile = $this->getStaticFile($request->getUri())) {
return $staticFile;
}

$response = $delegate->process($request);
$response = $handler->handle($request);

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

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
{
$handler = new class($next, $response) implements RequestHandlerInterface {
private $next;
private $response;

public function __construct(callable $next, ResponseInterface $response)
{
$this->next = $next;
$this->response = $response;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
return ($this->next)($request, $this->response);
}
};
return $this->process($request, $handler);
}

/**
* @return HtmlResponse
*/
Expand Down
3 changes: 2 additions & 1 deletion src/PhpDebugBarMiddlewareFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar;

Expand All @@ -7,7 +8,7 @@

final class PhpDebugBarMiddlewareFactory
{
public function __invoke(ContainerInterface $container = null)
public function __invoke(ContainerInterface $container = null): PhpDebugBarMiddleware
{
if ($container === null || !$container->has(JavascriptRenderer::class)) {
$rendererFactory = new JavascriptRendererFactory();
Expand Down
40 changes: 40 additions & 0 deletions src/ResponseInjector/AlwaysInjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar\ResponseInjector;

use DebugBar\JavascriptRenderer;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\Serializer;

final class AlwaysInjector implements ResponseInjectorInterface
{
public function injectPhpDebugBar(ResponseInterface $response, JavascriptRenderer $debugBarRenderer): ResponseInterface
{
$debugBarHead = $debugBarRenderer->renderHead();
$debugBarBody = $debugBarRenderer->render();

if ($this->isHtmlResponse($outResponse)) {
$body = $outResponse->getBody();
if (! $body->eof() && $body->isSeekable()) {
$body->seek(0, SEEK_END);
}
$body->write($debugBarHead . $debugBarBody);

return $outResponse;
}

$outResponseBody = Serializer::toString($outResponse);
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
$escapedOutResponseBody = htmlspecialchars($outResponseBody);
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody);

return new HtmlResponse($result);
}

private function isHtmlResponse(ResponseInterface $response): bool
{
return $this->hasHeaderContains($response, 'Content-Type', 'text/html');
}
}
15 changes: 15 additions & 0 deletions src/ResponseInjector/ResponseInjectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar\ResponseInjector;

use DebugBar\JavascriptRenderer;
use Psr\Http\Message\ResponseInterface;

/**
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
interface ResponseInjectorInterface
{
public function injectPhpDebugBar(ResponseInterface $response, JavascriptRenderer $debugBar): ResponseInterface;
}
3 changes: 2 additions & 1 deletion src/StandardDebugBarFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare (strict_types=1);

namespace PhpMiddleware\PhpDebugBar;

Expand All @@ -7,7 +8,7 @@

final class StandardDebugBarFactory
{
public function __invoke(ContainerInterface $container = null)
public function __invoke(ContainerInterface $container = null): StandardDebugBar
{
$debugBar = new StandardDebugBar();

Expand Down
14 changes: 7 additions & 7 deletions test/AbstractMiddlewareRunnerTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<?php
declare (strict_types=1);

namespace PhpMiddlewareTest\PhpDebugBar;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

abstract class AbstractMiddlewareRunnerTest extends TestCase
{

final public function testAppendJsIntoHtmlContent()
final public function testAppendJsIntoHtmlContent(): void
{
$response = $this->dispatchApplication([
'REQUEST_URI' => '/hello',
'REQUEST_METHOD' => 'GET',
'HTTP_ACCEPT' => 'text/html',
], [
'/hello' => function (ServerRequestInterface $request, ResponseInterface $response, $next) {
'/hello' => function (ServerRequestInterface $request) {
$response = new Response();
$response->getBody()->write('Hello!');
return $response;
},
Expand All @@ -29,7 +32,7 @@ final public function testAppendJsIntoHtmlContent()
$this->assertContains('"/phpdebugbar/debugbar.js"', $responseBody);
}

final public function testGetStatics()
final public function testGetStatics(): void
{
$response = $this->dispatchApplication([
'DOCUMENT_ROOT' => __DIR__,
Expand All @@ -53,8 +56,5 @@ final public function testGetStatics()
$this->assertContains('text/javascript', $contentType);
}

/**
* @return ResponseInterface
*/
abstract protected function dispatchApplication(array $server, array $pipe = []);
abstract protected function dispatchApplication(array $server, array $pipe = []): ResponseInterface;
}
3 changes: 2 additions & 1 deletion test/PhpDebugBarMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare (strict_types=1);

namespace PhpMiddlewareTest\PhpDebugBar;

Expand All @@ -11,7 +12,7 @@
*/
class PhpDebugBarMiddlewareFactoryTest extends TestCase
{
public function testFactory()
public function testFactory(): void
{
$factory = new PhpDebugBarMiddlewareFactory();

Expand Down
Loading

0 comments on commit 6c06189

Please sign in to comment.