Skip to content

Małe usprawnienie LMSPlugin::dispatch() #2505

@EnterVPL

Description

@EnterVPL

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions