source: sourcecode/system/core/Hooks.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 4.6 KB
Line 
1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package             CodeIgniter
8 * @author              ExpressionEngine Dev Team
9 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license             http://codeigniter.com/user_guide/license.html
11 * @link                http://codeigniter.com
12 * @since               Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Hooks Class
20 *
21 * Provides a mechanism to extend the base system without hacking.
22 *
23 * @package             CodeIgniter
24 * @subpackage  Libraries
25 * @category    Libraries
26 * @author              ExpressionEngine Dev Team
27 * @link                http://codeigniter.com/user_guide/libraries/encryption.html
28 */
29class CI_Hooks {
30
31        /**
32         * Determines wether hooks are enabled
33         *
34         * @var bool
35         */
36        var $enabled            = FALSE;
37        /**
38         * List of all hooks set in config/hooks.php
39         *
40         * @var array
41         */
42        var $hooks                      = array();
43        /**
44         * Determines wether hook is in progress, used to prevent infinte loops
45         *
46         * @var bool
47         */
48        var $in_progress        = FALSE;
49
50        /**
51         * Constructor
52         *
53         */
54        function __construct()
55        {
56                $this->_initialize();
57                log_message('debug', "Hooks Class Initialized");
58        }
59
60        // --------------------------------------------------------------------
61
62        /**
63         * Initialize the Hooks Preferences
64         *
65         * @access      private
66         * @return      void
67         */
68        function _initialize()
69        {
70                $CFG =& load_class('Config', 'core');
71
72                // If hooks are not enabled in the config file
73                // there is nothing else to do
74
75                if ($CFG->item('enable_hooks') == FALSE)
76                {
77                        return;
78                }
79
80                // Grab the "hooks" definition file.
81                // If there are no hooks, we're done.
82
83                if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
84                {
85                    include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
86                }
87                elseif (is_file(APPPATH.'config/hooks.php'))
88                {
89                        include(APPPATH.'config/hooks.php');
90                }
91
92
93                if ( ! isset($hook) OR ! is_array($hook))
94                {
95                        return;
96                }
97
98                $this->hooks =& $hook;
99                $this->enabled = TRUE;
100        }
101
102        // --------------------------------------------------------------------
103
104        /**
105         * Call Hook
106         *
107         * Calls a particular hook
108         *
109         * @access      private
110         * @param       string  the hook name
111         * @return      mixed
112         */
113        function _call_hook($which = '')
114        {
115                if ( ! $this->enabled OR ! isset($this->hooks[$which]))
116                {
117                        return FALSE;
118                }
119
120                if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
121                {
122                        foreach ($this->hooks[$which] as $val)
123                        {
124                                $this->_run_hook($val);
125                        }
126                }
127                else
128                {
129                        $this->_run_hook($this->hooks[$which]);
130                }
131
132                return TRUE;
133        }
134
135        // --------------------------------------------------------------------
136
137        /**
138         * Run Hook
139         *
140         * Runs a particular hook
141         *
142         * @access      private
143         * @param       array   the hook details
144         * @return      bool
145         */
146        function _run_hook($data)
147        {
148                if ( ! is_array($data))
149                {
150                        return FALSE;
151                }
152
153                // -----------------------------------
154                // Safety - Prevents run-away loops
155                // -----------------------------------
156
157                // If the script being called happens to have the same
158                // hook call within it a loop can happen
159
160                if ($this->in_progress == TRUE)
161                {
162                        return;
163                }
164
165                // -----------------------------------
166                // Set file path
167                // -----------------------------------
168
169                if ( ! isset($data['filepath']) OR ! isset($data['filename']))
170                {
171                        return FALSE;
172                }
173
174                $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
175
176                if ( ! file_exists($filepath))
177                {
178                        return FALSE;
179                }
180
181                // -----------------------------------
182                // Set class/function name
183                // -----------------------------------
184
185                $class          = FALSE;
186                $function       = FALSE;
187                $params         = '';
188
189                if (isset($data['class']) AND $data['class'] != '')
190                {
191                        $class = $data['class'];
192                }
193
194                if (isset($data['function']))
195                {
196                        $function = $data['function'];
197                }
198
199                if (isset($data['params']))
200                {
201                        $params = $data['params'];
202                }
203
204                if ($class === FALSE AND $function === FALSE)
205                {
206                        return FALSE;
207                }
208
209                // -----------------------------------
210                // Set the in_progress flag
211                // -----------------------------------
212
213                $this->in_progress = TRUE;
214
215                // -----------------------------------
216                // Call the requested class and/or function
217                // -----------------------------------
218
219                if ($class !== FALSE)
220                {
221                        if ( ! class_exists($class))
222                        {
223                                require($filepath);
224                        }
225
226                        $HOOK = new $class;
227                        $HOOK->$function($params);
228                }
229                else
230                {
231                        if ( ! function_exists($function))
232                        {
233                                require($filepath);
234                        }
235
236                        $function($params);
237                }
238
239                $this->in_progress = FALSE;
240                return TRUE;
241        }
242
243}
244
245// END CI_Hooks class
246
247/* End of file Hooks.php */
248/* Location: ./system/core/Hooks.php */
Note: See TracBrowser for help on using the repository browser.