如何做一个PHP钩子系统?

发布于 2024-12-18 16:22:59 字数 140 浏览 0 评论 0原文

如何在 PHP 应用程序中实现挂钩系统以在执行之前或之后更改代码。 PHP CMS(甚至简单的应用程序)的 hookloader 类的基本架构如何。那么如何将其扩展为完整的插件/模块加载器呢?

(另外,有没有关于CMS hook系统的书籍或教程?)

How do you impliment a hook system in a PHP application to change the code before or after it executes. How would the basic architecture of a hookloader class be for a PHP CMS (or even a simple application). How then could this be extended into a full plugins/modules loader?

(Also, are there any books or tutorials on a CMS hook system?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-12-25 16:22:59

您可以根据需要构建简单或复杂的事件系统它。

/**
 * 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);

You can build an events system as simple or complex as you want it.

/**
 * 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;
    }
}

Add an event

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');

Then call it like this

$text = event('filter_text', $_POST['text']);

Or remove all callbacks for that event like this

event('filter_text', null, false);
梦忆晨望 2024-12-25 16:22:59

这是另一个解决方案:

创建钩子

在您想要创建钩子的地方运行此命令:

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 */

}

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

的顶部

$x_events = array();
global $x_events;

将其添加到主 PHP 函数文件或等效基本函数

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;

}

Here's another solution:

Creating a hook

Run this wherever you want to create a hook:

x_do_action('header_scripts');


Attach a function to the hook

Then attach a function to the above by doing:

x_add_action('header_scripts','my_function_attach_header_scripts');

function my_function_attach_header_scripts($values) {

   /* add my scripts here */

}

Global variable to store all hooks/events

Add this to the top of your main PHP functions file, or equivalent

$x_events = array();
global $x_events;

Base functions

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;

}
深居我梦 2024-12-25 16:22:59

此解决方案存在一些问题,您无法为一个可调用挂钩设置多个函数。
你可以使用这个代码。



 $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();



this solutions has some problems that you can't set several function for one callable hook.
you can fallow this code.



 $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();



~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文