playbloom/guzzle-bundle

为 Guzzle 提供 Symfony2 网页分析器

安装次数: 309,312

依赖项: 1

建议者: 1

安全性: 0

星标: 82

关注者: 5

分支: 21

开放问题: 4

类型:symfony-bundle

v1.1.0 2015-05-12 04:26 UTC

This package is auto-updated.

Last update: 2024-09-21 17:35:04 UTC


README

为 Guzzle 提供基本的记录器和高级分析器

  • 基本记录器使用默认的 Symfony 应用程序记录器,在生产环境中使用是安全的。
  • 高级分析器用于调试目的,将在工具栏和 Symfony 网页分析器中显示专用报告。

Guzzle Symfony web profiler panel

Guzzle Symfony web profiler panel - request details

Guzzle Symfony web profiler panel - response details

安装

添加 composer 需求

{
    "require-dev": {
        "playbloom/guzzle-bundle": "v1.1.0"
    },
}

将捆绑包添加到您的 Symfony 应用程序内核中

<?php
    // in %your_project%/app/AppKernel.php
    $bundles[] = new Playbloom\Bundle\GuzzleBundle\PlaybloomGuzzleBundle();
?>

要启用高级分析器和工具栏/网页分析器面板,将此行添加到您的 app/config/config_dev.yml

playbloom_guzzle:
    web_profiler: true

Guzzle 客户端作为 Symfony 服务

具体的 Guzzle 客户端创建 可以通过 Symfony 服务容器轻松管理,这得益于一个简单的 工厂配置,在这种情况下,您只需使用 playbloom_guzzle.client 标记您的 guzzle 服务(们)。

它将为您的客户端添加基本记录器。如果当前环境中启用了 web_profiler,它还将添加高级分析器并在 Symfony 工具栏/网页分析器中显示报告。

<service id="acme.client"
    class="%acme.client.class%"
    factory-class="%acme.client.class%"
    factory-method="factory">
    <!-- your arguments -->
    <tag name="playbloom_guzzle.client" />
</service>

手动将记录器/分析器添加到 Guzzle 客户端

如果您需要手动处理记录器或分析器插件的注册,您可以从中检索这些服务。

<?php

$client = new \Guzzle\Http\Client('https://my.api.com');

// basic logger service plugged & configured with the default Symfony app logger
$loggerPlugin = $container->get('playbloom_guzzle.client.plugin.logger');
$client->addSubscriber($loggerPlugin);

// advanced profiler for developement and debug, requires web_profiler to be enabled
$profilerPlugin = $container->get('playbloom_guzzle.client.plugin.profiler');
$client->addSubscriber($profilerPlugin);

?>

自定义您的分析器面板

如果您需要一个 自定义分析器面板,您可以轻松扩展/重用此捆绑包中的数据收集器和分析器模板。

例如,您有一个与 Github API 交互的 GithubBundle。您还有一个用于调试开发的 Github 分析器面板,并且您想在面板中分析 API 请求。

这相当简单:首先,定义自己的 GithubDataCollector,它扩展了 Playbloom\Bundle\GuzzleBundle\DataCollector\GuzzleDataCollector

然后扩展 Guzzle 网页分析器模板

{% extends 'PlaybloomGuzzleBundle:Collector:guzzle.html.twig' %}

{% block panel %}
    <div class="github">
        <h2>Github</h2>
        <ul>
            <li><strong>Github API key:</strong> {{ collector.getApiKey }}</li>
            <!-- Some custom information -->
        </ul>
    </div>

    {% include 'PlaybloomGuzzleBundle:Profiler:requests.html.twig' with {'requests': collector.requests } %}
{% endblock %}

最后声明您的数据收集器

<service id="data_collector.github" class="Acme\GithubBundle\DataCollector\GithubDataCollector">
    <argument type="service" id="playbloom_guzzle.client.plugin.profiler"/>
    <tag name="data_collector"
        template="AcmeGithubBundle:Collector:github"
        id="github"/>
</service>

就这样,现在您的分析器面板显示了您的自定义信息和 Guzzle API 请求。

待办事项

  • 添加有关客户端配置本身的额外信息(感谢 guzzle 服务构建器?)
  • 为 HTTP 请求添加客户端|主机|端点|时间过滤器

许可

此捆绑包处于 MIT 许可之下。请参阅捆绑包中的完整许可。

致谢

  • Swagger 用于 UI

Bitdeli Badge