Comments (5)
@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.
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.
@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.
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.
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)
- Double invoked events with name same as their classname HOT 5
- How do you remove listeners?
- [docs] Typo in event/2.0/events/classes.md
- [docs] Typo in event/2.0/generator/trait.md
- Question: Can the Emitter be made ContainerAware? HOT 3
- Additional Arguments throw exception when using AbstractListener HOT 7
- Upcoming PSR HOT 3
- waitFor method HOT 4
- Ditch EventInterface HOT 5
- What is this repository for? HOT 1
- BufferedEmitter as service HOT 1
- Has this package been abandoned? HOT 2
- Fatal error: Uncaught Error: Class 'League\Event\Emitter' not found HOT 1
- New release? HOT 6
- Add the ability to have wildcard listener to be fired before all other listeners HOT 1
- Upgrade from 2 to 3 documentation is... incomplete... HOT 1
- What would be proper approach to add information to an event? HOT 2
- Stoppable Events HOT 5
- Is it possible to provide a working example? HOT 1
- Documentation issues HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from event.