CI 钩子函数的使用

点击查看官方钩子基本说明

手动调用钩子

  • 修改配置启用钩子
vim application/config/config.php
$config['enable_hooks'] = TRUE;
  • 在 application/config/hooks.php 中配置自定义钩子
//下单成功之后调用, 生成合同号之后
$hook['after_order'][] = array( 
  'class'    => 'SystemHook',
  'function' => 'index',
  'filename' => 'systemHook.php',
  'filepath' => 'hooks/', 
  'params'   => array()
);
  • 钩子类 application/hooks/systemHook.php文件内容:
CI->uri->segment(1);
        $a = $this->CI->uri->segment(2);
        $a = $this->CI->uri->segment(3);
        echo "";
        var_dump($a);
        exit;
    }
}
  • 在需要调用钩子的地方调用, 在controller中调用
  $this->CI->hooks->_call_hook_new('after_order',array('contractNum'=>151211111,'ljyunId'=>512));
  • 在library中调用
  $this->_hooks_call_hook_new('after_order',array('contractNum'=>151211111,'ljyunId'=>512));
  • 注意, 以上在调用的时候, _call_hook_new()方法是我自己定义的, 在CI中并不存在该方法, CI中使用的是 _call_hook() 方法, 并没有第二个参数的接收, 钩子调用函数的文件内容如下: /system/core/Hooks.php, 注意: 文件中含有 liangxifeng 标注的是我自己修改的.
_initialize();
        log_message('debug', "Hooks Class Initialized");
    }

    // --------------------------------------------------------------------

    /**
     * Initialize the Hooks Preferences
     *
     * @access  private
     * @return  void
     */
    function _initialize()
    {
        $CFG =& load_class('Config', 'core');

        // If hooks are not enabled in the config file
        // there is nothing else to do

        if ($CFG->item('enable_hooks') == FALSE)
        {
            return;
        }

        // Grab the "hooks" definition file.
        // If there are no hooks, we're done.

        if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
        {
            include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
        }
        elseif (is_file(APPPATH.'config/hooks.php'))
        {
            include(APPPATH.'config/hooks.php');
        }


        if ( ! isset($hook) OR ! is_array($hook))
        {
            return;
        }

        $this->hooks =& $hook;
        $this->enabled = TRUE;
    }

    /**
     * Call Hook liangxifeng 自定义钩子调用 自己新增$params参数,目的为了调用方可以传递参数
     * 2019-03-29
     *
     * Calls a particular hook
     *
     * @access  private
     * @param   string  the hook name
     * @return  mixed
     */
    function _call_hook_new($which = '',$params=array())
    {
        if ( ! $this->enabled OR ! isset($this->hooks[$which]))
        {
            return FALSE;
        }

        if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
        {
            foreach ($this->hooks[$which] as $val)
            {
                $this->_run_hook($val,$params);
            }
        }
        else
        {
            $this->_run_hook($this->hooks[$which],$params);
        }

        return TRUE;
    }

    /**
     * Call Hook
     *
     * Calls a particular hook
     *
     * @access  private
     * @param   string  the hook name
     * @return  mixed
     */
    function _call_hook($which = '')
    {
        if ( ! $this->enabled OR ! isset($this->hooks[$which]))
        {
            return FALSE;
        }

        if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
        {
            foreach ($this->hooks[$which] as $val)
            {
                $this->_run_hook($val);
            }
        }
        else
        {
            $this->_run_hook($this->hooks[$which]);
        }

        return TRUE;
    }

    // --------------------------------------------------------------------

    /**
     * Run Hook
     * liangxifeng 新增$customParams参数的接收, 目的是接收目标调用者传递过来的动态参数
     * 2019-03-29
     *
     * Runs a particular hook
     *
     * @access  private
     * @param   array   the hook details
     * @return  bool
     */
    function _run_hook($data,$customParams=array())
    {
        if ( ! is_array($data))
        {
            return FALSE;
        }

        // -----------------------------------
        // Safety - Prevents run-away loops
        // -----------------------------------

        // If the script being called happens to have the same
        // hook call within it a loop can happen

        if ($this->in_progress == TRUE)
        {
            return;
        }

        // -----------------------------------
        // Set file path
        // -----------------------------------

        if ( ! isset($data['filepath']) OR ! isset($data['filename']))
        {
            return FALSE;
        }

        $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];

        if ( ! file_exists($filepath))
        {
            return FALSE;
        }

        // -----------------------------------
        // Set class/function name
        // -----------------------------------

        $class      = FALSE;
        $function   = FALSE;
        $params     = '';

        if (isset($data['class']) AND $data['class'] != '')
        {
            $class = $data['class'];
        }

        if (isset($data['function']))
        {
            $function = $data['function'];
        }

        if (isset($data['params']))
        {
            $params = $data['params'];
        }
        //liangxifeng 如果$customParams自定义参数存在,则覆盖hooks配置中的参数
        //2019-03-29
        if(isset($customParams) && is_array($customParams) && !empty($customParams))
        {
            $params = $customParams;
        }
        //2019-03-29

        if ($class === FALSE AND $function === FALSE)
        {
            return FALSE;
        }

        // -----------------------------------
        // Set the in_progress flag
        // -----------------------------------

        $this->in_progress = TRUE;

        // -----------------------------------
        // Call the requested class and/or function
        // -----------------------------------

        if ($class !== FALSE)
        {
            if ( ! class_exists($class))
            {
                require($filepath);
            }

            $HOOK = new $class;
            $HOOK->$function($params);
        }
        else
        {
            if ( ! function_exists($function))
            {
                require($filepath);
            }

            $function($params);
        }

        $this->in_progress = FALSE;
        return TRUE;
    }

}

// END CI_Hooks class

/* End of file Hooks.php */
/* Location: ./system/core/Hooks.php */

你可能感兴趣的:(CI 钩子函数的使用)