当前位置:网站首页>WordPress like hooks and filters in laravel

WordPress like hooks and filters in laravel

2022-06-22 10:33:00 rorg

WordPress One of the most powerful tools in is the hook system it uses . This is a good way to change values from anywhere . It's for any WordPress The website adds great flexibility . Let's take a look , How to be in Laravel To achieve this

 

Basic concepts

Basic ideas and WordPress The hook system is actually the same . There are some values somewhere in the code , We want to easily modify them from the outside , Without modifying any code . Besides , It would be nice to set priorities , Because we can better control the modification

Events and hooks

If we can use Laravel Event system to achieve this function , That would be great , But it's not ideal . therefore , We will implement a small hook Repository , It will store and sort the registered hooks according to priority

 

Hook repository

This repository will be a very simple place to store our hooks and use them when we need them

 

class HookRepository
{
    /**
     * The repository items.
     *
     * @var \Illuminate\Support\Collection
     */
    protected $items;

    /**
     * Create a new repository instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->items = collect();
    }

    /**
     * Dynamically call methods.
     *
     * @param  string  $method
     * @param  array  $arguments
     * @return mixed
     */
    public function __call(string $method, array $arguments)
    {
        return $this->items->{$method}(...$arguments);
    }

    /**
     * Register a new hook callback.
     *
     * @param  string|array  $hook
     * @param  callable  $callback
     * @param  int  $priority
     * @return void
     */
    public function register($hook, callable $callback, int $priority = 10): void
    {
        $this->items->push(compact('hook', 'callback', 'priority'));
    }

    /**
     * Apply the callbacks on the given hook and value.
     *
     * @param  string  $hook
     * @param  array  $arguments
     * @return mixed
     */
    public function apply(string $hook, ...$arguments)
    {
        return $this->items->filter(function ($filter) use ($hook) {
            return !! array_filter((array) $filter['hook'], function ($item) use ($hook) {
                return Str::is($item, $hook);
            });
        })->sortBy('priority')->reduce(function ($value, $filter) use ($arguments) {
            return call_user_func_array($filter['callback'], [$value] + $arguments);
        }, $arguments[0] ?? null);
    }
}

therefore , We have two methods here ,register and apply. All other calls will be forwarded to the collection instance that holds the hook  

We can register hooks in the service provider , for example  :

public function boot()
{
    Hook::register('jobs.tags', function ($tags, $job) {
        return array_merge($tags, $job->someCondition() ? ['foo'] : ['bar']);
    });
}

Please note that , Using this method , We can also register hooks using wildcards or array notation .

for example :[jobs.tags, posts.tags] or jobs.* 

Application hook

that , Where to apply hooks ? Basically anywhere you want . The benefits of this solution are , You don't really need complex value managers for different places . You can use hooks , Pass any parameters you want , And return the modified value that will be used later

public function tags()
{
    return Hook::apply('jobs.tags', ['a', 'b'], $this);
}

We call apply Method and first pass the hook and value , Then there are any other parameters that may be required in the callback

原网站

版权声明
本文为[rorg]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221026059552.html