【问题标题】:How to do a PHP hook system?如何做一个PHP钩子系统?
【发布时间】:2012-01-10 07:12:59
【问题描述】:

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

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

【问题讨论】:

  • 这并不能回答您的问题 - 但另一种选择......在脚本的开头制作模板对象,通过将它们与控制器一起传递或启动它们在整个执行过程中加载不同的对象使用模板工厂(我在控制器中使用它)。然后,将模板编译成您希望它们最终的样子。你也可以指向自定义样式表。
  • 您能就这个问题提供您自己的见解吗?您问这个问题已经有一段时间了,我很想知道您从编写自定义 CMS 的经验中学到的最重要的方面是什么

标签: php plugins module content-management-system hook


【解决方案1】:

您可以构建一个事件系统,如simple 或您想要的复杂。

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

【讨论】:

  • 它如此简单.. 非常有效
【解决方案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;

}

【讨论】:

    【解决方案3】:

    这个解决方案有一些问题,你不能为一个可调用的钩子设置多个函数。 你可以放松这段代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多