Code Monkey home page Code Monkey logo

Comments (5)

frankdejonge avatar frankdejonge commented on May 20, 2024

@andreigabreanu sorry for the late reply. I'd propose a different approach for this. In your situation I'd create a DI specific listener factory which would produce listeners that implement the ListenerInterface, which would resolve the dependencies in the handle function. This would solve your problem without making the event package itself be aware of any lazy loading (which would add significant complexity). Would that be an option for you?

from event.

praswicaksono avatar praswicaksono commented on May 20, 2024

@frankdejonge

create a DI specific listener factory which would produce listeners that implement the ListenerInterface

How to pass factory product to event?

this way below its not lazy load, since it will create object of listener

$emitter->addListener("some.event", $container->get("listener");

from event.

sagikazarmark avatar sagikazarmark commented on May 20, 2024

@Atriedes I don't think it is possible. When the emitter starts to call the listeners, it creates a list of them in the right order. There is no way to add listeners WHILE they are being invoked. You would have to create a decorator/extend the emitter, to make sure those listeners are added before the emitting starts.

Something like this:

<?php

use League\Container\ContainerInterface;
use League\Event\EmitterTrait;
use League\Event\EmitterInterface;

class LazyEmitter implements EmitterInterface
{
    use EmitterTrait;

    protected $container;

    /**
     * You have to add listeners to your container
     *
     * You should register them as singletons
     *
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function emit($event)
    {
        // ensure event is an event object
        // get its name: $name
        if (!$this->container->isSingleton($name)) {
            $listeners = $this->container[$name];
            is_array($listeners) or $listeners = [$listeners];

            foreach ($listeners as $listener) {
                $this->getEmitter()->addListener($name, $listener);
            }
        }

        return call_user_func_array([$this->getEmitter(), 'emit'], func_get_args());
    }
}

from event.

frankdejonge avatar frankdejonge commented on May 20, 2024

My proposed solution was a little bit different. I'd make the listener resolving the responsibility of a listener decorator instead of a Emitter replacement.

A littler decorator would look something like this:

<?php

class LazyListener implements ListenerInterface
{
    protected $container;
    protected $containerKey;
    protected $listener;

    public function __construct(Container $container, $containerKey,)
    {
        $this->container = $container;
        $this->containerKey = $containerKey;
    }

    public function handle(EventInterface $event)
    {
        $arguments = [$event] + func_get_args();
        $listener = $this->resolveListener();

        return call_user_func_array([$listener, 'handle'], $arguments);
    }

    protected function resolveListener()
    {
        if (! $this->listener) {
            $this->listener = $this->container->get($this->containerKey);
        }

        return $this->listener;
    }

    public function getContainerKey()
    {
        return $this->containerKey;
    }

    public function isListener($listener)
    {
        if ($listeners instanceof LazyListener && $this->containerKey === $listener->getContainerKey) {
            return true;
        }

        return false;
    }
}

You'd use this, with changing the container implementation details to the one you're using, like so:

$container = new Container();
// .. configure container to have the listener which needs deps
$emitter = new Emitter;
$emitter->addListener('event.name', new LazyListener($container, 'some.dependency.key'));
$emitter->emit('event.name');

from event.

praswicaksono avatar praswicaksono commented on May 20, 2024

Thanks all for comments, right now I decided to extend Emitter and make them container aware. I need to override 3 methods addListener, InvokeListener, ensureListener

public function addListener($event, $listener, $priority = self::P_NORMAL)
{
    // I just remove this to make sure I can pass reference only
    // $listener = $this->ensureListener($listener);

    if (! isset($this->listeners[$event])) {
        $this->listeners[$event] = [];
    }

    $this->listeners[$event][] = [$listener, $priority];
    $this->clearSortedListeners($event);
    return $this;
}

We check and resolve listener later when there are invoked

protected function invokeListeners($name, EventInterface $event, array $arguments)
{
    $listeners = $this->getListeners($name);

    foreach ($listeners as $listener) {
        if ($event->isPropagationStopped()) {
            break;
        }

        // resolve listener
        $listener = $this->ensureListener($listener);

        call_user_func_array([$listener, 'handle'], $arguments);
    }
}

Finally if pass reference, resolve it in container otherwise throw exception

protected function ensureListener($listener)
{
    if ($listener instanceof ListenerInterface) {
        return $listener;
    }

    if (is_callable($listener)) {
        return CallbackListener::fromCallable($listener);
    }

    // If we pass reference in containter then resolve it
    if (($listener = $this->container->get($listener)) != null) {
        return $listener
    }

    throw new InvalidArgumentException('Listeners should be be ListenerInterface, Closure or callable. Received type: '.gettype($listener));
}

With this way all listener can resolve when event get invoked.

from event.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.