How does the CodeIgniter implement the hook mechanism? , there is a need for friends to refer to the next. Remember the last time I went to the interview, the interviewer asked me a question: How does the CodeIgniter implement the hook mechanism? At that time I did not answer, and later came back to check some information to understand, so here to record: CodeIgniter's hooks are implemented by first loading the hooks class in the frame's core file, system/core/codeiniter.php, 122 lines, and then defining several mount points in the file, such as Pre_system (129 rows), Post_controller_constructor (295 rows), and execute the _call_hook () method of the hooks class above these mount points. Additional source code for the hooks class with CodeIgniter:
- /**
- * CodeIgniter
- *
- * An open source application development framework for PHP 5.1.6 or newer
- *
- * @package CodeIgniter
- * @author Ellislab Dev Team
- * @copyright Copyright (c) 2008-2014, Ellislab, Inc.
- * @copyright Copyright (c) 2014-2015, British Columbia Institute of Technology (http://bcit.ca/)
- * @license http://codeigniter.com/user_guide/license.html
- * [url=home.php?mod=space&uid=17823] @LINK [/url] http://codeigniter.com
- * @since Version 1.0
- * @filesource
- */
- // ------------------------------------------------------------------------
- /**
- * CodeIgniter Hooks Class
- *
- * Provides a mechanism to extend the base system without hacking.
- *
- * @package CodeIgniter
- * @subpackage Libraries
- * @category Libraries
- * @author Ellislab Dev Team
- * @link http://codeigniter.com/user_guide/libraries/encryption.html
- */
- Class Ci_hooks {
- /**
- * Determines wether hooks is enabled
- *
- * @var BOOL
- */
- var $enabled = FALSE;
- /**
- * List of all hooks set in config/hooks.php
- *
- * @var Array
- */
- var $hooks = array ();
- /**
- * Determines wether hook is in progress, used to prevent infinte loops
- *
- * @var BOOL
- */
- var $in _progress = FALSE;
- /**
- * Constructor
- *
- */
- function __construct ()
- {
- $this->_initialize ();
- Log_message (' Debug ', ' Hooks Class Initialized ');
- }
- // --------------------------------------------------------------------
- /**
- * Initialize the Hooks Preferences
- *
- * @access Private
- * @return void
- */
- function _initialize ()
- {
- $CFG =& load_class (' Config ', ' core ');
- If hooks is 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 is 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
- *
- * 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
- *
- * Runs a particular hook
- *
- * @access Private
- * @param array the hook details
- * @return BOOL
- */
- function _run_hook ($data)
- {
- if (! Is_array ($data))
- {
- return FALSE;
- }
- // -----------------------------------
- Safety-prevents run-away Loops
- // -----------------------------------
- If the script being called happens to 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 '];
- }
- 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 */
Copy CodeIt can be seen that codeigniter implementation of the hook mechanism is not elegant, in fact, you can use the Observer mode to implement the hook mechanism, the mount point as a listener event. |