如何创建一个PHP钩子系统?

16
如何在PHP应用程序中实现钩子系统以在代码执行前后更改代码。 针对PHP CMS(甚至是简单应用程序)的hookloader类的基本架构将是什么样子。然后,如何将其扩展为完整的插件/模块加载器?
(此外,有关CMS钩子系统的书籍或教程吗?)

1
这并不是回答你的问题,而是提供一种替代方案...在脚本开始时创建模板对象,在执行过程中通过控制器传递它们或使用模板工厂初始化它们(我在控制器中使用它)。然后,将模板编译成最终想要的样子。你也可以指向自定义样式表。 - phpmeh
你能提供自己对这个问题的见解吗?你问这个问题已经有一段时间了,我很想知道你从编写自定义CMS的经验中学到了哪些最重要的方面。 - Adrian Moisa
3个回答

31

您可以构建一个事件系统,它可以是简单或复杂的。

/**
 * Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called.
 *
 * @param string $event name
 * @param mixed $value the optional value to pass to each callback
 * @param mixed $callback the method or function to call - FALSE to remove all callbacks for event
 */
function event($event, $value = NULL, $callback = NULL)
{
    static $events;

    // Adding or removing a callback?
    if($callback !== NULL)
    {
        if($callback)
        {
            $events[$event][] = $callback;
        }
        else
        {
            unset($events[$event]);
        }
    }
    elseif(isset($events[$event])) // Fire a callback
    {
        foreach($events[$event] as $function)
        {
            $value = call_user_func($function, $value);
        }
        return $value;
    }
}

添加事件
event('filter_text', NULL, function($text) { return htmlspecialchars($text); });
// add more as needed
event('filter_text', NULL, function($text) { return nl2br($text); });
// OR like this
//event('filter_text', NULL, 'nl2br');

然后像这样调用它
$text = event('filter_text', $_POST['text']);

或者像这样删除该事件的所有回调函数

event('filter_text', null, false);

这非常简单,而且非常有效。 - user007

2

这里有另一个解决方案:

创建钩子

在任何你想要创建钩子的地方运行以下代码:

x_do_action('header_scripts');


将函数附加到钩子上

然后通过以下方式将函数附加到上述钩子:

x_add_action('header_scripts','my_function_attach_header_scripts');

function my_function_attach_header_scripts($values) {

   /* add my scripts here */

}

用于存储所有钩子/事件的全局变量

将以下代码添加到您的主PHP函数文件或等效文件的顶部:

$x_events = array();
global $x_events;

基本功能

function x_do_action($hook, $value = NULL) {
    global $x_events;

    if (isset($x_events[$hook])) {

        foreach($x_events[$hook] as $function) {

            if (function_exists($function)) { call_user_func($function, $value); }

        }
    }

}

function x_add_action($hook, $func, $val = NULL) {
    global $x_events;
    $x_events[$hook][] = $func;

}

0

这个解决方案存在一些问题,你不能为一个可调用的钩子设置多个函数。 你可以按照这段代码操作。



 $action = [];

 function apply($hook, $args){
    global $action;
    $action[$hook]['args'] = $args;
    return doa($hook, $args);
 }

 function add($hook, $func){
    global $action;
    $action[$hook]['funcs'][] = $func;
 }

 function doa($hook,$args){
    global $action;
    if(isset($action[$hook]['funcs'])){
        foreach($action[$hook]['funcs'] as $k => $func){
            call_user_func_array($func, $args);
       }
    }
    
 }

 add('this_is', 'addOne');
function addOne($user){
    echo "this is test add one $user <br>";
}
add('this_is', function(){
    echo 'this is test add two <br>';
});


add('this_is_2', 'addTwo');
function addTwo($user, $name){
    echo $user . '   ' . $name . '<br>';
}


function test(){
    echo 'hello one <br>';
    apply('this_is', ['user'=> 123]);
}


function test2(){
    echo 'hello two <br>';
    apply('this_is_2', ['user'=> 123, 'name' => 'mohammad']);
}
test();
test2();




网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接