当前位置:网站首页>Laravel 中类似 WordPress 的钩子和过滤器
Laravel 中类似 WordPress 的钩子和过滤器
2022-06-22 10:26:00 【rorg】
WordPress 中最强大的工具之一是它使用的钩子系统。这是从任何地方修改值的好方法。它为任何 WordPress 网站增加了巨大的灵活性。让我们看一下,如何在 Laravel 中实现这一点
基本概念
基本思想与 WordPress 钩子系统其实是一样的。代码中的某处有一些值,我们希望从外部轻松修改它们,而无需修改任何代码。此外,设置优先级会很好,因为我们可以更好地控制修改
事件与钩子
如果我们可以使用 Laravel 的事件系统来实现这个功能,那就太好了,但这并不理想。因此,我们将实现一个小型钩子存储库,它将根据优先级存储和排序已注册的钩子
挂钩存储库
这个存储库将是一个非常简单的地方来存储我们的钩子并在我们需要时使用它们
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);
}
}所以,我们这里有两个方法,register和apply。所有其他调用将被转发到持有钩子的集合实例
我们可以在服务提供者中注册钩子,例如 :
public function boot()
{
Hook::register('jobs.tags', function ($tags, $job) {
return array_merge($tags, $job->someCondition() ? ['foo'] : ['bar']);
});
}请注意,使用这种方法,我们也可以使用通配符或数组表示法注册挂钩。
例如:[jobs.tags, posts.tags]或jobs.*
应用挂钩
那么,在哪里应用钩子呢?基本上你想要的任何地方。该解决方案的好处是,您实际上并不需要针对不同地方的复杂价值管理器。您可以使用钩子,传递您想要的任何参数,并返回稍后将使用的修改后的值
public function tags()
{
return Hook::apply('jobs.tags', ['a', 'b'], $this);
}我们调用apply方法并首先传递钩子和值,然后是回调中可能需要的任何其他参数
边栏推荐
- 快速掌握 ASP.NET 身份认证框架 Identity - 登录与登出
- TCP 拥塞识别
- Basic principles of the Internet
- 抖音实战~个人中心
- PowerDesigner tip 2 trigger template
- Force buckle 1108 IP address invalidation
- Good news - agile technology was selected into the 2022 China top 100 Digital Security Report
- 2022年深入推进IPv6部署和应用,该如何全面实现安全升级改造?
- It was exposed that more than 170million pieces of private data had been leaked, and Xuetong responded that no clear evidence had been found
- 【backtrader源码解析51】observers中七个文件源代码简单解读(枯燥,仅供参考)
猜你喜欢

TikTok 宣布将数据存储于 Oracle 服务器!

从MVC原理开始手敲一个MVC框架,带你体会当大神的乐趣

Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality

等重构完这系统,我就提离职!

Discussion on the open source GIS solution of our company

谁说postgresql 没有靠谱的高可用(2)

如何进行高效简洁的电子文档管理

IPO Configuration Guide

Gartner表示:云数据库发展强劲,但本地数据库仍然充满活力

批量创建/删除文件、文件夹、修改文件名 后缀名
随机推荐
原生JS动态添加和删除类
[backtrader source code analysis 51] simple interpretation of the source code of seven files in observers (boring, for reference only)
YOLOv3目标检测
一条TCP连接时占用内存空间多少?
Pytorch实现波阻抗反演
Solend废止「接管巨鲸」提案 清算「炸弹」未除
[popular science] to understand supervised learning, unsupervised learning and reinforcement learning
Should the theme of the IDE be bright or dark? Here comes the ultimate answer!
麒麟软件携手格尔软件聚焦网络数据安全发展
jg_使用easyexcel读取excel_20220619
DAO 的未来:构建 web3 的组织原语
传iPhone 14将全系涨价;TikTok美国用户数据转移到甲骨文,字节无法访问;SeaTunnel 2.1.2发布|极客头条...
Pareto's law at work: focus on results, not outputs
关于 GIN 的路由树
QT compile the Internet of things management platform 36- communication protocol
牛客网——华为题库(31~40)
Cobalt Strike 从入门到入狱(三)
批量创建/删除文件、文件夹、修改文件名 后缀名
2022年深入推进IPv6部署和应用,该如何全面实现安全升级改造?
投资交易管理