gremo / buzz-bundle
此包已被废弃,不再维护。未建议替代包。
Symfony Bundle,用于使用轻量级的Buzz HTTP客户端。
v1.1.0
2016-05-29 00:12 UTC
Requires
- php: >=5.3.2
- kriswallsmith/buzz: >= 0.6,<1.0
- symfony/config: ~2.0|~3.0
- symfony/dependency-injection: ~2.0|~3.0
- symfony/http-kernel: ~2.0|~3.0
README
Symfony Bundle,用于使用轻量级的Buzz HTTP客户端。
安装
将此包添加到您的 composer.json
文件中
{ "require": { "gremo/buzz-bundle": "~1.0" } }
然后运行 composer update
并在 app/appKernel.php
中注册此包到您的内核
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Gremo\BuzzBundle\GremoBuzzBundle(), // ... ); }
旧版Symfony (2.0.*)
将以下内容添加到您的 deps
文件中
[buzz]
git=https://github.com/kriswallsmith/Buzz.git
[GremoBuzzBundle]
git=https://github.com/gremo/GremoBuzzBundle.git
target=bundles/Gremo/BuzzBundle
然后运行 php bin/vendors update
并使用自动加载器注册命名空间(app/autoload.php
)
<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'Buzz' => __DIR__.'/../vendor/buzz/lib', 'Gremo' => __DIR__.'/../vendor/bundles', // ... ));
最后在 app/appKernel.php
中将此包注册到您的内核
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Gremo\BuzzBundle\GremoBuzzBundle(), // ... ); }
配置
不需要配置。可用选项和类型(默认值见Buzz\Client\AbstractClient
)
# GremoBuzzBundle Configuration gremo_buzz: client: "native" # allowed "curl", "multi_curl" or "native" options: ignore_errors: ~ # boolean max_redirects: ~ # integer proxy: ~ # string timeout: ~ # integer verify_host: ~ # integer verify_peer: ~ # boolean
使用方法
从服务容器中获取 gremo_buzz
服务
/** @var $browser \Buzz\Browser */ $browser = $this->get('gremo_buzz');
有关发送HTTP请求,请参考 Kris Wallsmith Buzz库。
依赖注入标签
您可以通过创建一个实现 Buzz\Listener\ListenerInterface
的服务并标记为 gremo_buzz.listener
(可选地定义一个 priority
属性)来注册一个监听器。更高的优先级意味着相应的监听器将首先执行。
示例监听器,用于记录出站请求
<?php use Buzz\Listener\ListenerInterface; use Buzz\Message\MessageInterface; use Buzz\Message\RequestInterface; use JMS\DiExtraBundle\Annotation as DI; use Psr\Log\LoggerInterface; /** * @DI\Service("buzz.listener.logger") * @DI\Tag("gremo_buzz.listener", attributes={"priority"=10}) */ class BuzzLoggerListener implements ListenerInterface { /** * @var \Psr\Log\LoggerInterface */ private $logger; /** * @var float */ private $startTime; /** * @DI\InjectParams({"logger" = @DI\Inject("logger")}) */ public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * {@inheritdoc} */ public function preSend(RequestInterface $request) { $this->startTime = microtime(true); } /** * {@inheritdoc} */ public function postSend(RequestInterface $request, MessageInterface $response) { $this->logger->info(sprintf( 'Sent "%s %s%s" in %dms', $request->getMethod(), $request->getHost(), $request->getResource(), round((microtime(true) - $this->startTime) * 1000) )); } }
请注意,此示例使用新的 Psr\Log\LoggerInterface
,可能不适用于旧版本的Symfony。