Coder Social home page Coder Social logo

prophiler-psr7-middleware's Introduction

prophiler-psr7-middleware

This package provides a slim PSR-7 middleware implementation based on the zendframework/zend-stratigility package. The middleware is responsible for "adding" the Prophiler Toolbar output to the Response object.

Build Status Coverage Status

Installation

The preferred way of installing bitexpert/prophiler-psr7-middleware is through Composer. Simply add bitexpert/prophiler-psr7-middleware as a dependency:

composer.phar require bitexpert/prophiler-psr7-middleware

How to use the Prophiler PSR7 Middleware

Create the Prophiler toolbar:

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

Set-up your PSR-7 middleware, e.g. by using zendframework/zend-stratigility:

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

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

Add the ProphilerMiddleware to the Middleware pipe:

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

"Execute" the Middleware pipe:

    $response = $app($request, $response);

How to add the Prophiler PSR7 Middleware to an Expressive app

Expressive 1.x

Register a pre_routing middleware in config/autoload/middleware-pipeline.local.php:

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

Add a factory definition to config/autoload/dependencies.global.php:

return [
    'dependencies' => [
        'factories' => [
            bitExpert\Http\Middleware\Psr7\Prophiler\
                ProphilerMiddleware::class =>
                App\Middleware\ProphilerFactory::class
        ]
    ]
];

The \App\Middleware\ProphilerFactory implementation looks like this:

namespace App\Middleware;

use Interop\Container\ContainerInterface;
use Fabfuel\Prophiler\Profiler;
use Fabfuel\Prophiler\Toolbar;
use bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware;

class ProphilerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $prophiler = new Profiler();
        $toolbar = new Toolbar($prophiler);
        return new ProphilerMiddleware($toolbar);
    }
}

Expressive 2.x (Programmatic Pipelines)

Adding the following code snippet to the config/pipeline.php file:

$app->pipe(ErrorHandler::class);

$debug = $app->getContainer()->get('config')['debug'] ?? false;
if ($debug) {
   $prophiler  = new \Fabfuel\Prophiler\Profiler();
   $toolbar    = new \Fabfuel\Prophiler\Toolbar($prophiler);
   $middleware = new \bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware($toolbar);
   $app->pipe($middleware);
}

$app->pipe(ServerUrlMiddleware::class);

Configure Prophiler

By default Prophiler will not log or profile anything. You can add custom benchmarks by adding the following code snippet to the code you want to profile:

$profiler->start('\My\Class::doSomeOtherThing', ['additional' => 'information'], 'My Component');

// here be your custom code

$profiler->stop();

In addition to that Prophiler offers a lot of adapters and decorators for 3rd party tools and libraries.

License

The Prophiler PSR7 Middleware is released under the Apache 2.0 license.

prophiler-psr7-middleware's People

Contributors

shochdoerfer avatar weierophinney avatar belgattitude avatar lowtower avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.