Subversion-Projekte lars-tiefland.ci

Revision

Revision 2049 | Zur aktuellen Revision | Details | 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
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
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/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
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
	 *
185
	 * @param	string	$library	Library name
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
	 *
229
	 * @param	string	$model		Model name
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;
353
		$CI->$name = new $model();
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
	{
489
		return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
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
	{
522
		if (is_string($vars))
523
		{
524
			$vars = array($vars => $val);
525
		}
526
 
527
		$vars = $this->_ci_object_to_array($vars);
528
 
529
		if (is_array($vars) && count($vars) > 0)
530
		{
531
			foreach ($vars as $key => $val)
532
			{
533
				$this->_ci_cached_vars[$key] = $val;
534
			}
535
		}
536
 
537
		return $this;
538
	}
539
 
540
	// --------------------------------------------------------------------
541
 
542
	/**
543
	 * Clear Cached Variables
544
	 *
545
	 * Clears the cached variables.
546
	 *
547
	 * @return	CI_Loader
548
	 */
549
	public function clear_vars()
550
	{
551
		$this->_ci_cached_vars = array();
552
		return $this;
553
	}
554
 
555
	// --------------------------------------------------------------------
556
 
557
	/**
558
	 * Get Variable
559
	 *
560
	 * Check if a variable is set and retrieve it.
561
	 *
562
	 * @param	string	$key	Variable name
563
	 * @return	mixed	The variable or NULL if not found
564
	 */
565
	public function get_var($key)
566
	{
567
		return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
568
	}
569
 
570
	// --------------------------------------------------------------------
571
 
572
	/**
573
	 * Get Variables
574
	 *
575
	 * Retrieves all loaded variables.
576
	 *
577
	 * @return	array
578
	 */
579
	public function get_vars()
580
	{
581
		return $this->_ci_cached_vars;
582
	}
583
 
584
	// --------------------------------------------------------------------
585
 
586
	/**
587
	 * Helper Loader
588
	 *
589
	 * @param	string|string[]	$helpers	Helper name(s)
590
	 * @return	object
591
	 */
592
	public function helper($helpers = array())
593
	{
594
		foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
595
		{
596
			if (isset($this->_ci_helpers[$helper]))
597
			{
598
				continue;
599
			}
600
 
601
			// Is this a helper extension request?
602
			$ext_helper = config_item('subclass_prefix').$helper;
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
		 */
937
		if (is_array($_ci_vars))
938
		{
939
			foreach (array_keys($_ci_vars) as $key)
940
			{
941
				if (strncmp($key, '_ci_', 4) === 0)
942
				{
943
					unset($_ci_vars[$key]);
944
				}
945
			}
946
 
947
			$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
948
		}
949
		extract($this->_ci_cached_vars);
950
 
951
		/*
952
		 * Buffer the output
953
		 *
954
		 * We buffer the output for two reasons:
955
		 * 1. Speed. You get a significant speed boost.
956
		 * 2. So that the final rendered template can be post-processed by
957
		 *	the output class. Why do we need post processing? For one thing,
958
		 *	in order to show the elapsed page load time. Unless we can
959
		 *	intercept the content right before it's sent to the browser and
960
		 *	then stop the timer it won't be accurate.
961
		 */
962
		ob_start();
963
 
964
		// If the PHP installation does not support short tags we'll
965
		// do a little string replacement, changing the short tags
966
		// to standard PHP echo statements.
967
		if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
968
		{
969
			echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
970
		}
971
		else
972
		{
973
			include($_ci_path); // include() vs include_once() allows for multiple views with the same name
974
		}
975
 
976
		log_message('info', 'File loaded: '.$_ci_path);
977
 
978
		// Return the file data if requested
979
		if ($_ci_return === TRUE)
980
		{
981
			$buffer = ob_get_contents();
982
			@ob_end_clean();
983
			return $buffer;
984
		}
985
 
986
		/*
987
		 * Flush the buffer... or buff the flusher?
988
		 *
989
		 * In order to permit views to be nested within
990
		 * other views, we need to flush the content back out whenever
991
		 * we are beyond the first level of output buffering so that
992
		 * it can be seen and included properly by the first included
993
		 * template and any subsequent ones. Oy!
994
		 */
995
		if (ob_get_level() > $this->_ci_ob_level + 1)
996
		{
997
			ob_end_flush();
998
		}
999
		else
1000
		{
1001
			$_ci_CI->output->append_output(ob_get_contents());
1002
			@ob_end_clean();
1003
		}
1004
 
1005
		return $this;
1006
	}
1007
 
1008
	// --------------------------------------------------------------------
1009
 
1010
	/**
1011
	 * Internal CI Library Loader
1012
	 *
1013
	 * @used-by	CI_Loader::library()
1014
	 * @uses	CI_Loader::_ci_init_library()
1015
	 *
1016
	 * @param	string	$class		Class name to load
1017
	 * @param	mixed	$params		Optional parameters to pass to the class constructor
1018
	 * @param	string	$object_name	Optional object name to assign to
1019
	 * @return	void
1020
	 */
1021
	protected function _ci_load_library($class, $params = NULL, $object_name = NULL)
1022
	{
1023
		// Get the class name, and while we're at it trim any slashes.
1024
		// The directory path can be included as part of the class name,
1025
		// but we don't want a leading slash
1026
		$class = str_replace('.php', '', trim($class, '/'));
1027
 
1028
		// Was the path included with the class name?
1029
		// We look for a slash to determine this
1030
		if (($last_slash = strrpos($class, '/')) !== FALSE)
1031
		{
1032
			// Extract the path
1033
			$subdir = substr($class, 0, ++$last_slash);
1034
 
1035
			// Get the filename from the path
1036
			$class = substr($class, $last_slash);
1037
		}
1038
		else
1039
		{
1040
			$subdir = '';
1041
		}
1042
 
1043
		$class = ucfirst($class);
1044
 
1045
		// Is this a stock library? There are a few special conditions if so ...
1046
		if (file_exists(BASEPATH.'libraries/'.$subdir.$class.'.php'))
1047
		{
1048
			return $this->_ci_load_stock_library($class, $subdir, $params, $object_name);
1049
		}
1050
 
1051
		// Let's search for the requested library file and load it.
1052
		foreach ($this->_ci_library_paths as $path)
1053
		{
1054
			// BASEPATH has already been checked for
1055
			if ($path === BASEPATH)
1056
			{
1057
				continue;
1058
			}
1059
 
1060
			$filepath = $path.'libraries/'.$subdir.$class.'.php';
1061
 
1062
			// Safety: Was the class already loaded by a previous call?
1063
			if (class_exists($class, FALSE))
1064
			{
1065
				// Before we deem this to be a duplicate request, let's see
1066
				// if a custom object name is being supplied. If so, we'll
1067
				// return a new instance of the object
1068
				if ($object_name !== NULL)
1069
				{
1070
					$CI =& get_instance();
1071
					if ( ! isset($CI->$object_name))
1072
					{
1073
						return $this->_ci_init_library($class, '', $params, $object_name);
1074
					}
1075
				}
1076
 
1077
				log_message('debug', $class.' class already loaded. Second attempt ignored.');
1078
				return;
1079
			}
1080
			// Does the file exist? No? Bummer...
1081
			elseif ( ! file_exists($filepath))
1082
			{
1083
				continue;
1084
			}
1085
 
1086
			include_once($filepath);
1087
			return $this->_ci_init_library($class, '', $params, $object_name);
1088
		}
1089
 
1090
		// One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
1091
		if ($subdir === '')
1092
		{
1093
			return $this->_ci_load_library($class.'/'.$class, $params, $object_name);
1094
		}
1095
 
1096
		// If we got this far we were unable to find the requested class.
1097
		log_message('error', 'Unable to load the requested class: '.$class);
1098
		show_error('Unable to load the requested class: '.$class);
1099
	}
1100
 
1101
	// --------------------------------------------------------------------
1102
 
1103
	/**
1104
	 * Internal CI Stock Library Loader
1105
	 *
1106
	 * @used-by	CI_Loader::_ci_load_library()
1107
	 * @uses	CI_Loader::_ci_init_library()
1108
	 *
1109
	 * @param	string	$library_name	Library name to load
1110
	 * @param	string	$file_path	Path to the library filename, relative to libraries/
1111
	 * @param	mixed	$params		Optional parameters to pass to the class constructor
1112
	 * @param	string	$object_name	Optional object name to assign to
1113
	 * @return	void
1114
	 */
1115
	protected function _ci_load_stock_library($library_name, $file_path, $params, $object_name)
1116
	{
1117
		$prefix = 'CI_';
1118
 
1119
		if (class_exists($prefix.$library_name, FALSE))
1120
		{
1121
			if (class_exists(config_item('subclass_prefix').$library_name, FALSE))
1122
			{
1123
				$prefix = config_item('subclass_prefix');
1124
			}
1125
 
1126
			// Before we deem this to be a duplicate request, let's see
1127
			// if a custom object name is being supplied. If so, we'll
1128
			// return a new instance of the object
1129
			if ($object_name !== NULL)
1130
			{
1131
				$CI =& get_instance();
1132
				if ( ! isset($CI->$object_name))
1133
				{
1134
					return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1135
				}
1136
			}
1137
 
1138
			log_message('debug', $library_name.' class already loaded. Second attempt ignored.');
1139
			return;
1140
		}
1141
 
1142
		$paths = $this->_ci_library_paths;
1143
		array_pop($paths); // BASEPATH
1144
		array_pop($paths); // APPPATH (needs to be the first path checked)
1145
		array_unshift($paths, APPPATH);
1146
 
1147
		foreach ($paths as $path)
1148
		{
1149
			if (file_exists($path = $path.'libraries/'.$file_path.$library_name.'.php'))
1150
			{
1151
				// Override
1152
				include_once($path);
1153
				if (class_exists($prefix.$library_name, FALSE))
1154
				{
1155
					return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1156
				}
1157
				else
1158
				{
1159
					log_message('debug', $path.' exists, but does not declare '.$prefix.$library_name);
1160
				}
1161
			}
1162
		}
1163
 
1164
		include_once(BASEPATH.'libraries/'.$file_path.$library_name.'.php');
1165
 
1166
		// Check for extensions
1167
		$subclass = config_item('subclass_prefix').$library_name;
1168
		foreach ($paths as $path)
1169
		{
1170
			if (file_exists($path = $path.'libraries/'.$file_path.$subclass.'.php'))
1171
			{
1172
				include_once($path);
1173
				if (class_exists($subclass, FALSE))
1174
				{
1175
					$prefix = config_item('subclass_prefix');
1176
					break;
1177
				}
1178
				else
1179
				{
1180
					log_message('debug', $path.' exists, but does not declare '.$subclass);
1181
				}
1182
			}
1183
		}
1184
 
1185
		return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1186
	}
1187
 
1188
	// --------------------------------------------------------------------
1189
 
1190
	/**
1191
	 * Internal CI Library Instantiator
1192
	 *
1193
	 * @used-by	CI_Loader::_ci_load_stock_library()
1194
	 * @used-by	CI_Loader::_ci_load_library()
1195
	 *
1196
	 * @param	string		$class		Class name
1197
	 * @param	string		$prefix		Class name prefix
1198
	 * @param	array|null|bool	$config		Optional configuration to pass to the class constructor:
1199
	 *						FALSE to skip;
1200
	 *						NULL to search in config paths;
1201
	 *						array containing configuration data
1202
	 * @param	string		$object_name	Optional object name to assign to
1203
	 * @return	void
1204
	 */
1205
	protected function _ci_init_library($class, $prefix, $config = FALSE, $object_name = NULL)
1206
	{
1207
		// Is there an associated config file for this class? Note: these should always be lowercase
1208
		if ($config === NULL)
1209
		{
1210
			// Fetch the config paths containing any package paths
1211
			$config_component = $this->_ci_get_component('config');
1212
 
1213
			if (is_array($config_component->_config_paths))
1214
			{
1215
				$found = FALSE;
1216
				foreach ($config_component->_config_paths as $path)
1217
				{
1218
					// We test for both uppercase and lowercase, for servers that
1219
					// are case-sensitive with regard to file names. Load global first,
1220
					// override with environment next
1221
					if (file_exists($path.'config/'.strtolower($class).'.php'))
1222
					{
1223
						include($path.'config/'.strtolower($class).'.php');
1224
						$found = TRUE;
1225
					}
1226
					elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
1227
					{
1228
						include($path.'config/'.ucfirst(strtolower($class)).'.php');
1229
						$found = TRUE;
1230
					}
1231
 
1232
					if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
1233
					{
1234
						include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
1235
						$found = TRUE;
1236
					}
1237
					elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
1238
					{
1239
						include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
1240
						$found = TRUE;
1241
					}
1242
 
1243
					// Break on the first found configuration, thus package
1244
					// files are not overridden by default paths
1245
					if ($found === TRUE)
1246
					{
1247
						break;
1248
					}
1249
				}
1250
			}
1251
		}
1252
 
1253
		$class_name = $prefix.$class;
1254
 
1255
		// Is the class name valid?
1256
		if ( ! class_exists($class_name, FALSE))
1257
		{
1258
			log_message('error', 'Non-existent class: '.$class_name);
1259
			show_error('Non-existent class: '.$class_name);
1260
		}
1261
 
1262
		// Set the variable name we will assign the class to
1263
		// Was a custom class name supplied? If so we'll use it
1264
		if (empty($object_name))
1265
		{
1266
			$object_name = strtolower($class);
1267
			if (isset($this->_ci_varmap[$object_name]))
1268
			{
1269
				$object_name = $this->_ci_varmap[$object_name];
1270
			}
1271
		}
1272
 
1273
		// Don't overwrite existing properties
1274
		$CI =& get_instance();
1275
		if (isset($CI->$object_name))
1276
		{
1277
			if ($CI->$object_name instanceof $class_name)
1278
			{
1279
				log_message('debug', $class_name." has already been instantiated as '".$object_name."'. Second attempt aborted.");
1280
				return;
1281
			}
1282
 
1283
			show_error("Resource '".$object_name."' already exists and is not a ".$class_name." instance.");
1284
		}
1285
 
1286
		// Save the class name and object name
1287
		$this->_ci_classes[$object_name] = $class;
1288
 
1289
		// Instantiate the class
1290
		$CI->$object_name = isset($config)
1291
			? new $class_name($config)
1292
			: new $class_name();
1293
	}
1294
 
1295
	// --------------------------------------------------------------------
1296
 
1297
	/**
1298
	 * CI Autoloader
1299
	 *
1300
	 * Loads component listed in the config/autoload.php file.
1301
	 *
1302
	 * @used-by	CI_Loader::initialize()
1303
	 * @return	void
1304
	 */
1305
	protected function _ci_autoloader()
1306
	{
1307
		if (file_exists(APPPATH.'config/autoload.php'))
1308
		{
1309
			include(APPPATH.'config/autoload.php');
1310
		}
1311
 
1312
		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
1313
		{
1314
			include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1315
		}
1316
 
1317
		if ( ! isset($autoload))
1318
		{
1319
			return;
1320
		}
1321
 
1322
		// Autoload packages
1323
		if (isset($autoload['packages']))
1324
		{
1325
			foreach ($autoload['packages'] as $package_path)
1326
			{
1327
				$this->add_package_path($package_path);
1328
			}
1329
		}
1330
 
1331
		// Load any custom config file
1332
		if (count($autoload['config']) > 0)
1333
		{
1334
			foreach ($autoload['config'] as $val)
1335
			{
1336
				$this->config($val);
1337
			}
1338
		}
1339
 
1340
		// Autoload helpers and languages
1341
		foreach (array('helper', 'language') as $type)
1342
		{
1343
			if (isset($autoload[$type]) && count($autoload[$type]) > 0)
1344
			{
1345
				$this->$type($autoload[$type]);
1346
			}
1347
		}
1348
 
1349
		// Autoload drivers
1350
		if (isset($autoload['drivers']))
1351
		{
1352
			$this->driver($autoload['drivers']);
1353
		}
1354
 
1355
		// Load libraries
1356
		if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
1357
		{
1358
			// Load the database driver.
1359
			if (in_array('database', $autoload['libraries']))
1360
			{
1361
				$this->database();
1362
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1363
			}
1364
 
1365
			// Load all other libraries
1366
			$this->library($autoload['libraries']);
1367
		}
1368
 
1369
		// Autoload models
1370
		if (isset($autoload['model']))
1371
		{
1372
			$this->model($autoload['model']);
1373
		}
1374
	}
1375
 
1376
	// --------------------------------------------------------------------
1377
 
1378
	/**
1379
	 * CI Object to Array translator
1380
	 *
1381
	 * Takes an object as input and converts the class variables to
1382
	 * an associative array with key/value pairs.
1383
	 *
1384
	 * @param	object	$object	Object data to translate
1385
	 * @return	array
1386
	 */
1387
	protected function _ci_object_to_array($object)
1388
	{
1389
		return is_object($object) ? get_object_vars($object) : $object;
1390
	}
1391
 
1392
	// --------------------------------------------------------------------
1393
 
1394
	/**
1395
	 * CI Component getter
1396
	 *
1397
	 * Get a reference to a specific library or model.
1398
	 *
1399
	 * @param 	string	$component	Component name
1400
	 * @return	bool
1401
	 */
1402
	protected function &_ci_get_component($component)
1403
	{
1404
		$CI =& get_instance();
1405
		return $CI->$component;
1406
	}
1407
 
1408
	// --------------------------------------------------------------------
1409
 
1410
	/**
1411
	 * Prep filename
1412
	 *
1413
	 * This function prepares filenames of various items to
1414
	 * make their loading more reliable.
1415
	 *
1416
	 * @param	string|string[]	$filename	Filename(s)
1417
	 * @param 	string		$extension	Filename extension
1418
	 * @return	array
1419
	 */
1420
	protected function _ci_prep_filename($filename, $extension)
1421
	{
1422
		if ( ! is_array($filename))
1423
		{
1424
			return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
1425
		}
1426
		else
1427
		{
1428
			foreach ($filename as $key => $val)
1429
			{
1430
				$filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
1431
			}
1432
 
1433
			return $filename;
1434
		}
1435
	}
1436
 
1437
}