symfonydependency-injectionevent-dispatchingpluggable

Best way to develeop plugin compatible application. Dependency injection?


I'm wondering the best way to create fully compatible application to plug-ins.

I'm used to Wordpress plug-ins concept that you can define actions and filters and then use in your plug-ins. So others can define methods on their plug-ins that are executed when the action is called (or the filter).

My idea is create my app with some actions and filters and then other developers can build a Bundle that interfere in the "normal" app flow...

I was reading about Symfony2 Dependency Injection, but I didn’t found some comprehensive example to do something similar that I want.

EDIT:

What I did to allow other bundles to add items to my knp-menu menu.

In my base bundle:

Defining the filter that allow subscribber to get and set menu data:

# BaseBundle/Event/FilterMenuEvent.php

class FilterMenuEvent extends Event
{
    protected $menu;

    public function __construct($menu)
    {
        $this->menu = $menu;
    }

    public function getMenu()
    {
        return $this->menu;
    }
}

Defining the events of the menu:

# Event/MenuEvents.php
final class MenuEvents
{
    const BEFORE_ITEMS = 'menu.before.items';
    const AFTER_ITEMS = 'menu.after.items';
}

Setting up the subscriber:

# Event/MenuSubscriber.php
class MenuSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            'menu.after.items'     => array(
                array('homeItems', 9000),
                array('quickactionsItems', 80),
                array('adminItems', 70),
             ...
                array('logoutItems', -9000),
            )
        );
    }
    public function homeItems(FilterMenuEvent $menu_filter)
    {
        $menu = $menu_filter->getMenu();
        $menu->addChild('Home', array('route' => 'zashost_zaspanel_homepage'));
    }

    public function quickactionsItems(FilterMenuEvent $menu_filter)
    {
        $menu = $menu_filter->getMenu();
        $menu->addChild('Quick actions', array( 'route' => null));
        $menu['Quick actions']->addChild('Add hosting', array( 'route' => 'zashost_zaspanel_register_host'));
    }
}

Dispatching events in the generation of menu:

# Menu\Builder.php

class Builder extends ContainerAware
{
    public function userMenu(FactoryInterface $factory, array $options)
    {
        $menu = $factory->createItem('root');

        $this->container->get('event_dispatcher')->dispatch(MenuEvents::AFTER_ITEMS , new FilterMenuEvent($menu));

        return $menu;
    }
}

Attach subscriber to kernel event subscriber:

# services.yml
    services:
        # Menu items added with event listener
        base_menu_subscriber:
            class: Acme\BaseBundle\Event\MenuSubscriber
            arguments: ['@event_dispatcher']
            tags:
                - {name: kernel.event_subscriber}

Then in third party bundle:

Setting up my third party event subscriber:

class MenuSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            'menu.after.items'     => array('afterItems', 55)
        );
    }

    public function afterItems(FilterMenuEvent $menu_filter)
    {
        $menu = $menu_filter->getMenu();
        $menu->addChild('Backups', array( 'route' => null));
        $menu['Backups']->addChild('Create new backup', array( 'route' => null));
        return $menu;
    }
}

And attaching to kernel event subscriber:

# srevices.yml
services:
    menu_subscriber:
        class: Acme\ThirdPartyBundle\Event\MenuSubscriber
        arguments: ['@event_dispatcher']
        tags:
            - {name: kernel.event_subscriber}

In that way I can use the priority of Event Dispatcher to set the position of each group of items of the menu.


Solution

  • A good starting point in providing extension points for your application, in which other developers can hook their custom behaviour, is to use the EventDispatcher component from Symfony - a implementation of the Observer Pattern.

    Symfony already uses the component extensively in it's own core ( HttpKernel ) to allow other components (or plugins, if you will) to hook in various points in the http request -> response flow and handle everything from Request matching to Response generation.

    For example you can hook to the kernel.request event and return a Response immediately if the Request is not valid or to the kernel.response event and change the response content.

    See the full list of default KernelEvents.

    By only using these (there are many others related to other components), you can create a plugin sytem that is more capable, more testable and more robust than that of the Wordpress "platform".

    Of course, you can easily create and dispatch your own events that will suit your business logic (for example create events like post.created or comment.created) for a blog application.

    Now, for the sake of an example, here is how you will configure a "plugin" that will do something with the generated Response and then will fire another event (that can be used by another plugin)

    namespace Vendor;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\EventDispatcher\EventDispatcher;
    use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
    
    class ResponseAlter implements EventSubscriberInterface
    {
    
        private $dispatcher;
    
        public function __construct(EventDispatcher $dispatcher)
        {
            $this->dispatcher = $dispatcher;
        }
    
        public function doSomethingWithResponse(FilterResponseEvent $event)
        {
            $response = $event->getResponse();
    
            /**
             * let other plugins hook to the provide.footer event and
             * add the result to the response
             */
            $footer = new ProvideFooterEvent();
            $this->dispatcher->dispatch('provide.footer', $footer);
    
            $this->addFooterProvidedByPluginToResponse($response, $footer->getProvidedFooter());
    
            $event->setResponse($response);
        }
    
        static function getSubscribedEvents() 
        {
            return array(
                'kernel.response' => 'doSomethingWithResponse'
            );
        }
    }
    

    Now you will simply have to tag your service as a service subscriber and you're done. You've just plugged in the HttpKernel component:

    services:
        my_subscriber:
            class: Vendor\ResponseAlter
            arguments: ['@event_dispatcher']
            tags:
                - {name: kernel.event_subscriber}