CI Framework source reading---------Hooks hooks.php
_initialize (); Log_message (' Debug ', "Hooks Class Initialized");} --------------------------------/** * Initialize the Hooks Preferences parameter, preferences * Initialize hooks * @accessprivate * @returnvoid * * function _initialize () {$CFG =& load_class (' Config ', ' core ');/If hooks is not enabled in the Config file//there is Nothing else to do//if the configuration file is set to not allow hooks, then return directly to exit this function. if ($CFG->item (' enable_hooks ') = = FALSE) {return;} Grab the definition file for the "hooks" definition file.//grab hooks//If there is no hooks, we ' re done.//if no hooks.php defined $hook array we return directly 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;} Reference the $hook array in hooks.php to $this->hooks//to open $this->enabled$this->hooks =& $hook; $this->enabled = TRUE;} --------------------------------/** * Call Hook * External is actually called this _call_hook function to tuneUse the hook procedure. * And this method calls _run_hook to execute the corresponding hooks. * Calls a particular hook * * @accessprivate * @paramstringthe Hook name * @returnmixed */function _call_hook ($which = ") {//Determine if the $this->enabled is open and the hook to be called is present in $htis->hooks. if (! $this->enabled OR! isset ($this->hooks[$which])) {return FALSE;} Determines whether the hook to invoke is a two-dimensional array, or if it is a traversal execution. If it is not a two-dimensional array is executed directly//here, it is possible to execute multiple hooks at a hook point, which is achieved by defining a two-dimensional array. 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 * Running hooks * Runs a particular special hooks * * @accessprivate * @paramarraythe Hook Details * @returnbool */function _run_hook ($data) {/* * $data is our hook array defined in apppath/config/hook.php * $hook [ ' Pre_controller ' = Array (* ' class ' = ' MyClass ', * ' function ' = ' Myfunction '), * ' filename ' = ' myclass.ph 'P ', * ' filepath ' = ' hooks ', * ' params ' = = Array (' Beer ', ' wine ', ' snacks ') * ); * Because each hook is definitely composed of an array * So here's the judgment $data is not an array if not then return * */IF (! Is_array ($data)) {return FALSE;} -----------------------------------//safety-prevents run-away loops//-----------------------------------//If The script being called happens to having the same//hook call within it a loop can happen//if a hook is called to execute certain scripts, and it is possible that these scripts will Trigger other hook//If this other hook contains the current//hook, then it will enter the dead loop, the in_progress exists to prevent this situation. if ($this->in_progress = = TRUE) {return;} -----------------------------------//Take out data inside the 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 = ";//Remove the class function params if (isset ($ $hooks) in the 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//the status of the current hook to be running before starting to execute the corresponding program for the hook. -----------------------------------$this->in_progress = true;//-----------------------------------//Call the Requested class and/or function//contains the hook file and instantiates the class, calling the function//-----------------------------------if ($class!== FALSE) {if (! clas S_exists ($class)) {require ($filepath);} $HOOK = new $class, $HOOK-$function ($params);} Else{if (! function_exists ($function)) {require ($filepath);} $function ($params);} After executing the corresponding program, re-change the status of the current hook to non-running//so that it can be triggered again. $this->in_progress = False;return TRUE;}} End Ci_hooks class/* end of File hooks.php *//* location:./system/core/hooks.php */