Will allow you to define anonymous function as event listener callbacks for Shopware (4.2.0+)

dev-master 2015-10-10 19:45 UTC

This package is auto-updated.

Last update: 2024-06-04 14:35:27 UTC


The Shopware SubscriberInterface allows you to define subscribers in order to subscribe to Shopware events easily. The SubscriberInterface, however, does not support anonymous function as callbacks, basically because Shopware tries to be forward compatible to the Symfony EventDispatcher.

The LazySubscriber will provide you a simple way to define your callbacks as anonymous functions, which is especially comfortable, if you want to register a lot of services for Shopware's DIC. Using the LazySubscriber you can define your services as you would in e.g. Pimple.

How to use


If you want to use composer for this dependency, just run composer require dnoegel/lazy-subscriber.

The Subscriber

namespace YourPlugin\Subscriber;

class ContainerSubscriber extends LazySubscriber
    public function define()
        return [
            'my_plugin.cart' => function() {
                return new Cart();
            'my_plugin.persister' => function(DIC $c) {
                return new Persister($c->get('connection'));


In order to use your subscriber defined above, you need to register it during runtime.

class Shopware_Plugins_Frontend_YourPlugin_Bootstrap extends Shopware_Components_Plugin_Bootstrap
    // register namespaces for your plugin as well as for the lazy-subscriber library
    public function registerMyComponents()
        // you can use this
        require_once __DIR__ . '/vendor/autoload.php';
        // or this line to include the dependency:
            $this->Path() . '/vendor/dnoegel/lazy-subscriber/src'

    // install: register an early event
    public function install()

        return true;

    // Dynamically add your own subscriber
    public function onStartDispatch(Enlight_Event_EventArgs $args)

        $subscribers = array(
            new \Shopware\YourPlugin\Subscriber\ContainerSubscriber(Shopware()->Container())


        foreach ($subscribers as $subscriber) {

Should I use it?

It might be ok for populating the DI in your plugin as this is a bit cumbersome, especially during development. You should not use it for default events, and you should not use it as a way to define all your event subscribers in one big lazy subscriber. In addition to that be aware, that event subscribers works nicely - if you know what is going on behind the scenes and what to take care of. If this is not the case, you might want to stick to the "default event registration" way for the time being.