Skip to main content

Prophiler PSR-7 Middleware

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

Prophiler is a PHP Profiler & Developer Toolbar which is part of the Phalcon project but can also be used as a stand-alone component, kind of like the Symfony Web Debug Toolbar or Z-Ray. What I like about Prophiler is that on the one hand it is super easy to install and on the other hand it offers a few nice adapters (e.g. PSR-3 logging or Doctrine integration) out-of-the-box.

Since we are currently working on a PSR-7 / ADR middleware for our own internal framework I was looking for a way to include Prophiler as a PSR-7 middleware in the stratigility environment we are using. This lead to the creation of the prophiler-psr7-middleware component which can be installed via Composer.

composer.phar require bitexpert/prophiler-psr7-middleware

First of all you need to create an Prophiler instance as well as a Toolbar instance. The toolbar will get injected in the middleware later:

$prophiler = new \Fabfuel\Prophiler\Profiler();
$toolbar = new \Fabfuel\Prophiler\Toolbar($prophiler);

Given you are also using zendframework/zend-stratigility your set-up should look fairly similar to this:

$request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
$response = \Zend\Diactoros\Response();

$app = new \Zend\Stratigility\MiddlewarePipe();

Ideally you would hook in the middleware as the first to run:

$app->pipe(new \bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware($toolbar));

Now run the pipe and send the response, e.g. by using the SapiEmitter.

$response = $app($request, $response);
$emitter = new \Zend\Diactoros\Response\SapiEmitter();

Fairly simple, isn't it? The Prophiler middleware will append the toolbar content for responses which are writeable and have the Content-Type set to "text/html" which means your JSON API won't be messed up (hopefully).