Skip to content

Commit

Permalink
Merge pull request #2 from Micro-PHP/release-1.1
Browse files Browse the repository at this point in the history
v1.1 release
  • Loading branch information
Asisyas authored Nov 25, 2022
2 parents ac870b0 + c4429c3 commit aa99bc8
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 7 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Micro Framework: Application plugin to provide an event emitter",
"type": "library",
"license": "MIT",
"version": "1.0",
"version": "1.1",
"autoload": {
"psr-4": {
"Micro\\Plugin\\EventEmitter\\": "src/"
Expand All @@ -17,6 +17,8 @@
],
"require": {
"micro/event-emitter": "^1",
"micro/kernel": "^1"
"micro/plugin-locator": "^1",
"micro/kernel": "^1",
"micro/autowire": "^1"
}
}
33 changes: 33 additions & 0 deletions src/Business/Factory/EventEmitterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Factory;

use Micro\Component\EventEmitter\EventEmitterInterface;
use Micro\Component\EventEmitter\EventEmitterFactory as BaseEventEmitterFactory;
use Micro\Plugin\EventEmitter\Business\Provider\ProviderFactoryInterface;

class EventEmitterFactory extends BaseEventEmitterFactory
{
/**
* @param ProviderFactoryInterface $providerFactoryInterface
*/
public function __construct(
private readonly ProviderFactoryInterface $providerFactoryInterface
)
{
}

/**
* {@inheritDoc}
*/
public function create(): EventEmitterInterface
{
$emitter = parent::create();

$emitter->addListenerProvider(
$this->providerFactoryInterface->create()
);

return $emitter;
}
}
32 changes: 32 additions & 0 deletions src/Business/Locator/EventListenerClassLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Locator;

use Micro\Component\EventEmitter\EventListenerInterface;
use Micro\Plugin\Locator\Facade\LocatorFacadeInterface;

class EventListenerClassLocator implements EventListenerClassLocatorInterface
{
/**
* @param LocatorFacadeInterface $locatorFacade
*/
public function __construct(
private readonly LocatorFacadeInterface $locatorFacade
)
{
}

/**
* {@inheritDoc}
*/
public function lookupListenerClasses(): iterable
{
$listenerClassCollection = [];

foreach ($this->locatorFacade->lookup(EventListenerInterface::class) as $listenerClass) {
$listenerClassCollection[] = $listenerClass;
}

return $listenerClassCollection;
}
}
27 changes: 27 additions & 0 deletions src/Business/Locator/EventListenerClassLocatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Locator;

use Micro\Plugin\Locator\Facade\LocatorFacadeInterface;

class EventListenerClassLocatorFactory implements EventListenerClassLocatorFactoryInterface
{
/**
* @param LocatorFacadeInterface $locatorFacade
*/
public function __construct(
private readonly LocatorFacadeInterface $locatorFacade
)
{
}

/**
* {@inheritDoc}
*/
public function create(): EventListenerClassLocatorInterface
{
return new EventListenerClassLocator(
$this->locatorFacade,
);
}
}
11 changes: 11 additions & 0 deletions src/Business/Locator/EventListenerClassLocatorFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Locator;

interface EventListenerClassLocatorFactoryInterface
{
/**
* @return EventListenerClassLocatorInterface
*/
public function create(): EventListenerClassLocatorInterface;
}
13 changes: 13 additions & 0 deletions src/Business/Locator/EventListenerClassLocatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Locator;

use Micro\Component\EventEmitter\EventListenerInterface;

interface EventListenerClassLocatorInterface
{
/**
* @return iterable<class-string<EventListenerInterface>>
*/
public function lookupListenerClasses(): iterable;
}
61 changes: 61 additions & 0 deletions src/Business/Provider/ApplicationListenerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Provider;

use Micro\Component\DependencyInjection\Autowire\AutowireHelperInterface;
use Micro\Component\EventEmitter\EventInterface;
use Micro\Component\EventEmitter\ListenerProviderInterface;

class ApplicationListenerProvider implements ListenerProviderInterface
{
/**
* @param AutowireHelperInterface $autowireHelper
* @param iterable $eventListenersClasses
*/
public function __construct(
private readonly AutowireHelperInterface $autowireHelper,
private readonly iterable $eventListenersClasses,
)
{
}

/**
* {@inheritDoc}
*/
public function getListenersForEvent(EventInterface $event): iterable
{
foreach ($this->getEventListeners() as $listenerClass) {
if(!$listenerClass::supports($event)) {
continue;
}

$callback = $this->autowireHelper->autowire($listenerClass);

yield $callback();
}
}

/**
* {@inheritDoc}
*/
public function __toString(): string
{
return $this->getName() ?? get_class($this);
}

/**
* {@inheritDoc}
*/
public function getName(): string
{
return 'events.listener_provider.default';
}

/**
* {@inheritDoc}
*/
public function getEventListeners(): iterable
{
return $this->eventListenersClasses;
}
}
48 changes: 48 additions & 0 deletions src/Business/Provider/ProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Provider;

use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactoryInterface;
use Micro\Component\DependencyInjection\Autowire\AutowireHelperInterface;
use Micro\Component\EventEmitter\EventListenerInterface;
use Micro\Component\EventEmitter\ListenerProviderInterface;
use Micro\Plugin\EventEmitter\Business\Locator\EventListenerClassLocatorFactoryInterface;

class ProviderFactory implements ProviderFactoryInterface
{
/**
* @param AutowireHelperInterface $autowireHelper
* @param EventListenerClassLocatorFactoryInterface $eventListenerClassLocatorFactory
*/
public function __construct(
private readonly AutowireHelperInterface $autowireHelper,
private readonly EventListenerClassLocatorFactoryInterface $eventListenerClassLocatorFactory
)
{
}

/**
*{@inheritDoc}
*/
public function create(): ListenerProviderInterface
{
$listeners = $this->eventListenerClassLocatorFactory
->create()
->lookupListenerClasses();

return $this->createProvider($listeners);
}

/**
* @param iterable<class-string<EventListenerInterface>> $listeners
*
* @return ListenerProviderInterface
*/
protected function createProvider(iterable $listeners): ListenerProviderInterface
{
return new ApplicationListenerProvider(
$this->autowireHelper,
$listeners
);
}
}
13 changes: 13 additions & 0 deletions src/Business/Provider/ProviderFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Micro\Plugin\EventEmitter\Business\Provider;

use Micro\Component\EventEmitter\ListenerProviderInterface;

interface ProviderFactoryInterface
{
/**
* @return ListenerProviderInterface
*/
public function create(): ListenerProviderInterface;
}
57 changes: 52 additions & 5 deletions src/EventEmitterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@

namespace Micro\Plugin\EventEmitter;

use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactory;
use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactoryInterface;
use Micro\Component\DependencyInjection\Autowire\AutowireHelperInterface;
use Micro\Component\DependencyInjection\Container;
use Micro\Component\EventEmitter\EventEmitterFactory;
use Micro\Component\EventEmitter\EventEmitterFactoryInterface;
use Micro\Framework\Kernel\Plugin\AbstractPlugin;
use Micro\Framework\Kernel\Plugin\DependencyProviderInterface;
use Micro\Plugin\EventEmitter\Business\Facade\EventsFacade;
use Micro\Plugin\EventEmitter\Business\Factory\EventEmitterFactory;
use Micro\Plugin\EventEmitter\Business\Locator\EventListenerClassLocatorFactory;
use Micro\Plugin\EventEmitter\Business\Locator\EventListenerClassLocatorFactoryInterface;
use Micro\Plugin\EventEmitter\Business\Provider\ProviderFactory;
use Micro\Plugin\EventEmitter\Business\Provider\ProviderFactoryInterface;
use Micro\Plugin\Locator\Facade\LocatorFacadeInterface;

class EventEmitterPlugin extends AbstractPlugin
class EventEmitterPlugin implements DependencyProviderInterface
{
/**
* @var LocatorFacadeInterface
*/
private readonly LocatorFacadeInterface $locatorFacade;

/**
* @var AutowireHelperInterface $autowireHelper
*/
private readonly AutowireHelperInterface $autowireHelper;

/**
* {@inheritDoc}
*/
public function provideDependencies(Container $container): void
{
$container->register(EventsFacadeInterface::class, function (Container $container) {
$container->register(EventsFacadeInterface::class, function (
AutowireHelperInterface $autowireHelper,
LocatorFacadeInterface $locatorFacade,
) {
$this->locatorFacade = $locatorFacade;
$this->autowireHelper = $autowireHelper;

return $this->createFacade();
});
}
Expand All @@ -33,6 +57,29 @@ protected function createFacade(): EventsFacadeInterface
*/
protected function createEventEmitterFactory(): EventEmitterFactoryInterface
{
return new EventEmitterFactory();
return new EventEmitterFactory(
$this->createProviderFactory()
);
}

/**
* @return ProviderFactoryInterface
*/
protected function createProviderFactory(): ProviderFactoryInterface
{
return new ProviderFactory(
$this->autowireHelper,
$this->createEventListenerClassLocatorFactory(),
);
}

/**
* @return EventListenerClassLocatorFactoryInterface
*/
protected function createEventListenerClassLocatorFactory(): EventListenerClassLocatorFactoryInterface
{
return new EventListenerClassLocatorFactory(
$this->locatorFacade
);
}
}

0 comments on commit aa99bc8

Please sign in to comment.