5.2 Plugin System: using subscribers

Hi guys

I’m implementing my first 5.2 plugin and I’m trying to use services.xml to define my subscribers, but I’m having some issues on Subscriber considering the implementation I used before 5.2.

Before we had Bootstrap file and we could have this:

 /**
     * Registers all necessary events and hooks.
     */
    public function subscribeEvents()
    {
        $this->subscribeEvent('Enlight_Controller_Front_DispatchLoopStartup', 'onStartDispatch');
    }


    /**
     * Create Subscribers
     *
     * @param Enlight_Event_EventArgs $args
     */
    public function onStartDispatch(\Enlight_Event_EventArgs $args)
    {
        $this->Application()->Events()->addSubscriber(new Subscribers\Backend($this));
        $this->Application()->Events()->addSubscriber(new Subscribers\Frontend($this));
    }

This was a way to load subscribers dynamically. And an example of my old Backend subscriber:

bootstrap = $bootstrap;
    }


    public static function getSubscribedEvents()
    {
        return array(
            'Enlight_Controller_Action_PostDispatchSecure_Backend_Article' => array(
                'onArticlePostDispatch'
            )
        );
    }


    public function onArticlePostDispatch(Enlight_Event_EventArgs $args)
    {
        /** @var \Enlight_Controller_Action $controller */
        $controller = $args->getSubject();
        $view = $controller->View();
        $request = $controller->Request();

        $view->addTemplateDir($this->bootstrap->Path() . '/Views');

        $this->bootstrap->Application()->Snippets()->addConfigDir(
            $this->bootstrap->Path() . 'Snippets/'
        );

        if ($request->getActionName() === 'load') {
            $args->getSubject()->View()->extendsTemplate('backend/my_plugin/model/attribute.js');
        }
    }
}

So, on subscriber we had access to Bootrap context so we could access Path(), Application(), etc.

My question is how can I access this vars using the recomended 5.2 Plugin System. Defining the subscriber on services.xml and without Bootstrap I can’t access Path() neither other stuff we need on Subscribers, like adding snippets as I show on example above.

Thanks for help.

Best

Carlos

Hi,

yes, you cannot access the “new” Plugin-Bootstrap easily in your subscriber - actually it was intended to be so :). Usually I suggest to inject the DIC into the subscriber like this:

It is totally fine to use the DIC as a service registry in a subscriber, as it mostly contains glue code anyway. The “Application” you used before was basically the same as “Shopware()” which again is mostly used to access services you can also access via DIC - so using the DIC is perfectly fine. Of course you can still inject more specific dependencies, if you want to.

Regarding templates and those kind of tasks: Just access $dic->get(‘template’)->addTemplateDir (). The only thing which is actually missing is the Path() helper in the subscriber - again this was actually intentional, as __DIR__ will be fine in most cases. Some developers also compile their plugin’s path into the DIC, so can access it, see https://github.com/shyim/shopware-profiler/blob/master/ShyimProfiler.php#L30

Don’t be confused by these changes too much. We know we removed some patterns that were popular in the past - but actually we did that to enforce a cleaner and more maintainable system, which is fun to develop for external developers. So of course feedback is also appreciated :slight_smile:

Best regards,

Daniel

1 „Gefällt mir“