pinkeen / api-debug-bundle
允许通过 Web 调试工具栏扩展轻松进行 API 消费者调试。
Requires
- php: >=5.5.9
- guzzlehttp/psr7: ~1.0
- psr/http-message: ~1.0
- symfony/framework-bundle: >=3.3
- symfony/symfony: >=3.3
Suggests
- guzzlehttp/guzzle: Install for easy API calling and integrated debugging.
README
此包添加了一个 Web 调试工具栏标签,用于显示关于 API 消费者请求的信息。
它旨在通用,并允许轻松与 SDK 和 HTTP 客户端库集成。
目前它支持开箱即用的 Guzzle 6。
它应该可以非常容易地与任何使用 PSR-7 消息的 HTTP 客户端集成。
对于兼容 Guzzle4
的版本,请使用 v1.0
标签。
对于兼容 Symfony < 3.3
的版本,请使用 v2.0
标签。
要求
- PHP 5.5.9
- Symfony 3.3
安装
常规 Symfony 操作。
在 composer.json 中需要:"pinkeen/api-debug-bundle": "dev-master",
。
在 AppKernel.php 中需要:new Pinkeen\ApiDebugBundle\PinkeenApiDebugBundle(),
。
如果您想查看原始正文数据,请将以下内容添加到您的 app/config/routing_dev.yml
中
_api_debug: resource: "@PinkeenApiDebugBundle/Resources/config/routing.yml" prefix: /_profiler
服务
新的 symfony 方法
除了 GuzzleClientFactory 和 RingPHPHandlerFactory 之外的所有服务都是私有的,这意味着您不能通过 $container->get() 直接从容器中获取服务。
它们也自动注册并设置为自动注入,您只需将类型提示的服务添加到您的类中,作为构造函数的参数即可。
// src/AppBundle/Service/FooService.php // ... use Pinkeen\ApiDebugBundle\Bridge\Guzzle\Middleware\DataCollectorMiddleware; class FooService { private $dataCollectorMiddleware; public function __construct(DataCollectorMiddleware $dataCollectorMiddleware) { $this->dataCollectorMiddleware = $dataCollectorMiddleware; } // ... }
用法
与您的自定义客户端集成
首先,您必须子类化 AbstractCallData
,它包含单个 API 请求的数据。
如果您使用的是 PSR-7 兼容的客户端,则可以使用 PsrCallData
而不是编写自己的数据类。
然后,每次您的 API 消费者发起请求时,请发送一个 ApiEvents::API_CALL
事件。
use Pinkeen\ApiDebugBundle\ApiEvents; use Pinkeen\ApiDebugBundle\Event\ApiCallEvent; /* ... */ /** @var $eventDispatcher \Symfony\Component\EventDispatcher\EventDispatcher */ $eventDispatcher->dispatch( ApiEvents::API_CALL, new ApiCallEvent( new YourApiCallData(/* your params */) ) );
Guzzle
这里有两种选择:
让包为您创建客户端...
/** @var $guzzleClientFactory \Pinkeen\ApiDebugBundle\Bridge\Guzzle\Service\GuzzleClientFactory */ $guzzleClientFactory->create([ /* Guzzle client config (optional). * It is passed directly to GuzzleHttp\Client constructor. */ ]);
...或者将收集器处理器推送到您的中间件栈。
$handler = new GuzzleHttp\HandlerStack(new GuzzleHttp\Handler\CurlHandler()); /** @var $dataCollectorMiddleware \Pinkeen\ApiDebugBundle\Bridge\Guzzle\Middleware\DataCollectorMiddleware */ $dataCollectorMiddlewareHandler = $dataCollectorMiddleware->getHandler(); $handler->push($dataCollectorMiddlewareHandler); $client = new GuzzleHttp\Client(['handler' => $handler]);
RingPHP
让包为您创建处理器
/** @var $ringPHPHandlerFactory \Pinkeen\ApiDebugBundle\Bridge\RingPHP\Service\RingPHPHandlerFactory */ $handler = $ringPHPHandlerFactory->create(new CurlHandler());
使用 collector_middleware 服务创建您的 RingPHP 中间件,并将其包装在基本处理器周围
/** @var $dataCollectorMiddleware \Pinkeen\ApiDebugBundle\Bridge\RingPHP\Middleware\DataCollectorMiddleware */ $ringPhpHandler = $dataCollectorMiddleware->createHandler(new GuzzleHttp\Ring\Client\CurlHandler(), 'apiname');
PS 与 elasticsearch-php 2.0 优雅集成。
生产环境
对于生产环境,您可能希望跳过所有数据收集。
您应该自己处理这个问题,除非您使用 Pinkeen\ApiDebugBundle\Bridge\Guzzle\Service\GuzzleClientFactory
或 Pinkeen\ApiDebugBundle\Bridge\RingPHP\Service\RingPHPHandlerFactory
,这些在非调试模式下会跳过它。
注意
我没有找到从 guzzle6 获取调用持续时间的方法,因此这里有一个回归。如果有人有想法,请告诉我。