How does the CodeIgniter implement the hook mechanism?

Source: Internet
Author: User
Tags codeigniter

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:

  1. /**
  2. * CodeIgniter
  3. *
  4. * An open source application development framework for PHP 5.1.6 or newer
  5. *
  6. * @package CodeIgniter
  7. * @author Ellislab Dev Team
  8. * @copyright Copyright (c) 2008-2014, Ellislab, Inc.
  9. * @copyright Copyright (c) 2014-2015, British Columbia Institute of Technology (http://bcit.ca/)
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * [url=home.php?mod=space&uid=17823] @LINK [/url] http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Hooks Class
  18. *
  19. * Provides a mechanism to extend the base system without hacking.
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category Libraries
  24. * @author Ellislab Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/encryption.html
  26. */
  27. Class Ci_hooks {
  28. /**
  29. * Determines wether hooks is enabled
  30. *
  31. * @var BOOL
  32. */
  33. var $enabled = FALSE;
  34. /**
  35. * List of all hooks set in config/hooks.php
  36. *
  37. * @var Array
  38. */
  39. var $hooks = array ();
  40. /**
  41. * Determines wether hook is in progress, used to prevent infinte loops
  42. *
  43. * @var BOOL
  44. */
  45. var $in _progress = FALSE;
  46. /**
  47. * Constructor
  48. *
  49. */
  50. function __construct ()
  51. {
  52. $this->_initialize ();
  53. Log_message (' Debug ', ' Hooks Class Initialized ');
  54. }
  55. // --------------------------------------------------------------------
  56. /**
  57. * Initialize the Hooks Preferences
  58. *
  59. * @access Private
  60. * @return void
  61. */
  62. function _initialize ()
  63. {
  64. $CFG =& load_class (' Config ', ' core ');
  65. If hooks is not enabled in the config file
  66. There is nothing else to do
  67. if ($CFG->item (' enable_hooks ') = = FALSE)
  68. {
  69. Return
  70. }
  71. Grab the "hooks" definition file.
  72. If There is no hooks, we ' re done.
  73. if (defined (' Environment ') and Is_file (APPPATH. ' config/'). Environment. ' /hooks.php '))
  74. {
  75. Include (APPPATH. ' config/'). Environment. ' /hooks.php ');
  76. }
  77. ElseIf (Is_file (APPPATH. ' config/hooks.php '))
  78. {
  79. Include (APPPATH. ' config/hooks.php ');
  80. }
  81. if (! isset ($hook) OR! Is_array ($hook))
  82. {
  83. Return
  84. }
  85. $this->hooks =& $hook;
  86. $this->enabled = TRUE;
  87. }
  88. // --------------------------------------------------------------------
  89. /**
  90. * Call Hook
  91. *
  92. * Calls a particular hook
  93. *
  94. * @access Private
  95. * @param string The hook name
  96. * @return Mixed
  97. */
  98. function _call_hook ($which = ")
  99. {
  100. if (! $this->enabled OR! isset ($this->hooks[$which]))
  101. {
  102. return FALSE;
  103. }
  104. if (Isset ($this->hooks[$which][0]) and Is_array ($this->hooks[$which][0]))
  105. {
  106. foreach ($this->hooks[$which] as $val)
  107. {
  108. $this->_run_hook ($val);
  109. }
  110. }
  111. Else
  112. {
  113. $this->_run_hook ($this->hooks[$which]);
  114. }
  115. return TRUE;
  116. }
  117. // --------------------------------------------------------------------
  118. /**
  119. * Run Hook
  120. *
  121. * Runs a particular hook
  122. *
  123. * @access Private
  124. * @param array the hook details
  125. * @return BOOL
  126. */
  127. function _run_hook ($data)
  128. {
  129. if (! Is_array ($data))
  130. {
  131. return FALSE;
  132. }
  133. // -----------------------------------
  134. Safety-prevents run-away Loops
  135. // -----------------------------------
  136. If the script being called happens to the same
  137. Hook call within it a loop can happen
  138. if ($this->in_progress = = TRUE)
  139. {
  140. Return
  141. }
  142. // -----------------------------------
  143. Set file path
  144. // -----------------------------------
  145. if (! isset ($data [' filepath ']) OR! isset ($data [' filename ']))
  146. {
  147. return FALSE;
  148. }
  149. $filepath = APPPATH. $data [' filepath ']. ' /'. $data [' filename '];
  150. if (! file_exists ($filepath))
  151. {
  152. return FALSE;
  153. }
  154. // -----------------------------------
  155. Set class/function Name
  156. // -----------------------------------
  157. $class = FALSE;
  158. $function = FALSE;
  159. $params = ";
  160. if (Isset ($data [' class ']) and $data [' class ']! = ')
  161. {
  162. $class = $data [' class '];
  163. }
  164. if (Isset ($data [' function ')])
  165. {
  166. $function = $data [' function '];
  167. }
  168. if (Isset ($data [' params ']))
  169. {
  170. $params = $data [' params '];
  171. }
  172. if ($class = = = False and $function = = = False)
  173. {
  174. return FALSE;
  175. }
  176. // -----------------------------------
  177. Set the in_progress flag
  178. // -----------------------------------
  179. $this->in_progress = TRUE;
  180. // -----------------------------------
  181. Call the requested class and/or function
  182. // -----------------------------------
  183. if ($class!== FALSE)
  184. {
  185. if (! class_exists ($class))
  186. {
  187. Require ($filepath);
  188. }
  189. $HOOK = new $class;
  190. $HOOK-$function ($params);
  191. }
  192. Else
  193. {
  194. if (! function_exists ($function))
  195. {
  196. Require ($filepath);
  197. }
  198. $function ($params);
  199. }
  200. $this->in_progress = FALSE;
  201. return TRUE;
  202. }
  203. }
  204. END Ci_hooks Class
  205. /* End of File hooks.php */
  206. /* location:./system/core/hooks.php */
Copy Code

It 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.

How to achieve, CodeIgniter
  • 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.