Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * Hooks Class
42
 *
43
 * Provides a mechanism to extend the base system without hacking.
44
 *
45
 * @package		CodeIgniter
46
 * @subpackage	Libraries
47
 * @category	Libraries
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/general/hooks.html
50
 */
51
class CI_Hooks {
52
 
53
	/**
54
	 * Determines whether hooks are enabled
55
	 *
56
	 * @var	bool
57
	 */
58
	public $enabled = FALSE;
59
 
60
	/**
61
	 * List of all hooks set in config/hooks.php
62
	 *
63
	 * @var	array
64
	 */
65
	public $hooks =	array();
66
 
67
	/**
68
	 * Array with class objects to use hooks methods
69
	 *
70
	 * @var array
71
	 */
72
	protected $_objects = array();
73
 
74
	/**
75
	 * In progress flag
76
	 *
77
	 * Determines whether hook is in progress, used to prevent infinte loops
78
	 *
79
	 * @var	bool
80
	 */
81
	protected $_in_progress = FALSE;
82
 
83
	/**
84
	 * Class constructor
85
	 *
86
	 * @return	void
87
	 */
88
	public function __construct()
89
	{
90
		$CFG =& load_class('Config', 'core');
91
		log_message('info', 'Hooks Class Initialized');
92
 
93
		// If hooks are not enabled in the config file
94
		// there is nothing else to do
95
		if ($CFG->item('enable_hooks') === FALSE)
96
		{
97
			return;
98
		}
99
 
100
		// Grab the "hooks" definition file.
101
		if (file_exists(APPPATH.'config/hooks.php'))
102
		{
103
			include(APPPATH.'config/hooks.php');
104
		}
105
 
106
		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
107
		{
108
			include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
109
		}
110
 
111
		// If there are no hooks, we're done.
112
		if ( ! isset($hook) OR ! is_array($hook))
113
		{
114
			return;
115
		}
116
 
117
		$this->hooks =& $hook;
118
		$this->enabled = TRUE;
119
	}
120
 
121
	// --------------------------------------------------------------------
122
 
123
	/**
124
	 * Call Hook
125
	 *
126
	 * Calls a particular hook. Called by CodeIgniter.php.
127
	 *
128
	 * @uses	CI_Hooks::_run_hook()
129
	 *
130
	 * @param	string	$which	Hook name
131
	 * @return	bool	TRUE on success or FALSE on failure
132
	 */
133
	public function call_hook($which = '')
134
	{
135
		if ( ! $this->enabled OR ! isset($this->hooks[$which]))
136
		{
137
			return FALSE;
138
		}
139
 
140
		if (is_array($this->hooks[$which]) && ! isset($this->hooks[$which]['function']))
141
		{
142
			foreach ($this->hooks[$which] as $val)
143
			{
144
				$this->_run_hook($val);
145
			}
146
		}
147
		else
148
		{
149
			$this->_run_hook($this->hooks[$which]);
150
		}
151
 
152
		return TRUE;
153
	}
154
 
155
	// --------------------------------------------------------------------
156
 
157
	/**
158
	 * Run Hook
159
	 *
160
	 * Runs a particular hook
161
	 *
162
	 * @param	array	$data	Hook details
163
	 * @return	bool	TRUE on success or FALSE on failure
164
	 */
165
	protected function _run_hook($data)
166
	{
167
		// Closures/lambda functions and array($object, 'method') callables
168
		if (is_callable($data))
169
		{
170
			is_array($data)
171
				? $data[0]->{$data[1]}()
172
				: $data();
173
 
174
			return TRUE;
175
		}
176
		elseif ( ! is_array($data))
177
		{
178
			return FALSE;
179
		}
180
 
181
		// -----------------------------------
182
		// Safety - Prevents run-away loops
183
		// -----------------------------------
184
 
185
		// If the script being called happens to have the same
186
		// hook call within it a loop can happen
187
		if ($this->_in_progress === TRUE)
188
		{
189
			return;
190
		}
191
 
192
		// -----------------------------------
193
		// Set file path
194
		// -----------------------------------
195
 
196
		if ( ! isset($data['filepath'], $data['filename']))
197
		{
198
			return FALSE;
199
		}
200
 
201
		$filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
202
 
203
		if ( ! file_exists($filepath))
204
		{
205
			return FALSE;
206
		}
207
 
208
		// Determine and class and/or function names
209
		$class		= empty($data['class']) ? FALSE : $data['class'];
210
		$function	= empty($data['function']) ? FALSE : $data['function'];
211
		$params		= isset($data['params']) ? $data['params'] : '';
212
 
213
		if (empty($function))
214
		{
215
			return FALSE;
216
		}
217
 
218
		// Set the _in_progress flag
219
		$this->_in_progress = TRUE;
220
 
221
		// Call the requested class and/or function
222
		if ($class !== FALSE)
223
		{
224
			// The object is stored?
225
			if (isset($this->_objects[$class]))
226
			{
227
				if (method_exists($this->_objects[$class], $function))
228
				{
229
					$this->_objects[$class]->$function($params);
230
				}
231
				else
232
				{
233
					return $this->_in_progress = FALSE;
234
				}
235
			}
236
			else
237
			{
238
				class_exists($class, FALSE) OR require_once($filepath);
239
 
240
				if ( ! class_exists($class, FALSE) OR ! method_exists($class, $function))
241
				{
242
					return $this->_in_progress = FALSE;
243
				}
244
 
245
				// Store the object and execute the method
246
				$this->_objects[$class] = new $class();
247
				$this->_objects[$class]->$function($params);
248
			}
249
		}
250
		else
251
		{
252
			function_exists($function) OR require_once($filepath);
253
 
254
			if ( ! function_exists($function))
255
			{
256
				return $this->_in_progress = FALSE;
257
			}
258
 
259
			$function($params);
260
		}
261
 
262
		$this->_in_progress = FALSE;
263
		return TRUE;
264
	}
265
 
266
}