How does codeigniter implement the hook mechanism?
How does codeigniter implement the hook mechanism ?, For more information, see. I remember the last time I went to the happy Interview. the interviewer asked me a question: how does codeigniter implement the hook mechanism? When I cannot answer the question, I can only find some information after I come back, so I will record it here: Codeigniter hooks are implemented as follows: first, the core file system/core/CodeIniter in the framework. the PHP file contains rows 122, loads the Hooks class, and defines several mount points in the file, such as pre_system (row 129) and post_controller_constructor (row 295, run the _ call_hook () method of the hooks class on these mount points. The source code of the codeigniter hooks class is also attached:
- /**
- * 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 mechanic 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 are 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 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 ('enable') 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
- *
- * Calla participant 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 participant 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 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 '];
- }
- 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 */
We can see that codeigniter is not elegant enough to implement the hook mechanism. In fact, you can use the observer mode to implement the hook mechanism and use the mount point as a listener event. |
How to implement codeigniter