Skip to content

Commit

Permalink
common interface
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Feb 2, 2019
1 parent 5b02124 commit 47d4811
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 131 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env:
- COMPOSER_FLAGS="--prefer-stable --prefer-dist"

php:
- 7.2
- 7.3
- nightly

matrix:
Expand All @@ -22,7 +22,7 @@ matrix:
- php: 7.1
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-stable --prefer-dist"
- php: 7.1
- php: 7.2
env:
- TEST_VERSION=true
- COMPOSER_FLAGS="--prefer-stable --prefer-dist"
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
"friendsofphp/php-cs-fixer": "^2.0",
"infection/infection": "^0.9",
"phpmd/phpmd": "^2.0",
"phpstan/phpstan": "^0.10",
"phpstan/phpstan-deprecation-rules": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-deprecation-rules": "^0.11",
"phpstan/phpstan-strict-rules": "^0.11",
"phpunit/phpunit": "^6.0|^7.0",
"povils/phpmnd": "^2.0",
"roave/security-advisories": "dev-master",
"sebastian/phpcpd": "^3.0|^4.0",
"squizlabs/php_codesniffer": "^2.0",
"thecodingmachine/phpstan-strict-rules": "^0.10.1"
"thecodingmachine/phpstan-strict-rules": "^0.11"
},
"suggest": {
},
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ parameters:
paths:
- src
ignoreErrors:
- '/^Parameter #2 \$listener of method .+\\EventDispatcher::addListener\(\) expects callable, .+ given.$/'
- '/^Parameter #2 \$listener of method .+\\EventDispatcher::addListener\(\) expects callable\(\): mixed, .+ given.$/'
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ContainerAwareEventDispatcher extends SymfonyEventDispatcher
class ContainerAwareDispatcher extends SymfonyEventDispatcher implements EventDispatcher
{
/**
* @var ContainerInterface
Expand Down Expand Up @@ -48,7 +48,9 @@ public function __construct(ContainerInterface $container, array $listenersMap =
}

/**
* {@inheritdoc}
* Adds an event subscriber.
*
* @param EventSubscriberInterface $subscriber
*/
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
Expand All @@ -70,9 +72,9 @@ public function addSubscriber(EventSubscriberInterface $subscriber): void
/**
* Adds an event listener that listens on the specified events.
*
* @param string $eventName
* @param mixed $listener
* @param mixed $priority
* @param string $eventName
* @param callable|string $listener
* @param int $priority
*/
public function addListener($eventName, $listener, $priority = 0): void
{
Expand All @@ -83,7 +85,7 @@ public function addListener($eventName, $listener, $priority = 0): void
));
}

parent::addListener($eventName, $listener, (int) $priority);
parent::addListener($eventName, $listener, $priority);
}

/**
Expand Down
117 changes: 117 additions & 0 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* event-symfony-event-dispatcher (https://github.com/phpgears/event-symfony-event-dispatcher).
* Event bus with Symfony Event Dispatcher.
*
* @license MIT
* @link https://github.com/phpgears/event-symfony-event-dispatcher
* @author Julián Gutiérrez <juliangut@gmail.com>
*/

declare(strict_types=1);

namespace Gears\Event\Symfony;

use Gears\Event\EventHandler;
use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class Dispatcher extends SymfonyEventDispatcher implements EventDispatcher
{
/**
* ContainerAwareEventDispatcher constructor.
*
* @param array<string, mixed> $listenersMap
*/
public function __construct(array $listenersMap = [])
{
foreach ($listenersMap as $eventName => $listeners) {
if (!\is_array($listeners)) {
$listeners = [$listeners];
}

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

/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
foreach ($subscriber::getSubscribedEvents() as $eventName => $params) {
if (!\is_array($params)) {
$params = [$params];
}

foreach ($params as $listener) {
if (!\is_array($listener)) {
$this->addListener($eventName, $listener);
} else {
$this->addListener($eventName, $listener[0], $listener[1] ?? 0);
}
}
}
}

/**
* Adds an event listener that listens on the specified events.
*
* @param string $eventName
* @param mixed $listener
* @param mixed $priority
*/
public function addListener($eventName, $listener, $priority = 0): void
{
if (!$listener instanceof EventHandler) {
throw new \InvalidArgumentException(\sprintf(
'Event handler must be an instance of %s, %s given',
EventHandler::class,
\is_object($listener) ? \get_class($listener) : \gettype($listener)
));
}

parent::addListener($eventName, $listener, (int) $priority);
}

/**
* {@inheritdoc}
*/
public function dispatch($eventName, SymfonyEvent $event = null): SymfonyEvent
{
if ($event === null) {
throw new \InvalidArgumentException('Dispatched event cannot be empty');
}

if (!$event instanceof EventEnvelope) {
throw new \InvalidArgumentException(\sprintf(
'Dispatched event must implement %s, %s given',
EventEnvelope::class,
\get_class($event)
));
}

$this->dispatchEvent($this->getListeners($eventName), $event);

return $event;
}

/**
* Dispatch event to registered listeners.
*
* @param EventHandler[] $listeners
* @param EventEnvelope $event
*/
private function dispatchEvent(array $listeners, EventEnvelope $event): void
{
$dispatchEvent = $event->getWrappedEvent();

foreach ($listeners as $handler) {
$handler->handle($dispatchEvent);
}
}
}
6 changes: 3 additions & 3 deletions src/EventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ final class EventBus implements EventBusInterface
/**
* Wrapped event dispatcher.
*
* @var ContainerAwareEventDispatcher
* @var ContainerAwareDispatcher
*/
private $wrappedDispatcher;

/**
* EventBus constructor.
*
* @param ContainerAwareEventDispatcher $wrappedDispatcher
* @param ContainerAwareDispatcher $wrappedDispatcher
*/
public function __construct(ContainerAwareEventDispatcher $wrappedDispatcher)
public function __construct(EventDispatcher $wrappedDispatcher)
{
$this->wrappedDispatcher = $wrappedDispatcher;
}
Expand Down
101 changes: 2 additions & 99 deletions src/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,105 +13,8 @@

namespace Gears\Event\Symfony;

use Gears\Event\EventHandler;
use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EventDispatcher extends SymfonyEventDispatcher
interface EventDispatcher extends EventDispatcherInterface
{
/**
* ContainerAwareEventDispatcher constructor.
*
* @param array<string, mixed> $listenersMap
*/
public function __construct(array $listenersMap = [])
{
foreach ($listenersMap as $eventName => $listeners) {
if (!\is_array($listeners)) {
$listeners = [$listeners];
}

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

/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
foreach ($subscriber::getSubscribedEvents() as $eventName => $params) {
if (!\is_array($params)) {
$params = [$params];
}

foreach ($params as $listener) {
if (!\is_array($listener)) {
$this->addListener($eventName, $listener);
} else {
$this->addListener($eventName, $listener[0], $listener[1] ?? 0);
}
}
}
}

/**
* Adds an event listener that listens on the specified events.
*
* @param string $eventName
* @param mixed $listener
* @param mixed $priority
*/
public function addListener($eventName, $listener, $priority = 0): void
{
if (!$listener instanceof EventHandler) {
throw new \InvalidArgumentException(\sprintf(
'Event handler must be an instance of %s, %s given',
EventHandler::class,
\is_object($listener) ? \get_class($listener) : \gettype($listener)
));
}

parent::addListener($eventName, $listener, (int) $priority);
}

/**
* {@inheritdoc}
*/
public function dispatch($eventName, SymfonyEvent $event = null): SymfonyEvent
{
if ($event === null) {
throw new \InvalidArgumentException('Dispatched event cannot be empty');
}

if (!$event instanceof EventEnvelope) {
throw new \InvalidArgumentException(\sprintf(
'Dispatched event must implement %s, %s given',
EventEnvelope::class,
\get_class($event)
));
}

$this->dispatchEvent($this->getListeners($eventName), $event);

return $event;
}

/**
* Dispatch event to registered listeners.
*
* @param EventHandler[] $listeners
* @param EventEnvelope $event
*/
private function dispatchEvent(array $listeners, EventEnvelope $event): void
{
$dispatchEvent = $event->getWrappedEvent();

foreach ($listeners as $handler) {
$handler->handle($dispatchEvent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Gears\Event\Symfony\Tests;

use Gears\Event\EventHandler;
use Gears\Event\Symfony\ContainerAwareEventDispatcher;
use Gears\Event\Symfony\ContainerAwareDispatcher;
use Gears\Event\Symfony\EventEnvelope;
use Gears\Event\Symfony\Tests\Stub\EventStub;
use Gears\Event\Symfony\Tests\Stub\EventSubscriberInterfaceStub;
Expand All @@ -25,7 +25,7 @@
/**
* Symfony event dispatcher wrapper test.
*/
class ContainerAwareEventDispatcherTest extends TestCase
class ContainerAwareDispatcherTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
Expand All @@ -38,7 +38,7 @@ public function testInvalidListener(): void
->disableOriginalConstructor()
->getMock();

new ContainerAwareEventDispatcher($containerMock, ['eventName' => new \stdClass()]);
new ContainerAwareDispatcher($containerMock, ['eventName' => new \stdClass()]);
}

/**
Expand All @@ -52,7 +52,7 @@ public function testEmptyEvent(): void
->disableOriginalConstructor()
->getMock();

$eventDispatcher = new ContainerAwareEventDispatcher($containerMock);
$eventDispatcher = new ContainerAwareDispatcher($containerMock);

$eventDispatcher->dispatch('eventName');
}
Expand All @@ -68,7 +68,7 @@ public function testInvalidEvent(): void
->disableOriginalConstructor()
->getMock();

$eventDispatcher = new ContainerAwareEventDispatcher($containerMock);
$eventDispatcher = new ContainerAwareDispatcher($containerMock);

$eventDispatcher->dispatch('eventName', new Event());
}
Expand All @@ -87,7 +87,7 @@ public function testInvalidHandler(): void
->with('eventHandler')
->will($this->returnValue('thisIsNoHandler'));
/** @var ContainerInterface $containerMock */
$eventDispatcher = new ContainerAwareEventDispatcher($containerMock, ['eventName' => 'eventHandler']);
$eventDispatcher = new ContainerAwareDispatcher($containerMock, ['eventName' => 'eventHandler']);

$eventDispatcher->dispatch('eventName', new EventEnvelope(EventStub::instance()));
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public function testEventDispatch(): void
],
]);

$eventDispatcher = new ContainerAwareEventDispatcher($containerMock);
$eventDispatcher = new ContainerAwareDispatcher($containerMock);
$eventDispatcher->addSubscriber($subscriber);

$eventDispatcher->dispatch('eventName', new EventEnvelope($event));
Expand Down

0 comments on commit 47d4811

Please sign in to comment.