Better Dispatcher documentation

pull/540/head
fadrian06 12 months ago
parent 8bec3658b0
commit b2d7164be0

@ -49,26 +49,26 @@ class Dispatcher
$output = ''; $output = '';
// Run pre-filters // Run pre-filters
$thereAreBeforeFilters = !empty($this->filters[$name]['before']); $thereAreBeforeFilters = !empty($this->filters[$name][self::FILTER_BEFORE]);
if ($thereAreBeforeFilters) { if ($thereAreBeforeFilters) {
$this->filter($this->filters[$name]['before'], $params, $output); $this->filter($this->filters[$name][self::FILTER_BEFORE], $params, $output);
} }
// Run requested method // Run requested method
$callback = $this->get($name); $requestedMethod = $this->get($name);
if ($callback === null) { if ($requestedMethod === null) {
throw new Exception("Event '$name' isn't found."); throw new Exception("Event '$name' isn't found.");
} }
$output = $callback(...$params); $output = $requestedMethod(...$params);
// Run post-filters // Run post-filters
$thereAreAfterFilters = !empty($this->filters[$name]['after']); $thereAreAfterFilters = !empty($this->filters[$name][self::FILTER_AFTER]);
if ($thereAreAfterFilters) { if ($thereAreAfterFilters) {
$this->filter($this->filters[$name]['after'], $params, $output); $this->filter($this->filters[$name][self::FILTER_AFTER], $params, $output);
} }
return $output; return $output;
@ -118,7 +118,7 @@ class Dispatcher
} }
/** /**
* Clears an event. If no name is given, all events are removed. * Clears an event. If no name is given, all events will be removed.
* *
* @param ?string $name Event name * @param ?string $name Event name
*/ */
@ -165,7 +165,7 @@ class Dispatcher
* @param array<int, mixed> $params Method parameters * @param array<int, mixed> $params Method parameters
* @param mixed $output Method output * @param mixed $output Method output
* *
* @throws Exception If an event throws an `Exception`. * @throws Exception If an event throws an `Exception` or if `$filters` contains an invalid filter.
*/ */
public static function filter(array $filters, array &$params, &$output): void public static function filter(array $filters, array &$params, &$output): void
{ {
@ -190,7 +190,7 @@ class Dispatcher
* @param array<int, mixed> $params Function parameters * @param array<int, mixed> $params Function parameters
* *
* @return mixed Function results * @return mixed Function results
* @throws Exception * @throws Exception If `$callback` also throws an `Exception`.
*/ */
public static function execute($callback, array &$params = []) public static function execute($callback, array &$params = [])
{ {

@ -208,9 +208,10 @@ class DispatcherTest extends TestCase
$params = []; $params = [];
$output = ''; $output = '';
$invalidCallable = 'invalidGlobalFunction';
$validCallable = function (): void { $validCallable = function (): void {
}; };
$invalidCallable = 'invalidGlobalFunction';
Dispatcher::filter([$validCallable, $invalidCallable], $params, $output); Dispatcher::filter([$validCallable, $invalidCallable], $params, $output);
} }

Loading…
Cancel
Save