[php] _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 // 如果設定檔中設定了是不允許hooks,則直接返回退出本函數。 if ($CFG->item('enable_hooks') == FALSE) { return; } // Grab the "hooks" definition file. // 抓取鉤子的定義檔案 // If there are no hooks, we're done. // 如果沒有定義hooks.php沒有定義$hook數組我們直接返回 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; } // 將hooks.php 中的$hook數組引用到$this->hooks // 開啟$this->enabled $this->hooks =& $hook; $this->enabled = TRUE; } // -------------------------------------------------------------------- /** * Call Hook * 外部其實就是調用這個_call_hook函數進行調用鉤子程式。 * 而此方法中再調用_run_hook去執行相應的鉤子。 * Calls a particular hook * * @access private * @param string the hook name * @return mixed */ function _call_hook($which = '') { // 判斷$this->enabled 是否開啟 和 要調用的鉤子是否在$htis->hooks中存在。 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 * 運行鉤子 * Runs a particular 特別的 hook * * @access private * @param array the hook details * @return bool */ function _run_hook($data) { /* * $data 就是我們在APPPATH/config/hook.php 定義的hook數組 * $hook['pre_controller'] = array( * 'class' => 'MyClass', * 'function' => 'Myfunction', * 'filename' => 'Myclass.php', * 'filepath' => 'hooks', * 'params' => array('beer', 'wine', 'snacks') * ); * * 由於每一個鉤子肯定是由數組組成的 * 所以這裡就判斷$data是不是數組如果不是則返回 * */ 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 // 如果調用某一個hook,執行某些指令碼,而有可能這些指令碼裡面再會觸發其它hook // 如果這個其它hook裡面又包含了當前 // 的hook,那麼就會進入死迴圈,這個in_progress的存在就是阻止這種情況。 if ($this->in_progress == TRUE) { return; } // ----------------------------------- // 取出data裡面的資料,載入 APPPATH.$data['filepath'].$data['filename']; // 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 = ''; // 取出$hooks 中的class function 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']; } if ($class === FALSE AND $function === FALSE) { return FALSE; } // ----------------------------------- // Set the in_progress flag // 在開始執行鉤子相應的程式之前,先把當前hook的狀態設為正在運行中。 // ----------------------------------- $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); } www.2cto.com // 執行相應程式完畢後,重新把當前hook的狀態改為非運行中 // 以讓它可以再次被觸發。 $this->in_progress = FALSE; return TRUE; } } // END CI_Hooks class /* End of file Hooks.php */ /* Location: ./system/core/Hooks.php */
http://www.bkjia.com/PHPjc/477711.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477711.htmlTechArticle[php] ?php if ( ! defined(BASEPATH)) exit(No direct script access allowed); /** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @packag...