CI framework source code reading --------- hook class hooks. php

Source: Internet
Author: User
[Php] & lt ;? Phpif (! Defined (& amp; #39; BASEPATH & amp; #39;) exit (& amp; #39; Nodirectscriptaccessallowed & amp; #39 ;); /*** CodeIgniter *** Anopensourceapplication [php] _ Initialize (); log_message ('debug', "Hooks Class Initialized");} // specify/*** Initialize the Hooks Preferences parameter, preference * initialization hook * @ 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 hooks is not allowed in the configuration file, exit this function directly. Number. If ($ CFG-> item ('enable _ Hooks') = FALSE) {return;} // Grab the "hooks" definition file. // capture the hook definition file // If there are no hooks, we're done. // If hooks is not defined. php does not define the $ hook array. we directly return 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;} // Set hooks. reference the $ hook array in php to $ this-> hooks // enable $ this-> enabled $ this-> hooks = & $ hook; $ this-> enabled = TRUE ;} // ----------------------------------------------------------------------/*** Call Hook * is actually calling the _ call_hook function to Call the Hook program. * In this method, _ run_hook is called to execute the corresponding hook. * Calla participant hook ** @ access private * @ param string the hook name * @ return mixed */function _ call_hook ($ which = '') {// Determine whether $ this-> enabled is enabled and whether the hook to be called exists in $ htis-> hooks. If (! $ This-> enabled OR! Isset ($ this-> hooks [$ which]) {return FALSE;} // checks whether the hook to be called is a two-dimensional array. If yes, it traverses and runs. // If it is not a two-dimensional array, execute it directly. // here, multiple hooks can be executed at a hook point, which is implemented 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 ;} // hooks/*** Run Hook * Runs a special hook of the particle ** @ access private * @ param array the hook details * @ return bool * /Function _ run_hook ($ data) {/** $ data is stored in APPPATH/config/hook. php-defined hook array * $ hook ['pre _ controller'] = array (* 'class' => 'myclass', * 'function' => 'myfunction ', * 'filename' => 'myclass. php ', * 'filepath' => 'Hooks', * 'params' => array ('beer', 'wine', 'snacks ')*); ** because each hook must be composed of arrays *, we can determine whether $ data is an array. if not, the system returns **/if (! Is_array ($ data) {return FALSE ;} // outputs // Safety-Prevents run-away loops // --------------------------------------- // If the script being called happens to have the same // hook call within it a loop can happen // If call a hook, execute some scripts, and it is possible that these scripts will trigger other hooks. // if the other hooks contain the current/hook, it will enter an endless loop, this in_progress is to prevent this situation. If ($ this-> in_progress = TRUE) {return;} // ----------------------------------- // extract the data in the data and load 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/functions $ class = FALSE; $ function = FALSE; $ params = ''; // Retrieve the class function params if (isset ($ data ['class']) AND $ data ['class'] in $ hooks! = '') {$ 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 // Set the current hook status to running before running the program. // ----------------------------------- $ This-> in_progress = TRUE; // ----------------------------------- // Call the requested class and/or function // contains the hook file and instantiate the class, call the 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 // after the corresponding program is executed, change the current hook status to not 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 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.