diff --git a/dist/DependencyInjection/CompilerPass/SubscriberPass.php b/dist/DependencyInjection/CompilerPass/SubscriberPass.php index 8f7b3721..7f18ee13 100644 --- a/dist/DependencyInjection/CompilerPass/SubscriberPass.php +++ b/dist/DependencyInjection/CompilerPass/SubscriberPass.php @@ -30,17 +30,12 @@ public function process(ContainerBuilder $container) return; } - $clients = $container->findTaggedServiceIds('csa_guzzle.client'); - - if (!count($clients)) { - return; - } - - foreach ($clients as $id => $options) { - $client = $container->findDefinition($id); - foreach ($subscribers as $subscriber => $options) { - $client->addMethodCall('addSubscriber', [new Reference($subscriber)]); - } + // Factory + $factory = $container->findDefinition('csa_guzzle.client_factory'); + $arg = []; + foreach ($subscribers as $subscriber => $options) { + $arg[] = new Reference($subscriber); } + $factory->replaceArgument(1, $arg); } } diff --git a/dist/Factory/ClientFactory.php b/dist/Factory/ClientFactory.php index fa867822..0d2c9a0c 100644 --- a/dist/Factory/ClientFactory.php +++ b/dist/Factory/ClientFactory.php @@ -11,6 +11,9 @@ namespace Csa\Bundle\GuzzleBundle\Factory; +use GuzzleHttp\Event\HasEmitterInterface; +use GuzzleHttp\Event\SubscriberInterface; + /** * Csa Guzzle client compiler pass * @@ -19,17 +22,28 @@ class ClientFactory { private $class; + private $subscribers; /** - * @param string $class The client's class + * @param string $class The client's class + * @param SubscriberInterface[] $subscribers A list of subscribers to attach to each client */ - public function __construct($class) + public function __construct($class, array $subscribers = []) { $this->class = $class; + $this->subscribers = $subscribers; } public function create(array $options = []) { - return new $this->class($options); + $client = new $this->class($options); + + if ($client instanceof HasEmitterInterface) { + foreach ($this->subscribers as $subscriber) { + $client->getEmitter()->attach($subscriber); + } + } + + return $client; } } diff --git a/dist/Resources/config/factory.xml b/dist/Resources/config/factory.xml index c811302f..87466293 100644 --- a/dist/Resources/config/factory.xml +++ b/dist/Resources/config/factory.xml @@ -13,6 +13,7 @@ %csa_guzzle.client.class% +