bitExpert / prophiler-psr7-middleware

[DEPRECATED] Prophiler PSR7 Middleware
Apache License 2.0
7 stars 8 forks source link

The debug bar isn't rendered when returning a HtmlResponse #1

Closed geerteltink closed 8 years ago

geerteltink commented 8 years ago

As mentioned in zendframework/zend-expressive#181

If you return a HtmlResponse in an Action the application stops processing other middleware: example.

The solution that worked for me is wrapping the real app inside another debug app:

$profiler = new \Fabfuel\Prophiler\Profiler();
$profiler->addAggregator(new \Fabfuel\Prophiler\Aggregator\Database\QueryAggregator());
$profiler->addAggregator(new \Fabfuel\Prophiler\Aggregator\Cache\CacheAggregator());

$toolbar = new \Fabfuel\Prophiler\Toolbar($profiler);
//$toolbar->addDataCollector(new \Fabfuel\Prophiler\DataCollector\Request());

/** @var \Zend\Expressive\Application $app */
$app = $container->get('Zend\Expressive\Application');

$debugMiddleware = \Zend\Expressive\AppFactory::create();
$debugMiddleware->pipe(new \bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware($toolbar));
$debugMiddleware->pipe(function ($request, $response, $next) use ($app, $toolbar) {
    // Add PSR-7 Request DataCollector to toolbar
    $response = $app($request, $response);
    // Add PSR-7 Response DataCollector to toolbar
    return $response;
});
$debugMiddleware->run();

Also if it pretends to be PSR-7 compliant, it would be nice to have a real PSR-7 request datacollector.

shochdoerfer commented 8 years ago

@xtreamwayz thanks for the error report. I will have a look at it in the next couple of days (hopefully). Also thanks for the idea of an real PSR-7 request datacollector, that totally makes sense.

shochdoerfer commented 8 years ago

I am not able to reproduce the problem. These are the steps I took:

I registered a pre_routing middleware in config/autoload/middleware-pipeline.global.php

return [
    'middleware_pipeline' => [
        'pre_routing' => [
            [
                'middleware' => bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware::class,
            ],
        ],
        'post_routing' => [
        ],
    ],
];

I added a factory definition for \bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware to config/autoload/dependencies.global.php

return [
    'dependencies' => [
        'invokables' => [
            App\Action\PingAction::class => App\Action\PingAction::class,
        ],
        'factories' => [
            App\Action\HomePageAction::class => App\Action\HomePageFactory::class,
            Zend\Expressive\Application::class => Zend\Expressive\Container\ApplicationFactory::class,
            bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware::class => App\Middleware\ProphilerFactory::class
        ]
    ]
];

The \App\Middleware\ProphilerFactory looks like this:

namespace App\Middleware;

use Interop\Container\ContainerInterface;

class ProphilerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $prophiler = new \Fabfuel\Prophiler\Profiler();
        $toolbar = new \Fabfuel\Prophiler\Toolbar($prophiler);
        return new \bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware($toolbar);
    }
}
geerteltink commented 8 years ago

Thanx, it's working now after following these steps. Not sure what part I missed before.