当前位置:网站首页>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
边栏推荐
- [deep learning] great! The new attention makes the model 2-4 times faster!
- LeetCode Algorithm 21. 合并两个有序链表
- 社区文章|MOSN 构建 Subset 优化思路分享
- What kind of experience is middle-aged unemployment
- 前 AMD 芯片架构师吐槽,取消 K12 处理器项目是因为 AMD 怂了!
- Quel est le risque de divulgation d'un certificat de signature de code?
- nodejs基础快速复习
- AttributeError: module ‘skimage.draw‘ has no attribute ‘circle‘
- QT compile the Internet of things management platform 36- communication protocol
- Niuke.com Huawei question bank (31~40)
猜你喜欢

被曝泄露超 1.7 亿条隐私数据,学习通回应:尚未发现明确证据

After using Matplotlib for so long, I didn't know that the data could move

普乐蛙5d飞行影院5d动感影院体验馆设备7d多人互动影院

Don't be silly enough to distinguish hash, chunkhash and contenthash

社区文章|MOSN 构建 Subset 优化思路分享

喜讯丨敏捷科技入选《2022年中国数字安全百强报告》

Tiktok announces data storage on Oracle server!

Cobalt Strike 从入门到入狱(三)

2022-06-09 工作记录--yarn/npm-Error-EPERM: operation not permitted, uv_cwd

From in MySQL_ Unixtime and UNIX_ Timestamp processing database timestamp conversion - Case
随机推荐
Qt编写物联网管理平台36-通信协议
Cobalt strike from starting to Imprisonment (3)
YOLOv3目标检测
前 AMD 芯片架构师吐槽,取消 K12 处理器项目是因为 AMD 怂了!
Niuke.com Huawei question bank (31~40)
scrapy.Request() 的 meta参数 数据的传递
数据库课程虚拟教研室负责人杜小勇:立足国产数据库重大需求,探索课程体系建设新模式
How to transfer the values in the database to JSP pages through servlets and display them in El expressions?
Quel est le risque de divulgation d'un certificat de signature de code?
原生JS动态添加和删除类
MySQL skip scan range small function to solve big problems?
Quickly master asp Net authentication framework identity - login and logout
VS2022连接sqlserver数据库教程
投资交易管理
学会用VisualStudio开发人员工具查看对象模型
jg_使用easyexcel读取excel_20220619
【深度学习】TensorFlow,危!抛弃者正是谷歌自己
From in MySQL_ Unixtime and UNIX_ Timestamp processing database timestamp conversion - Case
【无标题】#修复日志#
After using Matplotlib for so long, I didn't know that the data could move