bitexpert/prophiler-psr7-middleware

此包已被弃用且不再维护。未建议替代包。

Prophiler 的 PSR-7 中间件

v0.6.0 2017-10-08 11:19 UTC

README

此包提供基于 zendframework/zend-stratigility 包的简洁 PSR-7 中间件实现。该中间件负责将 Prophiler 工具栏输出添加到响应对象。

Build Status Coverage Status

安装

安装 bitexpert/prophiler-psr7-middleware 的首选方式是通过 Composer。只需将 bitexpert/prophiler-psr7-middleware 添加为依赖项

composer.phar require bitexpert/prophiler-psr7-middleware

如何使用 Prophiler PSR7 中间件

创建 Prophiler 工具栏

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

设置 PSR-7 中间件,例如使用 zendframework/zend-stratigility

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

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

将 ProphilerMiddleware 添加到中间件管道

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

“执行”中间件管道

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

如何将 Prophiler PSR7 中间件添加到 Expressive 应用

Expressive 1.x

config/autoload/middleware-pipeline.local.php 中注册一个预路由中间件

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

config/autoload/dependencies.global.php 中添加一个工厂定义

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

\App\Middleware\ProphilerFactory 实现如下所示

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 (程序化管道)

将以下代码片段添加到 config/pipeline.php 文件中

$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);

配置 Prophiler

默认情况下,Prophiler 不会记录或分析任何内容。您可以通过向要分析代码中添加以下代码片段来添加自定义基准测试

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

// here be your custom code

$profiler->stop();

此外,Prophiler 为第三方工具和库提供许多 适配器和装饰器

许可证

Prophiler PSR7 中间件是在 Apache 2.0 许可下发布的。