php-middleware / php-debug-bar
PHP Debug Bar PSR-15 middleware with PSR-7
Installs: 211 077
Dependents: 3
Suggesters: 0
Security: 0
Stars: 74
Watchers: 6
Forks: 18
Open Issues: 5
Requires
- php: ^7.3 || ^8.0
- maximebf/debugbar: ^1.4
- psr/container-implementation: ^1.0 || ^2.0
- psr/http-factory-implementation: ^1.0
- psr/http-message-implementation: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.0
- laminas/laminas-servicemanager: ^3.3.2
- mezzio/mezzio: ^3.0
- mezzio/mezzio-fastroute: ^3.0.1
- mikey179/vfsstream: ^1.6.8
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.1.4
- slim/slim: ^3.0
README
PHP Debug Bar as framework-agnostic PSR-15 middleware with PSR-7 messages created by PSR-17 message factories. Also provides PSR-11 container invokable factories.
Framework-agnostic way to attach PHP Debug Bar to your response (html or non-html!).
Installation
composer require --dev php-middleware/php-debug-bar
To build middleware you need to inject DebugBar\JavascriptRenderer
(you can get it from DebugBar\StandardDebugBar
) inside PhpDebugBarMiddleware
and add it into your middleware runner:
$debugbar = new DebugBar\StandardDebugBar(); $debugbarRenderer = $debugbar->getJavascriptRenderer('/phpdebugbar'); $middleware = new PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware($debugbarRenderer, $psr17ResponseFactory, $psr17StreamFactory); // or use provided factory $factory = new PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory(); $middleware = $factory($psr11Container); $app = new MiddlewareRunner(); $app->add($middleware); $app->run($request, $response);
You don't need to copy any static assets from phpdebugbar vendor!
How to force disable or enable PHP Debug Bar?
Sometimes you want to have control when enable or disable PHP Debug Bar:
- custom content negotiation,
- allow debug redirects responses.
We allow you to disable attaching phpdebugbar using X-Enable-Debug-Bar: false
header, cookie or request attribute.
To force enable just send request with X-Enable-Debug-Bar
header, cookie or request attribute with true
value.
PSR-17
This package isn't require any PSR-7 implementation - you need to provide it by own. Middleware require ResponseFactory and StreamFactory interfaces. List of existing interfaces.
... and PSR-11
If you use provided PSR-11 factories, then your container must have services registered as PSR-17 interface's name. Example for laminas-diactoros implementation and Pimple:
$container[Psr\Http\Message\ResponseInterface::class] = new Laminas\Diactoros\ResponseFactory(); $container[Psr\Http\Message\StreamFactoryInterface::class] = new Laminas\Diactoros\StreamFactory();
How to install on Mezzio?
You need to register PhpMiddleware\PhpDebugBar\ConfigProvider
and pipe provided middleware:
$app->pipe(\PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class);
For more - follow Mezzio documentation.
How to install on Slim 3?
Register factories in container:
foreach (ConfigProvider::getConfig()['dependencies']['factories'] as $key => $factory) { $container[$key] = new $factory(); }
and add middleware from container to app:
$app->add( $app->getContainer()->get(\PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class) );
How to configure using existing factories?
Put array with a configuration into PhpMiddleware\PhpDebugBar\ConfigProvider
service in your container:
return [ 'phpmiddleware' => [ 'phpdebugbar' => [ 'javascript_renderer' => [ 'base_url' => '/phpdebugbar', ], 'collectors' => [ DebugBar\DataCollector\ConfigCollector::class, // Service names of collectors ], 'storage' => null, // Service name of storage ], ], ];
You can override existing configuration by merge default configuration with your own (example):
return array_merge(PhpMiddleware\PhpDebugBar\ConfigProvider::getConfig(), $myOverritenConfig);
It's just works with any modern php framework!
Middleware tested on:
And any other modern framework supported PSR-17 middlewares and PSR-7.