Added EventDispatcher
parent
d1408fc3fe
commit
814cafd05d
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Events;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class EventDispatcher
|
||||
{
|
||||
/** @var callable[] */
|
||||
protected $listeners;
|
||||
|
||||
/**
|
||||
* @param array|string $events
|
||||
* @param callable|string $listener
|
||||
*/
|
||||
public function listen($events, $listener): void
|
||||
{
|
||||
foreach ((array)$events as $event) {
|
||||
$this->listeners[$event][] = $listener;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $event
|
||||
*/
|
||||
public function forget($event): void
|
||||
{
|
||||
unset($this->listeners[$event]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|object $event
|
||||
* @param array|mixed $payload
|
||||
* @param bool $halt
|
||||
*
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function fire($event, $payload = [], $halt = false)
|
||||
{
|
||||
return $this->dispatch($event, $payload, $halt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|object $event
|
||||
* @param array|mixed $payload
|
||||
* @param bool $halt Stop on first non-null return
|
||||
*
|
||||
* @return array|null|mixed
|
||||
*/
|
||||
public function dispatch($event, $payload = [], $halt = false)
|
||||
{
|
||||
if (is_object($event)) {
|
||||
$payload = $event;
|
||||
$event = get_class($event);
|
||||
}
|
||||
|
||||
$listeners = [];
|
||||
if (isset($this->listeners[$event])) {
|
||||
$listeners = $this->listeners[$event];
|
||||
}
|
||||
|
||||
$responses = [];
|
||||
foreach ($listeners as $listener) {
|
||||
if (!is_callable($listener) && is_string($listener) && !Str::contains($listener, '@')) {
|
||||
$listener = $listener . '@handle';
|
||||
}
|
||||
|
||||
$response = app()->call($listener, ['event' => $event] + Arr::wrap($payload));
|
||||
|
||||
// Return the events response
|
||||
if ($halt && !is_null($response)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
// Stop further event propagation
|
||||
if ($response === false) {
|
||||
break;
|
||||
}
|
||||
|
||||
$responses[] = $response;
|
||||
}
|
||||
|
||||
return $halt ? null : $responses;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Events;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
|
||||
class EventsServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$dispatcher = $this->app->make(EventDispatcher::class);
|
||||
|
||||
$this->app->instance(EventDispatcher::class, $dispatcher);
|
||||
$this->app->instance('events.dispatcher', $dispatcher);
|
||||
|
||||
$this->registerEvents($dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EventDispatcher $dispatcher
|
||||
*/
|
||||
protected function registerEvents(EventDispatcher $dispatcher)
|
||||
{
|
||||
/** @var Config $config */
|
||||
$config = $this->app->get('config');
|
||||
|
||||
foreach ($config->get('event-handlers', []) as $event => $handlers) {
|
||||
foreach ((array)$handlers as $handler) {
|
||||
$dispatcher->listen($event, $handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Events;
|
||||
|
||||
use Engelsystem\Events\EventDispatcher;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
|
||||
class EventDispatcherTest extends TestCase
|
||||
{
|
||||
/** @var array */
|
||||
protected $firedEvents = [];
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::listen
|
||||
* @covers \Engelsystem\Events\EventDispatcher::fire
|
||||
*/
|
||||
public function testListen(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$event->listen('foo', [$this, 'eventHandler']);
|
||||
$event->listen(['foo', 'bar'], [$this, 'eventHandler']);
|
||||
|
||||
$event->fire('foo');
|
||||
$event->fire('bar', 'Test!');
|
||||
|
||||
$this->assertEquals(
|
||||
['foo' => ['count' => 2, ['foo'], ['foo']], 'bar' => ['count' => 1, ['bar', 'Test!']]],
|
||||
$this->firedEvents
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::forget
|
||||
*/
|
||||
public function testForget(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$event->forget('not-existing-event');
|
||||
|
||||
$event->listen('test', [$this, 'eventHandler']);
|
||||
$event->forget('test');
|
||||
|
||||
$event->fire('test');
|
||||
|
||||
$this->assertEquals([], $this->firedEvents);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::dispatch
|
||||
*/
|
||||
public function testDispatchNotExistingEvent(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$response = $event->fire('not-existing-event');
|
||||
|
||||
$this->assertEquals([], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::dispatch
|
||||
*/
|
||||
public function testDispatchObject(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$event->listen(static::class, [$this, 'eventHandler']);
|
||||
$event->fire($this);
|
||||
|
||||
$this->assertEquals([static::class => ['count' => 1, [static::class, $this]]], $this->firedEvents);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::dispatch
|
||||
*/
|
||||
public function testDispatchHalt(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$event->listen('test', [$this, 'returnNull']);
|
||||
$event->listen('test', [$this, 'returnData']);
|
||||
$event->listen('test', [$this, 'eventHandler']);
|
||||
$response = $event->dispatch('test', [], true);
|
||||
|
||||
$this->assertEquals(['example' => 'data'], $response);
|
||||
$this->assertEquals([], $this->firedEvents);
|
||||
|
||||
$event = new EventDispatcher();
|
||||
$response = $event->dispatch('test', [], true);
|
||||
$this->assertNull($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::dispatch
|
||||
*/
|
||||
public function testDispatchStopPropagation(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$event->listen('test', [$this, 'returnNull']);
|
||||
$event->listen('test', [$this, 'returnFalse']);
|
||||
$event->listen('test', [$this, 'eventHandler']);
|
||||
$response = $event->dispatch('test');
|
||||
|
||||
$this->assertEquals([null], $response);
|
||||
$this->assertEquals([], $this->firedEvents);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventDispatcher::dispatch
|
||||
*/
|
||||
public function testDispatchFallbackHandleMethod(): void
|
||||
{
|
||||
$event = new EventDispatcher();
|
||||
$event->listen('test', EventDispatcherTest::class);
|
||||
$response = $event->dispatch('test', [], true);
|
||||
|
||||
$this->assertEquals(['default' => 'handler'], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $event
|
||||
*/
|
||||
public function eventHandler(string $event): void
|
||||
{
|
||||
if (!isset($this->firedEvents[$event])) {
|
||||
$this->firedEvents[$event] = ['count' => 0];
|
||||
}
|
||||
|
||||
$this->firedEvents[$event]['count']++;
|
||||
$this->firedEvents[$event][] = func_get_args();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function returnNull()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function returnFalse(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function returnData(): array
|
||||
{
|
||||
return ['example' => 'data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
return ['default' => 'handler'];
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Events;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Events\EventDispatcher;
|
||||
use Engelsystem\Events\EventsServiceProvider;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
|
||||
class EventsServiceProviderTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Events\EventsServiceProvider::register
|
||||
* @covers \Engelsystem\Events\EventsServiceProvider::registerEvents
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
$dispatcher = $this->createMock(EventDispatcher::class);
|
||||
$this->app->instance(EventDispatcher::class, $dispatcher);
|
||||
$dispatcher->expects($this->exactly(3))
|
||||
->method('listen')
|
||||
->withConsecutive(
|
||||
['test.event', 'someFunction'],
|
||||
['another.event', 'Foo\Bar@baz'],
|
||||
['another.event', [$this, 'someMethod']]
|
||||
);
|
||||
|
||||
$config = new Config([
|
||||
'event-handlers' => [
|
||||
'test.event' => 'someFunction',
|
||||
'another.event' => ['Foo\Bar@baz', [$this, 'someMethod']]
|
||||
]
|
||||
]);
|
||||
$this->app->instance('config', $config);
|
||||
|
||||
/** @var EventsServiceProvider $provider */
|
||||
$provider = $this->app->make(EventsServiceProvider::class);
|
||||
|
||||
$provider->register();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue