Skip to content
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

Closed
gimler opened this issue Apr 27, 2012 · 3 comments
Closed

Add Plugin to all Clients from Service Builder #44

gimler opened this issue Apr 27, 2012 · 3 comments

Comments

@gimler
Copy link
Contributor

gimler commented Apr 27, 2012

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 the get method

@gimler
Copy link
Contributor Author

gimler commented Apr 27, 2012

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;
    }

@mtdowling
Copy link
Member

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?

@gimler
Copy link
Contributor Author

gimler commented May 2, 2012

nice this is the better way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants