Skip to content

Commit

Permalink
Add EventDispatcherInterface to container.
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jun 25, 2018
1 parent 219b99f commit 20cbf55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Package/EventsContainerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
use Limoncello\Contracts\Settings\SettingsProviderInterface;
use Limoncello\Events\Contracts\EventDispatcherInterface;
use Limoncello\Events\Contracts\EventEmitterInterface;
use Limoncello\Events\Package\EventSettings as C;
use Limoncello\Events\SimpleEventEmitter;
Expand All @@ -37,12 +38,27 @@ class EventsContainerConfigurator implements ContainerConfiguratorInterface
*/
public static function configureContainer(LimoncelloContainerInterface $container): void
{
$container[EventEmitterInterface::class] = function (PsrContainerInterface $container): EventEmitterInterface {
$emitter = new SimpleEventEmitter();
$cacheData = $container->get(SettingsProviderInterface::class)->get(C::class)[C::KEY_CACHED_DATA];
$emitter->setStaticSubscribers($cacheData);
$emitter = null;
$getOrCreateEmitter = function (PsrContainerInterface $container) use (&$emitter): SimpleEventEmitter {
if ($emitter === null) {
$emitter = new SimpleEventEmitter();
$cacheData = $container->get(SettingsProviderInterface::class)->get(C::class)[C::KEY_CACHED_DATA];
$emitter->setStaticSubscribers($cacheData);
}

return $emitter;
};

$container[EventEmitterInterface::class] =
function (PsrContainerInterface $container) use ($getOrCreateEmitter): EventEmitterInterface
{
return call_user_func($getOrCreateEmitter, $container);
};

$container[EventDispatcherInterface::class] =
function (PsrContainerInterface $container) use ($getOrCreateEmitter): EventDispatcherInterface
{
return call_user_func($getOrCreateEmitter, $container);
};
}
}
2 changes: 2 additions & 0 deletions tests/ContainerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
use Limoncello\Contracts\Container\ContainerInterface;
use Limoncello\Contracts\Settings\SettingsProviderInterface;
use Limoncello\Events\Contracts\EventDispatcherInterface;
use Limoncello\Events\Contracts\EventEmitterInterface;
use Limoncello\Events\Package\EventProvider;
use Limoncello\Events\Package\EventSettings as BaseEventSettings;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function testEventProvider()
$configuratorClass::configureContainer($container);

$this->assertNotNull($container->get(EventEmitterInterface::class));
$this->assertNotNull($container->get(EventDispatcherInterface::class));
}

/**
Expand Down

0 comments on commit 20cbf55

Please sign in to comment.