New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Plugin to all Clients from Service Builder #44
Comments
i use the following code in my Service builder class at the moment. I can make a pull request for that when i would be accepted? private $plugins = array();
/**
* {@inheritdoc}
*/
public function get($name, $throwAway = false)
{
if (!isset($this->builderConfig[$name])) {
throw new ClientNotFoundException('No client is registered as ' . $name);
}
if (!$throwAway && isset($this->clients[$name])) {
return $this->clients[$name];
}
$client = parent::get($name, $throwAway);
foreach ($this->getPlugins() as $plugin) {
$client->getEventDispatcher()->addSubscriber($plugin);
}
return $client;
}
public function addPlugin($plugin)
{
$this->plugins[] = $plugin;
}
public function getPlugins()
{
return $this->plugins;
} |
What do you think about making the ServiceBuilder own an EventDispatcher, and make it so that the ServiceBuilder emits an event each time a client is created? Then you could add a listener for this event that decorates the created clients in whatever way you see fit (adds plugins, sets configs, etc). <?php
$builder->getEventDispatcher()->addListener('service_builder.create_client', function(Event $event) use ($plugins) {
// Add plugins to the client
foreach ($plugins as $plugin) {
$event['client']->getEventDispatcher()->addSubscriber($plugin);
}
// Do more stuff other than add plugins
$event['client']->getConfig()->set('foo', 'bar');
}); I think something like the above would be a bit more flexible, but it does add a bit of code to the caller. What do you think? |
nice this is the better way |
Is there a way to add a Plugin to all clients getting from service builder.
My use case i build a data collector for GuzzlebBundle (Symfony2) the problem is that i must now add my plugin to all clients. So the user use the
$this->get('guzzle.service_builder')->get('myClient')
to get the client. Is there a simple way or should i must extends the service builder class with my own and extends theget
methodThe text was updated successfully, but these errors were encountered: