Added method chaining Dispatcher::hook and FILTER_TYPES consts

pull/538/head
fadrian06 12 months ago
parent e35fc32a97
commit 631d4a26c2

@ -19,6 +19,9 @@ use InvalidArgumentException;
*/ */
class Dispatcher class Dispatcher
{ {
public const FILTER_BEFORE = 'before';
public const FILTER_AFTER = 'after';
/** /**
* Mapped events. * Mapped events.
* *
@ -125,10 +128,14 @@ class Dispatcher
* @param string $name Event name * @param string $name Event name
* @param 'before'|'after' $type Filter type * @param 'before'|'after' $type Filter type
* @param Closure(array<int, mixed> &$params, string &$output): (void|false) $callback * @param Closure(array<int, mixed> &$params, string &$output): (void|false) $callback
*
* @return $this
*/ */
public function hook(string $name, string $type, callable $callback): void public function hook(string $name, string $type, callable $callback): self
{ {
$this->filters[$name][$type][] = $callback; $this->filters[$name][$type][] = $callback;
return $this;
} }
/** /**

@ -19,40 +19,33 @@ class DispatcherTest extends TestCase
$this->dispatcher = new Dispatcher(); $this->dispatcher = new Dispatcher();
} }
// Map a closure public function testClosureMapping(): void
public function testClosureMapping()
{ {
$closure = Closure::fromCallable(function (): string { $this->dispatcher->set('map1', Closure::fromCallable(function (): string {
return 'hello'; return 'hello';
}); }));
$this->dispatcher->set('map1', $closure);
$result = $this->dispatcher->run('map1'); $this->assertSame('hello', $this->dispatcher->run('map1'));
self::assertEquals('hello', $result);
} }
// Map a function public function testFunctionMapping(): void
public function testFunctionMapping()
{ {
$this->dispatcher->set('map2', function (): string { $this->dispatcher->set('map2', function (): string {
return 'hello'; return 'hello';
}); });
$result = $this->dispatcher->run('map2'); $this->assertSame('hello', $this->dispatcher->run('map2'));
self::assertEquals('hello', $result);
} }
public function testHasEvent() public function testHasEvent(): void
{ {
$this->dispatcher->set('map-event', function (): void { $this->dispatcher->set('map-event', function (): void {
}); });
$result = $this->dispatcher->has('map-event'); $this->assertTrue($this->dispatcher->has('map-event'));
$this->assertTrue($result);
} }
public function testClearAllRegisteredEvents() public function testClearAllRegisteredEvents(): void
{ {
$customFunction = $anotherFunction = function (): void { $customFunction = $anotherFunction = function (): void {
}; };
@ -67,76 +60,61 @@ class DispatcherTest extends TestCase
$this->assertFalse($this->dispatcher->has('map-event-2')); $this->assertFalse($this->dispatcher->has('map-event-2'));
} }
public function testClearDeclaredRegisteredEvent() public function testClearDeclaredRegisteredEvent(): void
{ {
$this->dispatcher->set('map-event', function () { $customFunction = $anotherFunction = function (): void {
return 'hello'; };
});
$this->dispatcher->set('map-event-2', function () { $this->dispatcher
return 'there'; ->set('map-event', $customFunction)
}); ->set('map-event-2', $anotherFunction);
$this->dispatcher->clear('map-event'); $this->dispatcher->clear('map-event');
$result = $this->dispatcher->has('map-event'); $this->assertFalse($this->dispatcher->has('map-event'));
$this->assertFalse($result); $this->assertTrue($this->dispatcher->has('map-event-2'));
$result = $this->dispatcher->has('map-event-2');
$this->assertTrue($result);
} }
// Map a static function public function testStaticFunctionMapping(): void
public function testStaticFunctionMapping()
{ {
$this->dispatcher->set('map2', 'tests\classes\Hello::sayBye'); $this->dispatcher->set('map2', Hello::class . '::sayBye');
$result = $this->dispatcher->run('map2');
self::assertEquals('goodbye', $result); $this->assertSame('goodbye', $this->dispatcher->run('map2'));
} }
// Map a class method public function testClassMethodMapping(): void
public function testClassMethodMapping()
{ {
$h = new Hello(); $this->dispatcher->set('map3', [new Hello(), 'sayHi']);
$this->dispatcher->set('map3', [$h, 'sayHi']);
$result = $this->dispatcher->run('map3');
self::assertEquals('hello', $result); $this->assertSame('hello', $this->dispatcher->run('map3'));
} }
// Map a static class method public function testStaticClassMethodMapping(): void
public function testStaticClassMethodMapping()
{ {
$this->dispatcher->set('map4', ['\tests\classes\Hello', 'sayBye']); $this->dispatcher->set('map4', [Hello::class, 'sayBye']);
$result = $this->dispatcher->run('map4'); $this->assertSame('goodbye', $this->dispatcher->run('map4'));
self::assertEquals('goodbye', $result);
} }
// Run before and after filters public function testBeforeAndAfter(): void
public function testBeforeAndAfter()
{ {
$this->dispatcher->set('hello', function ($name) { $this->dispatcher->set('hello', function (string $name): string {
return "Hello, $name!"; return "Hello, $name!";
}); });
$this->dispatcher->hook('hello', 'before', function (&$params) { $this->dispatcher
->hook('hello', $this->dispatcher::FILTER_BEFORE, function (array &$params): void {
// Manipulate the parameter // Manipulate the parameter
$params[0] = 'Fred'; $params[0] = 'Fred';
}); })
->hook('hello', $this->dispatcher::FILTER_AFTER, function (array &$params, string &$output): void {
$this->dispatcher->hook('hello', 'after', function (&$params, &$output) {
// Manipulate the output // Manipulate the output
$output .= ' Have a nice day!'; $output .= ' Have a nice day!';
}); });
$result = $this->dispatcher->run('hello', ['Bob']); $result = $this->dispatcher->run('hello', ['Bob']);
self::assertEquals('Hello, Fred! Have a nice day!', $result); $this->assertSame('Hello, Fred! Have a nice day!', $result);
} }
// Test an invalid callback // Test an invalid callback

Loading…
Cancel
Save