mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-25 10:33:18 +01:00
add ServiceBuilderPlugin
This commit is contained in:
parent
9586d67b66
commit
fc467d24fe
50
src/Guzzle/Service/Plugin/ServiceBuilderPlugin.php
Normal file
50
src/Guzzle/Service/Plugin/ServiceBuilderPlugin.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Guzzle\Service\Plugin;
|
||||
|
||||
use Guzzle\Common\Event;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Service builder plugin to add plugins to all service clients
|
||||
*
|
||||
* @author Gordon Franke <info@nevalon.de>
|
||||
*/
|
||||
class ServiceBuilderPlugin implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var $plugins array plugins to add
|
||||
*/
|
||||
private $plugins = array();
|
||||
|
||||
/**
|
||||
* @param array $plugins plugins to add
|
||||
*/
|
||||
public function __construct(array $plugins)
|
||||
{
|
||||
$this->plugins = $plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'service_builder.create_client' => 'onCreateClient'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add plugins when client whould create
|
||||
*
|
||||
* @param Event $event
|
||||
*/
|
||||
public function onCreateClient(Event $event)
|
||||
{
|
||||
foreach ($this->plugins as $plugin) {
|
||||
$event['client']->addSubscriber($plugin);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Guzzle\Tests\Service;
|
||||
|
||||
use Guzzle\Service\Builder\ServiceBuilder;
|
||||
use Guzzle\Service\Plugin\ServiceBuilderPlugin;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
class ServiceBuilderPluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
/**
|
||||
* @covers Guzzle\Service\Plugin\ServiceBuilderPlugin
|
||||
*/
|
||||
public function testPluginPassPluginsThroughToClients()
|
||||
{
|
||||
$s = new ServiceBuilder(array(
|
||||
'michael.mock' => array(
|
||||
'class' => 'Guzzle\\Tests\\Service\\Mock\\MockClient',
|
||||
'params' => array(
|
||||
'base_url' => 'http://www.test.com/',
|
||||
'subdomain' => 'michael',
|
||||
'password' => 'test',
|
||||
'username' => 'michael',
|
||||
)
|
||||
)
|
||||
));
|
||||
|
||||
$plugin = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface');
|
||||
|
||||
$plugin::staticExpects($this->any())
|
||||
->method('getSubscribedEvents')
|
||||
->will($this->returnValue(array('client.create_request' => 'onRequestCreate')));
|
||||
|
||||
$s->addSubscriber(new ServiceBuilderPlugin(array($plugin)));
|
||||
|
||||
$c = $s->get('michael.mock');
|
||||
$this->assertTrue($c->getEventDispatcher()->hasListeners('client.create_request'));
|
||||
|
||||
$listeners = $c->getEventDispatcher()->getListeners('client.create_request');
|
||||
$this->assertSame($plugin, $listeners[0][0]);
|
||||
$this->assertEquals('onRequestCreate', $listeners[0][1]);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user