Skip to content

Commit 0970ccb

Browse files
committed
issue/#3: remove requirement of event name
* Update Event dispatcher, provider, and collection to do not need of event name to locate and dispatch the event. * Update tests accordingly * Delete EventInterface and update abstract Event
1 parent 92497c3 commit 0970ccb

17 files changed

+87
-77
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Change Log
22

3+
## [Unreleased](https://github.com/kpicaza/antidot-event-dispatcher/tree/HEAD)
4+
5+
[Full Changelog](https://github.com/kpicaza/antidot-event-dispatcher/compare/1.1.0...HEAD)
6+
7+
**Implemented enhancements:**
8+
9+
- Remove Requirement of Event name [\#3](https://github.com/kpicaza/antidot-event-dispatcher/issues/3)
10+
11+
**Closed issues:**
12+
13+
- Create docs site for Event dispatcher [\#4](https://github.com/kpicaza/antidot-event-dispatcher/issues/4)
14+
15+
## [1.1.0](https://github.com/kpicaza/antidot-event-dispatcher/tree/1.1.0) (2019-04-10)
16+
[Full Changelog](https://github.com/kpicaza/antidot-event-dispatcher/compare/1.0.0...1.1.0)
17+
18+
**Implemented enhancements:**
19+
20+
- issue/\#3: remove requirement of event name [\#6](https://github.com/kpicaza/antidot-event-dispatcher/pull/6) ([kpicaza](https://github.com/kpicaza))
21+
22+
**Merged pull requests:**
23+
24+
- issue/\#4: Create docs for github pages [\#5](https://github.com/kpicaza/antidot-event-dispatcher/pull/5) ([kpicaza](https://github.com/kpicaza))
25+
326
## [1.0.0](https://github.com/kpicaza/antidot-event-dispatcher/tree/1.0.0) (2019-04-08)
427
**Fixed bugs:**
528

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"phpro/grumphp": "^0.15.0",
2121
"phpstan/phpstan": "^0.11.5",
2222
"phpunit/phpunit": "^8.0",
23-
"squizlabs/php_codesniffer": "^3.4"
23+
"squizlabs/php_codesniffer": "^3.4",
24+
"symfony/var-dumper": "^4.2"
2425
},
2526
"autoload": {
2627
"psr-4": {

src/Container/EventDispatcherFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public function __invoke(ContainerInterface $container): EventDispatcherInterfac
1717
$config = $container->get('config')['app-events'];
1818
$listenerProvider = new ListenerProvider();
1919

20-
foreach ($config['event-listeners'] as $eventName => $listeners) {
20+
foreach ($config['event-listeners'] as $eventClass => $listeners) {
2121
foreach ($listeners as $listenerId) {
2222
$listenerProvider->addListener(
23-
$eventName,
23+
$eventClass,
2424
static function () use ($container, $listenerId): callable {
2525
return $container->get($listenerId);
2626
}

src/Event.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44

55
namespace Antidot\Event;
66

7-
abstract class Event implements EventInterface
7+
use Psr\EventDispatcher\StoppableEventInterface;
8+
9+
abstract class Event implements StoppableEventInterface
810
{
9-
/** @var string */
10-
protected $name;
1111
/** @var bool */
1212
protected $stopped;
1313

14-
public function name(): string
15-
{
16-
return $this->name;
17-
}
18-
1914
public function isPropagationStopped(): bool
2015
{
2116
return $this->stopped;

src/EventDispatcher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psr\EventDispatcher\EventDispatcherInterface;
88
use Psr\EventDispatcher\ListenerProviderInterface;
9+
use Psr\EventDispatcher\StoppableEventInterface;
910

1011
class EventDispatcher implements EventDispatcherInterface
1112
{
@@ -17,7 +18,7 @@ public function __construct(ListenerProviderInterface $listenerProvider)
1718
}
1819

1920
/**
20-
* @param EventInterface $event
21+
* @param StoppableEventInterface $event
2122
* @return object
2223
*/
2324
public function dispatch(object $event): object

src/EventInterface.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/ListenerCollection.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ public function __construct()
1818
$this->listeners = [];
1919
}
2020

21-
public function addListener(string $eventName, callable $listener): void
21+
public function addListener(string $eventClass, callable $listener): void
2222
{
23-
if ($this->has($eventName)) {
24-
$this->listeners[$eventName][] = $listener;
23+
if ($this->has($eventClass)) {
24+
$this->listeners[$eventClass][] = $listener;
2525
return;
2626
}
2727

28-
$this->listeners[$eventName] = [$listener];
28+
$this->listeners[$eventClass] = [$listener];
2929
}
3030

31-
public function get(string $eventName): iterable
31+
public function get(string $eventClass): iterable
3232
{
33-
if ($this->has($eventName)) {
34-
yield from $this->listeners[$eventName];
33+
if ($this->has($eventClass)) {
34+
yield from $this->listeners[$eventClass];
3535
}
3636
}
3737

38-
public function has(string $eventName): bool
38+
public function has(string $eventClass): bool
3939
{
40-
return array_key_exists($eventName, $this->listeners);
40+
return array_key_exists($eventClass, $this->listeners);
4141
}
4242

4343
public function getIterator()

src/ListenerCollectorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface ListenerCollectorInterface
88
{
9-
public function addListener(string $eventName, callable $listener): void;
9+
public function addListener(string $eventClass, callable $listener): void;
1010
}

src/ListenerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Antidot\Event;
66

7+
use Psr\EventDispatcher\StoppableEventInterface;
8+
79
interface ListenerInterface
810
{
9-
public function __invoke(EventInterface $event): EventInterface;
11+
public function __invoke(StoppableEventInterface $event): StoppableEventInterface;
1012
}

src/ListenerLocatorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
interface ListenerLocatorInterface
88
{
9-
public function has(string $eventName): bool;
10-
public function get(string $eventName): iterable;
9+
public function has(string $eventClass): bool;
10+
public function get(string $eventClass): iterable;
1111
}

0 commit comments

Comments
 (0)