Subversion-Projekte lars-tiefland.ci

Revision

Revision 2242 | Revision 2257 | Zur aktuellen Revision | 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
 *
2254 lars 9
 * Copyright (c) 2014 - 2017, 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/)
2254 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 33
 * @license	http://opensource.org/licenses/MIT	MIT License
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
 * Loader Class
42
 *
43
 * Loads framework components.
44
 *
45
 * @package		CodeIgniter
46
 * @subpackage	Libraries
47
 * @category	Loader
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/libraries/loader.html
50
 */
51
class CI_Loader {
52
 
53
	// All these are set automatically. Don't mess with them.
54
	/**
55
	 * Nesting level of the output buffering mechanism
56
	 *
57
	 * @var	int
58
	 */
59
	protected $_ci_ob_level;
60
 
61
	/**
62
	 * List of paths to load views from
63
	 *
64
	 * @var	array
65
	 */
66
	protected $_ci_view_paths =	array(VIEWPATH	=> TRUE);
67
 
68
	/**
69
	 * List of paths to load libraries from
70
	 *
71
	 * @var	array
72
	 */
73
	protected $_ci_library_paths =	array(APPPATH, BASEPATH);
74
 
75
	/**
76
	 * List of paths to load models from
77
	 *
78
	 * @var	array
79
	 */
80
	protected $_ci_model_paths =	array(APPPATH);
81
 
82
	/**
83
	 * List of paths to load helpers from
84
	 *
85
	 * @var	array
86
	 */
87
	protected $_ci_helper_paths =	array(APPPATH, BASEPATH);
88
 
89
	/**
90
	 * List of cached variables
91
	 *
92
	 * @var	array
93
	 */
94
	protected $_ci_cached_vars =	array();
95
 
96
	/**
97
	 * List of loaded classes
98
	 *
99
	 * @var	array
100
	 */
101
	protected $_ci_classes =	array();
102
 
103
	/**
104
	 * List of loaded models
105
	 *
106
	 * @var	array
107
	 */
108
	protected $_ci_models =	array();
109
 
110
	/**
111
	 * List of loaded helpers
112
	 *
113
	 * @var	array
114
	 */
115
	protected $_ci_helpers =	array();
116
 
117
	/**
118
	 * List of class name mappings
119
	 *
120
	 * @var	array
121
	 */
122
	protected $_ci_varmap =	array(
123
		'unit_test' => 'unit',
124
		'user_agent' => 'agent'
125
	);
126
 
127
	// --------------------------------------------------------------------
128
 
129
	/**
130
	 * Class constructor
131
	 *
132
	 * Sets component load paths, gets the initial output buffering level.
133
	 *
134
	 * @return	void
135
	 */
136
	public function __construct()
137
	{
138
		$this->_ci_ob_level = ob_get_level();
139
		$this->_ci_classes =& is_loaded();
140
 
141
		log_message('info', 'Loader Class Initialized');
142
	}
143
 
144
	// --------------------------------------------------------------------
145
 
146
	/**
147
	 * Initializer
148
	 *
149
	 * @todo	Figure out a way to move this to the constructor
150
	 *		without breaking *package_path*() methods.
151
	 * @uses	CI_Loader::_ci_autoloader()
152
	 * @used-by	CI_Controller::__construct()
153
	 * @return	void
154
	 */
155
	public function initialize()
156
	{
157
		$this->_ci_autoloader();
158
	}
159
 
160
	// --------------------------------------------------------------------
161
 
162
	/**
163
	 * Is Loaded
164
	 *
165
	 * A utility method to test if a class is in the self::$_ci_classes array.
166
	 *
167
	 * @used-by	Mainly used by Form Helper function _get_validation_object().
168
	 *
169
	 * @param 	string		$class	Class name to check for
170
	 * @return 	string|bool	Class object name if loaded or FALSE
171
	 */
172
	public function is_loaded($class)
173
	{
174
		return array_search(ucfirst($class), $this->_ci_classes, TRUE);
175
	}
176
 
177
	// --------------------------------------------------------------------
178
 
179
	/**
180
	 * Library Loader
181
	 *
182
	 * Loads and instantiates libraries.
183
	 * Designed to be called from application controllers.
184
	 *
2107 lars 185
	 * @param	mixed	$library	Library name
68 lars 186
	 * @param	array	$params		Optional parameters to pass to the library class constructor
187
	 * @param	string	$object_name	An optional object name to assign to
188
	 * @return	object
189
	 */
190
	public function library($library, $params = NULL, $object_name = NULL)
191
	{
192
		if (empty($library))
193
		{
194
			return $this;
195
		}
196
		elseif (is_array($library))
197
		{
198
			foreach ($library as $key => $value)
199
			{
200
				if (is_int($key))
201
				{
202
					$this->library($value, $params);
203
				}
204
				else
205
				{
206
					$this->library($key, $params, $value);
207
				}
208
			}
209
 
210
			return $this;
211
		}
212
 
213
		if ($params !== NULL && ! is_array($params))
214
		{
215
			$params = NULL;
216
		}
217
 
218
		$this->_ci_load_library($library, $params, $object_name);
219
		return $this;
220
	}
221
 
222
	// --------------------------------------------------------------------
223
 
224
	/**
225
	 * Model Loader
226
	 *
227
	 * Loads and instantiates models.
228
	 *
2254 lars 229
	 * @param	string	$model		Model name
68 lars 230
	 * @param	string	$name		An optional object name to assign to
231
	 * @param	bool	$db_conn	An optional database connection configuration to initialize
232
	 * @return	object
233
	 */
234
	public function model($model, $name = '', $db_conn = FALSE)
235
	{
236
		if (empty($model))
237
		{
238
			return $this;
239
		}
240
		elseif (is_array($model))
241
		{
242
			foreach ($model as $key => $value)
243
			{
244
				is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn);
245
			}
246
 
247
			return $this;
248
		}
249
 
250
		$path = '';
251
 
252
		// Is the model in a sub-folder? If so, parse out the filename and path.
253
		if (($last_slash = strrpos($model, '/')) !== FALSE)
254
		{
255
			// The path is in front of the last slash
256
			$path = substr($model, 0, ++$last_slash);
257
 
258
			// And the model name behind it
259
			$model = substr($model, $last_slash);
260
		}
261
 
262
		if (empty($name))
263
		{
264
			$name = $model;
265
		}
266
 
267
		if (in_array($name, $this->_ci_models, TRUE))
268
		{
269
			return $this;
270
		}
271
 
272
		$CI =& get_instance();
273
		if (isset($CI->$name))
274
		{
275
			throw new RuntimeException('The model name you are loading is the name of a resource that is already being used: '.$name);
276
		}
277
 
278
		if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
279
		{
280
			if ($db_conn === TRUE)
281
			{
282
				$db_conn = '';
283
			}
284
 
285
			$this->database($db_conn, FALSE, TRUE);
286
		}
287
 
288
		// Note: All of the code under this condition used to be just:
289
		//
290
		//       load_class('Model', 'core');
291
		//
292
		//       However, load_class() instantiates classes
293
		//       to cache them for later use and that prevents
294
		//       MY_Model from being an abstract class and is
295
		//       sub-optimal otherwise anyway.
296
		if ( ! class_exists('CI_Model', FALSE))
297
		{
298
			$app_path = APPPATH.'core'.DIRECTORY_SEPARATOR;
299
			if (file_exists($app_path.'Model.php'))
300
			{
301
				require_once($app_path.'Model.php');
302
				if ( ! class_exists('CI_Model', FALSE))
303
				{
304
					throw new RuntimeException($app_path."Model.php exists, but doesn't declare class CI_Model");
305
				}
306
			}
307
			elseif ( ! class_exists('CI_Model', FALSE))
308
			{
309
				require_once(BASEPATH.'core'.DIRECTORY_SEPARATOR.'Model.php');
310
			}
311
 
312
			$class = config_item('subclass_prefix').'Model';
313
			if (file_exists($app_path.$class.'.php'))
314
			{
315
				require_once($app_path.$class.'.php');
316
				if ( ! class_exists($class, FALSE))
317
				{
318
					throw new RuntimeException($app_path.$class.".php exists, but doesn't declare class ".$class);
319
				}
320
			}
321
		}
322
 
323
		$model = ucfirst($model);
324
		if ( ! class_exists($model, FALSE))
325
		{
326
			foreach ($this->_ci_model_paths as $mod_path)
327
			{
328
				if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
329
				{
330
					continue;
331
				}
332
 
333
				require_once($mod_path.'models/'.$path.$model.'.php');
334
				if ( ! class_exists($model, FALSE))
335
				{
336
					throw new RuntimeException($mod_path."models/".$path.$model.".php exists, but doesn't declare class ".$model);
337
				}
338
 
339
				break;
340
			}
341
 
342
			if ( ! class_exists($model, FALSE))
343
			{
344
				throw new RuntimeException('Unable to locate the model you have specified: '.$model);
345
			}
346
		}
347
		elseif ( ! is_subclass_of($model, 'CI_Model'))
348
		{
349
			throw new RuntimeException("Class ".$model." already exists and doesn't extend CI_Model");
350
		}
351
 
352
		$this->_ci_models[] = $name;
2254 lars 353
		$CI->$name = new $model();
68 lars 354
		return $this;
355
	}
356
 
357
	// --------------------------------------------------------------------
358
 
359
	/**
360
	 * Database Loader
361
	 *
362
	 * @param	mixed	$params		Database configuration options
363
	 * @param	bool	$return 	Whether to return the database object
364
	 * @param	bool	$query_builder	Whether to enable Query Builder
365
	 *					(overrides the configuration setting)
366
	 *
367
	 * @return	object|bool	Database object if $return is set to TRUE,
368
	 *					FALSE on failure, CI_Loader instance in any other case
369
	 */
370
	public function database($params = '', $return = FALSE, $query_builder = NULL)
371
	{
372
		// Grab the super object
373
		$CI =& get_instance();
374
 
375
		// Do we even need to load the database class?
376
		if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
377
		{
378
			return FALSE;
379
		}
380
 
381
		require_once(BASEPATH.'database/DB.php');
382
 
383
		if ($return === TRUE)
384
		{
385
			return DB($params, $query_builder);
386
		}
387
 
388
		// Initialize the db variable. Needed to prevent
389
		// reference errors with some configurations
390
		$CI->db = '';
391
 
392
		// Load the DB class
393
		$CI->db =& DB($params, $query_builder);
394
		return $this;
395
	}
396
 
397
	// --------------------------------------------------------------------
398
 
399
	/**
400
	 * Load the Database Utilities Class
401
	 *
402
	 * @param	object	$db	Database object
403
	 * @param	bool	$return	Whether to return the DB Utilities class object or not
404
	 * @return	object
405
	 */
406
	public function dbutil($db = NULL, $return = FALSE)
407
	{
408
		$CI =& get_instance();
409
 
410
		if ( ! is_object($db) OR ! ($db instanceof CI_DB))
411
		{
412
			class_exists('CI_DB', FALSE) OR $this->database();
413
			$db =& $CI->db;
414
		}
415
 
416
		require_once(BASEPATH.'database/DB_utility.php');
417
		require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
418
		$class = 'CI_DB_'.$db->dbdriver.'_utility';
419
 
420
		if ($return === TRUE)
421
		{
422
			return new $class($db);
423
		}
424
 
425
		$CI->dbutil = new $class($db);
426
		return $this;
427
	}
428
 
429
	// --------------------------------------------------------------------
430
 
431
	/**
432
	 * Load the Database Forge Class
433
	 *
434
	 * @param	object	$db	Database object
435
	 * @param	bool	$return	Whether to return the DB Forge class object or not
436
	 * @return	object
437
	 */
438
	public function dbforge($db = NULL, $return = FALSE)
439
	{
440
		$CI =& get_instance();
441
		if ( ! is_object($db) OR ! ($db instanceof CI_DB))
442
		{
443
			class_exists('CI_DB', FALSE) OR $this->database();
444
			$db =& $CI->db;
445
		}
446
 
447
		require_once(BASEPATH.'database/DB_forge.php');
448
		require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
449
 
450
		if ( ! empty($db->subdriver))
451
		{
452
			$driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
453
			if (file_exists($driver_path))
454
			{
455
				require_once($driver_path);
456
				$class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
457
			}
458
		}
459
		else
460
		{
461
			$class = 'CI_DB_'.$db->dbdriver.'_forge';
462
		}
463
 
464
		if ($return === TRUE)
465
		{
466
			return new $class($db);
467
		}
468
 
469
		$CI->dbforge = new $class($db);
470
		return $this;
471
	}
472
 
473
	// --------------------------------------------------------------------
474
 
475
	/**
476
	 * View Loader
477
	 *
478
	 * Loads "view" files.
479
	 *
480
	 * @param	string	$view	View name
481
	 * @param	array	$vars	An associative array of data
482
	 *				to be extracted for use in the view
483
	 * @param	bool	$return	Whether to return the view output
484
	 *				or leave it to the Output class
485
	 * @return	object|string
486
	 */
487
	public function view($view, $vars = array(), $return = FALSE)
488
	{
2049 lars 489
		return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
68 lars 490
	}
491
 
492
	// --------------------------------------------------------------------
493
 
494
	/**
495
	 * Generic File Loader
496
	 *
497
	 * @param	string	$path	File path
498
	 * @param	bool	$return	Whether to return the file output
499
	 * @return	object|string
500
	 */
501
	public function file($path, $return = FALSE)
502
	{
503
		return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
504
	}
505
 
506
	// --------------------------------------------------------------------
507
 
508
	/**
509
	 * Set Variables
510
	 *
511
	 * Once variables are set they become available within
512
	 * the controller class and its "view" files.
513
	 *
514
	 * @param	array|object|string	$vars
515
	 *					An associative array or object containing values
516
	 *					to be set, or a value's name if string
517
	 * @param 	string	$val	Value to set, only used if $vars is a string
518
	 * @return	object
519
	 */
520
	public function vars($vars, $val = '')
521
	{
2049 lars 522
		$vars = is_string($vars)
523
			? array($vars => $val)
524
			: $this->_ci_prepare_view_vars($vars);
68 lars 525
 
2049 lars 526
		foreach ($vars as $key => $val)
68 lars 527
		{
2049 lars 528
			$this->_ci_cached_vars[$key] = $val;
68 lars 529
		}
530
 
531
		return $this;
532
	}
533
 
534
	// --------------------------------------------------------------------
535
 
536
	/**
537
	 * Clear Cached Variables
538
	 *
539
	 * Clears the cached variables.
540
	 *
541
	 * @return	CI_Loader
542
	 */
543
	public function clear_vars()
544
	{
545
		$this->_ci_cached_vars = array();
546
		return $this;
547
	}
548
 
549
	// --------------------------------------------------------------------
550
 
551
	/**
552
	 * Get Variable
553
	 *
554
	 * Check if a variable is set and retrieve it.
555
	 *
556
	 * @param	string	$key	Variable name
557
	 * @return	mixed	The variable or NULL if not found
558
	 */
559
	public function get_var($key)
560
	{
561
		return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
562
	}
563
 
564
	// --------------------------------------------------------------------
565
 
566
	/**
567
	 * Get Variables
568
	 *
569
	 * Retrieves all loaded variables.
570
	 *
571
	 * @return	array
572
	 */
573
	public function get_vars()
574
	{
575
		return $this->_ci_cached_vars;
576
	}
577
 
578
	// --------------------------------------------------------------------
579
 
580
	/**
581
	 * Helper Loader
582
	 *
583
	 * @param	string|string[]	$helpers	Helper name(s)
584
	 * @return	object
585
	 */
586
	public function helper($helpers = array())
587
	{
2049 lars 588
		is_array($helpers) OR $helpers = array($helpers);
589
		foreach ($helpers as &$helper)
68 lars 590
		{
2049 lars 591
			$filename = basename($helper);
592
			$filepath = ($filename === $helper) ? '' : substr($helper, 0, strlen($helper) - strlen($filename));
2107 lars 593
			$filename = strtolower(preg_replace('#(_helper)?(\.php)?$#i', '', $filename)).'_helper';
2049 lars 594
			$helper   = $filepath.$filename;
595
 
68 lars 596
			if (isset($this->_ci_helpers[$helper]))
597
			{
598
				continue;
599
			}
600
 
601
			// Is this a helper extension request?
2049 lars 602
			$ext_helper = config_item('subclass_prefix').$filename;
68 lars 603
			$ext_loaded = FALSE;
604
			foreach ($this->_ci_helper_paths as $path)
605
			{
606
				if (file_exists($path.'helpers/'.$ext_helper.'.php'))
607
				{
608
					include_once($path.'helpers/'.$ext_helper.'.php');
609
					$ext_loaded = TRUE;
610
				}
611
			}
612
 
613
			// If we have loaded extensions - check if the base one is here
614
			if ($ext_loaded === TRUE)
615
			{
616
				$base_helper = BASEPATH.'helpers/'.$helper.'.php';
617
				if ( ! file_exists($base_helper))
618
				{
619
					show_error('Unable to load the requested file: helpers/'.$helper.'.php');
620
				}
621
 
622
				include_once($base_helper);
623
				$this->_ci_helpers[$helper] = TRUE;
624
				log_message('info', 'Helper loaded: '.$helper);
625
				continue;
626
			}
627
 
628
			// No extensions found ... try loading regular helpers and/or overrides
629
			foreach ($this->_ci_helper_paths as $path)
630
			{
631
				if (file_exists($path.'helpers/'.$helper.'.php'))
632
				{
633
					include_once($path.'helpers/'.$helper.'.php');
634
 
635
					$this->_ci_helpers[$helper] = TRUE;
636
					log_message('info', 'Helper loaded: '.$helper);
637
					break;
638
				}
639
			}
640
 
641
			// unable to load the helper
642
			if ( ! isset($this->_ci_helpers[$helper]))
643
			{
644
				show_error('Unable to load the requested file: helpers/'.$helper.'.php');
645
			}
646
		}
647
 
648
		return $this;
649
	}
650
 
651
	// --------------------------------------------------------------------
652
 
653
	/**
654
	 * Load Helpers
655
	 *
656
	 * An alias for the helper() method in case the developer has
657
	 * written the plural form of it.
658
	 *
659
	 * @uses	CI_Loader::helper()
660
	 * @param	string|string[]	$helpers	Helper name(s)
661
	 * @return	object
662
	 */
663
	public function helpers($helpers = array())
664
	{
665
		return $this->helper($helpers);
666
	}
667
 
668
	// --------------------------------------------------------------------
669
 
670
	/**
671
	 * Language Loader
672
	 *
673
	 * Loads language files.
674
	 *
675
	 * @param	string|string[]	$files	List of language file names to load
676
	 * @param	string		Language name
677
	 * @return	object
678
	 */
679
	public function language($files, $lang = '')
680
	{
681
		get_instance()->lang->load($files, $lang);
682
		return $this;
683
	}
684
 
685
	// --------------------------------------------------------------------
686
 
687
	/**
688
	 * Config Loader
689
	 *
690
	 * Loads a config file (an alias for CI_Config::load()).
691
	 *
692
	 * @uses	CI_Config::load()
693
	 * @param	string	$file			Configuration file name
694
	 * @param	bool	$use_sections		Whether configuration values should be loaded into their own section
695
	 * @param	bool	$fail_gracefully	Whether to just return FALSE or display an error message
696
	 * @return	bool	TRUE if the file was loaded correctly or FALSE on failure
697
	 */
698
	public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
699
	{
700
		return get_instance()->config->load($file, $use_sections, $fail_gracefully);
701
	}
702
 
703
	// --------------------------------------------------------------------
704
 
705
	/**
706
	 * Driver Loader
707
	 *
708
	 * Loads a driver library.
709
	 *
710
	 * @param	string|string[]	$library	Driver name(s)
711
	 * @param	array		$params		Optional parameters to pass to the driver
712
	 * @param	string		$object_name	An optional object name to assign to
713
	 *
714
	 * @return	object|bool	Object or FALSE on failure if $library is a string
715
	 *				and $object_name is set. CI_Loader instance otherwise.
716
	 */
717
	public function driver($library, $params = NULL, $object_name = NULL)
718
	{
719
		if (is_array($library))
720
		{
721
			foreach ($library as $key => $value)
722
			{
723
				if (is_int($key))
724
				{
725
					$this->driver($value, $params);
726
				}
727
				else
728
				{
729
					$this->driver($key, $params, $value);
730
				}
731
			}
732
 
733
			return $this;
734
		}
735
		elseif (empty($library))
736
		{
737
			return FALSE;
738
		}
739
 
740
		if ( ! class_exists('CI_Driver_Library', FALSE))
741
		{
742
			// We aren't instantiating an object here, just making the base class available
743
			require BASEPATH.'libraries/Driver.php';
744
		}
745
 
746
		// We can save the loader some time since Drivers will *always* be in a subfolder,
747
		// and typically identically named to the library
748
		if ( ! strpos($library, '/'))
749
		{
750
			$library = ucfirst($library).'/'.$library;
751
		}
752
 
753
		return $this->library($library, $params, $object_name);
754
	}
755
 
756
	// --------------------------------------------------------------------
757
 
758
	/**
759
	 * Add Package Path
760
	 *
761
	 * Prepends a parent path to the library, model, helper and config
762
	 * path arrays.
763
	 *
764
	 * @see	CI_Loader::$_ci_library_paths
765
	 * @see	CI_Loader::$_ci_model_paths
766
	 * @see CI_Loader::$_ci_helper_paths
767
	 * @see CI_Config::$_config_paths
768
	 *
769
	 * @param	string	$path		Path to add
770
	 * @param 	bool	$view_cascade	(default: TRUE)
771
	 * @return	object
772
	 */
773
	public function add_package_path($path, $view_cascade = TRUE)
774
	{
775
		$path = rtrim($path, '/').'/';
776
 
777
		array_unshift($this->_ci_library_paths, $path);
778
		array_unshift($this->_ci_model_paths, $path);
779
		array_unshift($this->_ci_helper_paths, $path);
780
 
781
		$this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
782
 
783
		// Add config file path
784
		$config =& $this->_ci_get_component('config');
785
		$config->_config_paths[] = $path;
786
 
787
		return $this;
788
	}
789
 
790
	// --------------------------------------------------------------------
791
 
792
	/**
793
	 * Get Package Paths
794
	 *
795
	 * Return a list of all package paths.
796
	 *
797
	 * @param	bool	$include_base	Whether to include BASEPATH (default: FALSE)
798
	 * @return	array
799
	 */
800
	public function get_package_paths($include_base = FALSE)
801
	{
802
		return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
803
	}
804
 
805
	// --------------------------------------------------------------------
806
 
807
	/**
808
	 * Remove Package Path
809
	 *
810
	 * Remove a path from the library, model, helper and/or config
811
	 * path arrays if it exists. If no path is provided, the most recently
812
	 * added path will be removed removed.
813
	 *
814
	 * @param	string	$path	Path to remove
815
	 * @return	object
816
	 */
817
	public function remove_package_path($path = '')
818
	{
819
		$config =& $this->_ci_get_component('config');
820
 
821
		if ($path === '')
822
		{
823
			array_shift($this->_ci_library_paths);
824
			array_shift($this->_ci_model_paths);
825
			array_shift($this->_ci_helper_paths);
826
			array_shift($this->_ci_view_paths);
827
			array_pop($config->_config_paths);
828
		}
829
		else
830
		{
831
			$path = rtrim($path, '/').'/';
832
			foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
833
			{
834
				if (($key = array_search($path, $this->{$var})) !== FALSE)
835
				{
836
					unset($this->{$var}[$key]);
837
				}
838
			}
839
 
840
			if (isset($this->_ci_view_paths[$path.'views/']))
841
			{
842
				unset($this->_ci_view_paths[$path.'views/']);
843
			}
844
 
845
			if (($key = array_search($path, $config->_config_paths)) !== FALSE)
846
			{
847
				unset($config->_config_paths[$key]);
848
			}
849
		}
850
 
851
		// make sure the application default paths are still in the array
852
		$this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
853
		$this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
854
		$this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
855
		$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
856
		$config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
857
 
858
		return $this;
859
	}
860
 
861
	// --------------------------------------------------------------------
862
 
863
	/**
864
	 * Internal CI Data Loader
865
	 *
866
	 * Used to load views and files.
867
	 *
868
	 * Variables are prefixed with _ci_ to avoid symbol collision with
869
	 * variables made available to view files.
870
	 *
871
	 * @used-by	CI_Loader::view()
872
	 * @used-by	CI_Loader::file()
873
	 * @param	array	$_ci_data	Data to load
874
	 * @return	object
875
	 */
876
	protected function _ci_load($_ci_data)
877
	{
878
		// Set the default data variables
879
		foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
880
		{
881
			$$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
882
		}
883
 
884
		$file_exists = FALSE;
885
 
886
		// Set the path to the requested file
887
		if (is_string($_ci_path) && $_ci_path !== '')
888
		{
889
			$_ci_x = explode('/', $_ci_path);
890
			$_ci_file = end($_ci_x);
891
		}
892
		else
893
		{
894
			$_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
895
			$_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
896
 
897
			foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
898
			{
899
				if (file_exists($_ci_view_file.$_ci_file))
900
				{
901
					$_ci_path = $_ci_view_file.$_ci_file;
902
					$file_exists = TRUE;
903
					break;
904
				}
905
 
906
				if ( ! $cascade)
907
				{
908
					break;
909
				}
910
			}
911
		}
912
 
913
		if ( ! $file_exists && ! file_exists($_ci_path))
914
		{
915
			show_error('Unable to load the requested file: '.$_ci_file);
916
		}
917
 
918
		// This allows anything loaded using $this->load (views, files, etc.)
919
		// to become accessible from within the Controller and Model functions.
920
		$_ci_CI =& get_instance();
921
		foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
922
		{
923
			if ( ! isset($this->$_ci_key))
924
			{
925
				$this->$_ci_key =& $_ci_CI->$_ci_key;
926
			}
927
		}
928
 
929
		/*
930
		 * Extract and cache variables
931
		 *
932
		 * You can either set variables using the dedicated $this->load->vars()
933
		 * function or via the second parameter of this function. We'll merge
934
		 * the two types and cache them so that views that are embedded within
935
		 * other views can have access to these variables.
936
		 */
2049 lars 937
		empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
68 lars 938
		extract($this->_ci_cached_vars);
939
 
940
		/*
941
		 * Buffer the output
942
		 *
943
		 * We buffer the output for two reasons:
944
		 * 1. Speed. You get a significant speed boost.
945
		 * 2. So that the final rendered template can be post-processed by
946
		 *	the output class. Why do we need post processing? For one thing,
947
		 *	in order to show the elapsed page load time. Unless we can
948
		 *	intercept the content right before it's sent to the browser and
949
		 *	then stop the timer it won't be accurate.
950
		 */
951
		ob_start();
952
 
953
		// If the PHP installation does not support short tags we'll
954
		// do a little string replacement, changing the short tags
955
		// to standard PHP echo statements.
956
		if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
957
		{
958
			echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
959
		}
960
		else
961
		{
962
			include($_ci_path); // include() vs include_once() allows for multiple views with the same name
963
		}
964
 
965
		log_message('info', 'File loaded: '.$_ci_path);
966
 
967
		// Return the file data if requested
968
		if ($_ci_return === TRUE)
969
		{
970
			$buffer = ob_get_contents();
971
			@ob_end_clean();
972
			return $buffer;
973
		}
974
 
975
		/*
976
		 * Flush the buffer... or buff the flusher?
977
		 *
978
		 * In order to permit views to be nested within
979
		 * other views, we need to flush the content back out whenever
980
		 * we are beyond the first level of output buffering so that
981
		 * it can be seen and included properly by the first included
982
		 * template and any subsequent ones. Oy!
983
		 */
984
		if (ob_get_level() > $this->_ci_ob_level + 1)
985
		{
986
			ob_end_flush();
987
		}
988
		else
989
		{
990
			$_ci_CI->output->append_output(ob_get_contents());
991
			@ob_end_clean();
992
		}
993
 
994
		return $this;
995
	}
996
 
997
	// --------------------------------------------------------------------
998
 
999
	/**
1000
	 * Internal CI Library Loader
1001
	 *
1002
	 * @used-by	CI_Loader::library()
1003
	 * @uses	CI_Loader::_ci_init_library()
1004
	 *
1005
	 * @param	string	$class		Class name to load
1006
	 * @param	mixed	$params		Optional parameters to pass to the class constructor
1007
	 * @param	string	$object_name	Optional object name to assign to
1008
	 * @return	void
1009
	 */
1010
	protected function _ci_load_library($class, $params = NULL, $object_name = NULL)
1011
	{
1012
		// Get the class name, and while we're at it trim any slashes.
1013
		// The directory path can be included as part of the class name,
1014
		// but we don't want a leading slash
1015
		$class = str_replace('.php', '', trim($class, '/'));
1016
 
1017
		// Was the path included with the class name?
1018
		// We look for a slash to determine this
1019
		if (($last_slash = strrpos($class, '/')) !== FALSE)
1020
		{
1021
			// Extract the path
1022
			$subdir = substr($class, 0, ++$last_slash);
1023
 
1024
			// Get the filename from the path
1025
			$class = substr($class, $last_slash);
1026
		}
1027
		else
1028
		{
1029
			$subdir = '';
1030
		}
1031
 
1032
		$class = ucfirst($class);
1033
 
1034
		// Is this a stock library? There are a few special conditions if so ...
1035
		if (file_exists(BASEPATH.'libraries/'.$subdir.$class.'.php'))
1036
		{
1037
			return $this->_ci_load_stock_library($class, $subdir, $params, $object_name);
1038
		}
1039
 
2107 lars 1040
		// Safety: Was the class already loaded by a previous call?
1041
		if (class_exists($class, FALSE))
1042
		{
1043
			$property = $object_name;
1044
			if (empty($property))
1045
			{
1046
				$property = strtolower($class);
1047
				isset($this->_ci_varmap[$property]) && $property = $this->_ci_varmap[$property];
1048
			}
1049
 
1050
			$CI =& get_instance();
1051
			if (isset($CI->$property))
1052
			{
1053
				log_message('debug', $class.' class already loaded. Second attempt ignored.');
1054
				return;
1055
			}
1056
 
1057
			return $this->_ci_init_library($class, '', $params, $object_name);
1058
		}
1059
 
68 lars 1060
		// Let's search for the requested library file and load it.
1061
		foreach ($this->_ci_library_paths as $path)
1062
		{
1063
			// BASEPATH has already been checked for
1064
			if ($path === BASEPATH)
1065
			{
1066
				continue;
1067
			}
1068
 
1069
			$filepath = $path.'libraries/'.$subdir.$class.'.php';
1070
			// Does the file exist? No? Bummer...
2107 lars 1071
			if ( ! file_exists($filepath))
68 lars 1072
			{
1073
				continue;
1074
			}
1075
 
1076
			include_once($filepath);
1077
			return $this->_ci_init_library($class, '', $params, $object_name);
1078
		}
1079
 
1080
		// One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
1081
		if ($subdir === '')
1082
		{
1083
			return $this->_ci_load_library($class.'/'.$class, $params, $object_name);
1084
		}
1085
 
1086
		// If we got this far we were unable to find the requested class.
1087
		log_message('error', 'Unable to load the requested class: '.$class);
1088
		show_error('Unable to load the requested class: '.$class);
1089
	}
1090
 
1091
	// --------------------------------------------------------------------
1092
 
1093
	/**
1094
	 * Internal CI Stock Library Loader
1095
	 *
1096
	 * @used-by	CI_Loader::_ci_load_library()
1097
	 * @uses	CI_Loader::_ci_init_library()
1098
	 *
1099
	 * @param	string	$library_name	Library name to load
1100
	 * @param	string	$file_path	Path to the library filename, relative to libraries/
1101
	 * @param	mixed	$params		Optional parameters to pass to the class constructor
1102
	 * @param	string	$object_name	Optional object name to assign to
1103
	 * @return	void
1104
	 */
1105
	protected function _ci_load_stock_library($library_name, $file_path, $params, $object_name)
1106
	{
1107
		$prefix = 'CI_';
1108
 
1109
		if (class_exists($prefix.$library_name, FALSE))
1110
		{
1111
			if (class_exists(config_item('subclass_prefix').$library_name, FALSE))
1112
			{
1113
				$prefix = config_item('subclass_prefix');
1114
			}
1115
 
2107 lars 1116
			$property = $object_name;
1117
			if (empty($property))
68 lars 1118
			{
2107 lars 1119
				$property = strtolower($library_name);
1120
				isset($this->_ci_varmap[$property]) && $property = $this->_ci_varmap[$property];
68 lars 1121
			}
1122
 
2107 lars 1123
			$CI =& get_instance();
1124
			if ( ! isset($CI->$property))
1125
			{
1126
				return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1127
			}
1128
 
68 lars 1129
			log_message('debug', $library_name.' class already loaded. Second attempt ignored.');
1130
			return;
1131
		}
1132
 
1133
		$paths = $this->_ci_library_paths;
1134
		array_pop($paths); // BASEPATH
1135
		array_pop($paths); // APPPATH (needs to be the first path checked)
1136
		array_unshift($paths, APPPATH);
1137
 
1138
		foreach ($paths as $path)
1139
		{
1140
			if (file_exists($path = $path.'libraries/'.$file_path.$library_name.'.php'))
1141
			{
1142
				// Override
1143
				include_once($path);
1144
				if (class_exists($prefix.$library_name, FALSE))
1145
				{
1146
					return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1147
				}
2107 lars 1148
 
1149
				log_message('debug', $path.' exists, but does not declare '.$prefix.$library_name);
68 lars 1150
			}
1151
		}
1152
 
1153
		include_once(BASEPATH.'libraries/'.$file_path.$library_name.'.php');
1154
 
1155
		// Check for extensions
1156
		$subclass = config_item('subclass_prefix').$library_name;
1157
		foreach ($paths as $path)
1158
		{
1159
			if (file_exists($path = $path.'libraries/'.$file_path.$subclass.'.php'))
1160
			{
1161
				include_once($path);
1162
				if (class_exists($subclass, FALSE))
1163
				{
1164
					$prefix = config_item('subclass_prefix');
1165
					break;
1166
				}
2107 lars 1167
 
1168
				log_message('debug', $path.' exists, but does not declare '.$subclass);
68 lars 1169
			}
1170
		}
1171
 
1172
		return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1173
	}
1174
 
1175
	// --------------------------------------------------------------------
1176
 
1177
	/**
1178
	 * Internal CI Library Instantiator
1179
	 *
1180
	 * @used-by	CI_Loader::_ci_load_stock_library()
1181
	 * @used-by	CI_Loader::_ci_load_library()
1182
	 *
1183
	 * @param	string		$class		Class name
1184
	 * @param	string		$prefix		Class name prefix
1185
	 * @param	array|null|bool	$config		Optional configuration to pass to the class constructor:
1186
	 *						FALSE to skip;
1187
	 *						NULL to search in config paths;
1188
	 *						array containing configuration data
1189
	 * @param	string		$object_name	Optional object name to assign to
1190
	 * @return	void
1191
	 */
1192
	protected function _ci_init_library($class, $prefix, $config = FALSE, $object_name = NULL)
1193
	{
1194
		// Is there an associated config file for this class? Note: these should always be lowercase
1195
		if ($config === NULL)
1196
		{
1197
			// Fetch the config paths containing any package paths
1198
			$config_component = $this->_ci_get_component('config');
1199
 
1200
			if (is_array($config_component->_config_paths))
1201
			{
1202
				$found = FALSE;
1203
				foreach ($config_component->_config_paths as $path)
1204
				{
1205
					// We test for both uppercase and lowercase, for servers that
1206
					// are case-sensitive with regard to file names. Load global first,
1207
					// override with environment next
1208
					if (file_exists($path.'config/'.strtolower($class).'.php'))
1209
					{
1210
						include($path.'config/'.strtolower($class).'.php');
1211
						$found = TRUE;
1212
					}
1213
					elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
1214
					{
1215
						include($path.'config/'.ucfirst(strtolower($class)).'.php');
1216
						$found = TRUE;
1217
					}
1218
 
1219
					if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
1220
					{
1221
						include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
1222
						$found = TRUE;
1223
					}
1224
					elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
1225
					{
1226
						include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
1227
						$found = TRUE;
1228
					}
1229
 
1230
					// Break on the first found configuration, thus package
1231
					// files are not overridden by default paths
1232
					if ($found === TRUE)
1233
					{
1234
						break;
1235
					}
1236
				}
1237
			}
1238
		}
1239
 
1240
		$class_name = $prefix.$class;
1241
 
1242
		// Is the class name valid?
1243
		if ( ! class_exists($class_name, FALSE))
1244
		{
1245
			log_message('error', 'Non-existent class: '.$class_name);
1246
			show_error('Non-existent class: '.$class_name);
1247
		}
1248
 
1249
		// Set the variable name we will assign the class to
1250
		// Was a custom class name supplied? If so we'll use it
1251
		if (empty($object_name))
1252
		{
1253
			$object_name = strtolower($class);
1254
			if (isset($this->_ci_varmap[$object_name]))
1255
			{
1256
				$object_name = $this->_ci_varmap[$object_name];
1257
			}
1258
		}
1259
 
1260
		// Don't overwrite existing properties
1261
		$CI =& get_instance();
1262
		if (isset($CI->$object_name))
1263
		{
1264
			if ($CI->$object_name instanceof $class_name)
1265
			{
1266
				log_message('debug', $class_name." has already been instantiated as '".$object_name."'. Second attempt aborted.");
1267
				return;
1268
			}
1269
 
1270
			show_error("Resource '".$object_name."' already exists and is not a ".$class_name." instance.");
1271
		}
1272
 
1273
		// Save the class name and object name
1274
		$this->_ci_classes[$object_name] = $class;
1275
 
1276
		// Instantiate the class
1277
		$CI->$object_name = isset($config)
1278
			? new $class_name($config)
1279
			: new $class_name();
1280
	}
1281
 
1282
	// --------------------------------------------------------------------
1283
 
1284
	/**
1285
	 * CI Autoloader
1286
	 *
1287
	 * Loads component listed in the config/autoload.php file.
1288
	 *
1289
	 * @used-by	CI_Loader::initialize()
1290
	 * @return	void
1291
	 */
1292
	protected function _ci_autoloader()
1293
	{
1294
		if (file_exists(APPPATH.'config/autoload.php'))
1295
		{
1296
			include(APPPATH.'config/autoload.php');
1297
		}
1298
 
1299
		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
1300
		{
1301
			include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1302
		}
1303
 
1304
		if ( ! isset($autoload))
1305
		{
1306
			return;
1307
		}
1308
 
1309
		// Autoload packages
1310
		if (isset($autoload['packages']))
1311
		{
1312
			foreach ($autoload['packages'] as $package_path)
1313
			{
1314
				$this->add_package_path($package_path);
1315
			}
1316
		}
1317
 
1318
		// Load any custom config file
1319
		if (count($autoload['config']) > 0)
1320
		{
1321
			foreach ($autoload['config'] as $val)
1322
			{
1323
				$this->config($val);
1324
			}
1325
		}
1326
 
1327
		// Autoload helpers and languages
1328
		foreach (array('helper', 'language') as $type)
1329
		{
1330
			if (isset($autoload[$type]) && count($autoload[$type]) > 0)
1331
			{
1332
				$this->$type($autoload[$type]);
1333
			}
1334
		}
1335
 
1336
		// Autoload drivers
1337
		if (isset($autoload['drivers']))
1338
		{
1339
			$this->driver($autoload['drivers']);
1340
		}
1341
 
1342
		// Load libraries
1343
		if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
1344
		{
1345
			// Load the database driver.
1346
			if (in_array('database', $autoload['libraries']))
1347
			{
1348
				$this->database();
1349
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1350
			}
1351
 
1352
			// Load all other libraries
1353
			$this->library($autoload['libraries']);
1354
		}
1355
 
1356
		// Autoload models
1357
		if (isset($autoload['model']))
1358
		{
1359
			$this->model($autoload['model']);
1360
		}
1361
	}
1362
 
1363
	// --------------------------------------------------------------------
1364
 
1365
	/**
2049 lars 1366
	 * Prepare variables for _ci_vars, to be later extract()-ed inside views
68 lars 1367
	 *
2049 lars 1368
	 * Converts objects to associative arrays and filters-out internal
2107 lars 1369
	 * variable names (i.e. keys prefixed with '_ci_').
68 lars 1370
	 *
2049 lars 1371
	 * @param	mixed	$vars
68 lars 1372
	 * @return	array
1373
	 */
2049 lars 1374
	protected function _ci_prepare_view_vars($vars)
68 lars 1375
	{
2049 lars 1376
		if ( ! is_array($vars))
1377
		{
1378
			$vars = is_object($vars)
2107 lars 1379
				? get_object_vars($vars)
2049 lars 1380
				: array();
1381
		}
1382
 
1383
		foreach (array_keys($vars) as $key)
1384
		{
1385
			if (strncmp($key, '_ci_', 4) === 0)
1386
			{
1387
				unset($vars[$key]);
1388
			}
1389
		}
1390
 
1391
		return $vars;
68 lars 1392
	}
1393
 
1394
	// --------------------------------------------------------------------
1395
 
1396
	/**
1397
	 * CI Component getter
1398
	 *
1399
	 * Get a reference to a specific library or model.
1400
	 *
1401
	 * @param 	string	$component	Component name
1402
	 * @return	bool
1403
	 */
1404
	protected function &_ci_get_component($component)
1405
	{
1406
		$CI =& get_instance();
1407
		return $CI->$component;
1408
	}
1409
}