How does codeigniter implement the hook mechanism?

Source: Internet
Author: User
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:

  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 mechanic 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 are 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 are 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 are no hooks, we're done.
  73. If (defined ('enable') 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. * Calla participant 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 participant 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 have 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 */

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

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.