-
Notifications
You must be signed in to change notification settings - Fork 138
Małe usprawnienie LMSPlugin::dispatch() #2505
Copy link
Copy link
Open
Description
Na własne potrzeby tworzę plugin i postanowiłem nadpisać dispatcher klasy LMSPlugin, aby nieco ułatwić logikę dispatchera. Do tego wykorzystałem array callable https://www.php.net/manual/en/language.types.callable.php oraz null coalescing operator https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
<?php
declare(strict_types=1);
class MyPlugin extends LMSPlugin
// ...
#[Override]
public function registerHandlers(): void
{
$this->handlers = [
'customeradd_after_submit' => [CustomeraddHandler::class, 'customeraddAfterSubmit'],
];
}
#[Override]
protected function dispatcher($hook_name, mixed $hook_data): mixed
{
// array_values dla kompatybilności wstecznej
$handler = array_values($this->handlers[$hook_name] ?? []);
if (empty($handler) && !is_callable($handler)) {
throw new Exception("Wrong handler configuration format for hook '$hook_name'!");
}
return $handler ? $handler($hook_data) : $hook_data;
}
}wtedy też metody getHandlerClass oraz getHandlerMethod nie są potrzebne.
Oczywiście do zmiany nie zmuszam, ale pomyślałem że się podzielę. Jeśli się przyda to fajnie, a jak nie no to zostanie jako ciekawostka :)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels