forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Loader.php
executable file
·632 lines (568 loc) · 14.5 KB
/
Loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
/**
* Loader Class
*
* Loads resources (libraries, controllers, views, etc.) into CodeIgniter.
* The base class, CI_Core_share, is defined in CodeIgniter.php and allows
* Loader access to protected loading methods in CodeIgniter.
*
* @package CodeIgniter
* @subpackage Libraries
* @author ExpressionEngine Dev Team
* @category Loader
* @link http://codeigniter.com/user_guide/libraries/loader.html
*/
class CI_Loader extends CI_Core_share {
/**
* Reference to CodeIgniter object
*
* @var object
* @access protected
*/
protected $CI = NULL;
/**
* List of cached variables
*
* @var array
* @access protected
*/
protected $cached_vars = array();
/**
* List of class name mappings
*
* @var array
* @access protected
*/
protected $varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
/**
* Constructor
*
* Sets default package paths, gets the initial output buffering level,
* and autoloads additional paths and config files
*
* @param object parent reference
*/
public function __construct(CodeIgniter $CI)
{
// Attach parent reference
$this->CI =& $CI;
$CI->log_message('debug', 'Loader Class Initialized');
}
/**
* Library loader
*
* This function lets users load and instantiate library classes.
*
* @param mixed the name of the class or an array of names
* @param array optional parameters
* @param string an optional object name
* @return void
*/
public function library($class, array $params = NULL, $obj_name = NULL)
{
// Check for missing class
if (empty($class))
{
return FALSE;
}
// Delegate multiples
if (is_array($class))
{
foreach ($class as $class)
{
$this->library($class, $params);
}
return;
}
// Parse out the filename and path.
$subdir = $this->_get_path($class);
// Set object name if not provided
if (is_null($obj_name))
{
$obj_name = isset($this->varmap[$class]) ? $this->varmap[$class] : strtolower($class);
}
// Load object in core
$this->_call_core($this->CI, '_load', 'library', $class, $obj_name, $params, $subdir);
}
/**
* Driver loader
*
* Loads a driver library
*
* @param string the name of the class
* @param array the optional parameters
* @param string an optional object name
* @return void
*/
public function driver($class, array $params = NULL, $obj_name = NULL)
{
if (!class_exists('CI_Driver_Library'))
{
// we aren't instantiating an object here, that'll be done by the Library itself
require BASEPATH.'libraries/Driver.php';
}
// We can save the loader some time since Drivers will *always* be in a subfolder,
// and typically identically named to the library
if (!strpos($class, '/'))
{
$class = ucfirst($class).'/'.$class;
}
return $this->library($class, $params, $obj_name);
}
/**
* Helper loader
*
* This function loads the specified helper file.
*
* @param mixed the name of the helper or an array of names
* @return void
*/
public function helper($helper)
{
// Check for missing name
if (empty($helper))
{
return FALSE;
}
// Delegate multiples
if (is_array($helper))
{
foreach ($helper as $help)
{
$this->helper($help);
}
return;
}
// Parse out the filename and path and make sure _helper suffix is attached
$subdir = $this->_get_path($helper);
if (substr($helper, -7) != '_helper')
{
$helper .= '_helper';
}
// Load helper in core
$this->_call_core($this->CI, '_load', 'helper', $helper, FALSE, NULL, $subdir);
}
/**
* Helpers loader
*
* This is simply an alias to the above function in case the
* user has written the plural form of this function.
*
* @param array
* @return void
*/
public function helpers($helpers = array())
{
$this->helper($helpers);
}
/**
* Controller Loader
*
* This function lets users load and instantiate (sub)controllers.
* It accepts the controller route as a string, or an array of
* segments already parsed by CI_Route::validate_route(), and automatically
* calls the specified method (or 'index'), unless $call is FALSE.
*
* @access public
* @param string the URI route
* @param string object name for the controller
* @param boolean FALSE to skip calling controller method
* @return boolean TRUE on success, otherwise FALSE
*/
public function controller($route, $obj_name = '', $call = TRUE)
{
// Check for missing route
if (empty($route))
{
return FALSE;
}
// Get instance and establish route stack
if (is_array($route))
{
// Assume segments have been pre-parsed by CI_Router::validate_route() - make sure there's 4
if (count($route) < 4)
{
return FALSE;
}
}
else
{
// Call validate_route() to break URI into segments
$route = $this->CI->router->validate_route(explode('/', $route));
if ($route === FALSE)
{
return FALSE;
}
}
// Extract segment parts
$path = array_shift($route);
$subdir = array_shift($route);
$class = array_shift($route);
$method = array_shift($route);
// Load object in core
$this->_call_core($this->CI, '_load', 'controller', $class, $obj_name, NULL, $subdir, $path);
// Check if call is disabled
if ($call)
{
// Pass any remaining route segments as arguments to the call
return $this->_call_core($this->CI, '_call_controller', $class, $method, $route, $obj_name);
}
return TRUE;
}
/**
* Model Loader
*
* This function lets users load and instantiate models.
*
* @param string the name of the class
* @param string an optional object name
* @param mixed database connection name or TRUE to load default
* @return void
*/
public function model($class, $obj_name = '', $db_conn = FALSE)
{
// Check for missing class
if ($class == '')
{
return;
}
// Delegate multiples
if (is_array($class))
{
foreach ($class as $babe)
{
$this->model($babe);
}
return;
}
// Parse out the filename and path.
$subdir = $this->_get_path($class);
// Load database if needed
if ($db_conn !== FALSE && !class_exists('CI_DB'))
{
if ($db_conn === TRUE)
{
$db_conn = '';
}
$this->database($db_conn, FALSE, TRUE);
}
// Load object in core
$this->_call_core($this->CI, '_load', 'model', $class, $obj_name, NULL, $subdir);
}
/**
* View loader
*
* This function is used to load a "view" file.
* You can either set variables using the dedicated vars() function or
* via the second parameter of this function. We'll merge the two types and
* cache them so that views that are embedded within other views can have
* access to these variables.
*
* @param string view name
* @param array associative array of local variables for the view
* @param bool TRUE to return the output
* @return mixed output if $return is TRUE, otherwise void
*/
public function view($view, array $vars = array(), $return = FALSE)
{
// Append any vars to cache
if (!empty($vars))
{
$this->vars($vars);
}
// Run file in core context
return $this->_call_core($this->CI, '_run_file', $view, TRUE, $return, $this->cached_vars);
}
/**
* Language file loader
*
* @param mixed file name or array of names
* @param string language name
* @return void
*/
public function language($file, $lang = '')
{
// Force file to array
if (!is_array($file))
{
$file = array($file);
}
// Load each file via Lang
foreach ($file as $langfile)
{
$this->CI->lang->load($langfile, $lang);
}
}
/**
* Config file loader
*
* @param mixed file name or array of names
* @param boolean if configuration values should be loaded into their own section
* @param boolean TRUE if errors should just return FALSE, otherwise an error message is displayed
* @return void
*/
public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
{
// Force file to array
if (!is_array($file))
{
$file = array($file);
}
// Load each file via Config
foreach ($file as $config)
{
$this->CI->config->load($config, $use_sections, $fail_gracefully);
}
}
/**
* Database Loader
*
* @param string the DB credentials
* @param bool whether to return the DB object
* @param bool whether to enable active record (this allows us to override the config setting)
* @return object
*/
public function database($params = '', $return = FALSE, $active_record = NULL)
{
// Do we even need to load the database class?
if (class_exists('CI_DB') && $return == FALSE && $active_record == NULL && isset($this->CI->db) &&
is_object($this->CI->db))
{
return FALSE;
}
require_once(BASEPATH.'database/DB.php');
if ($return === TRUE)
{
return DB($params, $active_record);
}
// Initialize the db variable. Needed to prevent
// reference errors with some configurations
$this->CI->db = '';
// Load the DB class
$this->CI->db =& DB($params, $active_record);
}
/**
* Load the Utilities Class
*
* @return void
*/
public function dbutil()
{
if (!class_exists('CI_DB'))
{
$this->database();
}
// for backwards compatibility, load dbforge so we can extend dbutils off it
// this use is deprecated and strongly discouraged
$this->dbforge();
$driver = $this->CI->db->dbdriver;
require_once(BASEPATH.'database/DB_utility.php');
require_once(BASEPATH.'database/drivers/'.$driver.'/'.$driver.'_utility.php');
$class = 'CI_DB_'.$driver.'_utility';
$this->CI->dbutil = new $class();
}
/**
* Load the Database Forge Class
*
* @return void
*/
public function dbforge()
{
if (!class_exists('CI_DB'))
{
$this->database();
}
$driver = $this->CI->db->dbdriver;
require_once(BASEPATH.'database/DB_forge.php');
require_once(BASEPATH.'database/drivers/'.$driver.'/'.$driver.'_forge.php');
$class = 'CI_DB_'.$driver.'_forge';
$this->CI->dbforge = new $class();
}
/**
* User file loader
*
* This is a generic file loader
*
* @param string file path
* @param bool TRUE to return output
* @return mixed output if $return is TRUE, otherwise void
*/
public function file($path, $return = FALSE)
{
// Run file in core context
return $this->_call_core($this->CI, '_run_file', $path, FALSE, $return);
}
/**
* Set Variables
*
* Once variables are set they become available within
* the controller class and its "view" files.
*
* @param mixed variable name or array of vars
* @param mixed variable value
* @return void
*/
public function vars($vars = array(), $val = NULL)
{
// Handle non-array arguments
if ($val != NULL && is_string($vars))
{
$vars = array($vars => $val);
}
else if (is_object($vars))
{
$vars = get_object_vars($vars);
}
// Set values into cached vars
if (is_array($vars) && count($vars) > 0)
{
foreach ($vars as $key => $val)
{
$this->cached_vars[$key] = $val;
}
}
}
/**
* Get Variable
*
* Check if a variable is set and retrieve it.
*
* @param string var key
* @return mixed var value
*/
public function get_var($key)
{
// Return cached variable or NULL
return isset($this->cached_vars[$key]) ? $this->cached_vars[$key] : NULL;
}
/**
* Add Package Path
*
* Adds a package path to the list
*
* @param string path
* @param boolean view cascade flag
* @param boolean add to config path flag
* @return void
*/
public function add_package_path($path, $view_cascade = TRUE, $add_config_path = TRUE)
{
// Pass arguments to core method
$this->CI->add_package_path($path, $view_cascade, $add_config_path);
}
/**
* Remove Package Path
*
* Remove a package path from the list
* If no path is provided, the most recently added path is removed.
*
* @param string path
* @param boolean remove from config path flag
* @return void
*/
public function remove_package_path($path = '', $remove_config_path = TRUE)
{
// Pass arguments to core method
$this->CI->remove_packate_path($path, $remove_config_path);
}
/**
* Get Package Paths
*
* Return a list of all package paths, by default it will ignore BASEPATH.
*
* @param boolean include base path flag
* @return void
*/
public function get_package_paths($include_base = FALSE)
{
// Pass arguments to core method
return $this->CI->get_package_paths($include_base);
}
/**
* Autoloader
*
* The config/autoload.php file contains an array that permits various
* resources to be loaded automatically.
* The CodeIgniter object calls this protected method via CI_Core_share.
*
* @access protected
* @param array autoload array
* @return void
*/
protected function _autoloader($autoload)
{
// Autoload languages
if (isset($autoload['language']) && count($autoload['language']) > 0)
{
$this->language($autoload['language']);
}
// A little tweak to remain backward compatible
// The $autoload['core'] item was deprecated
if (!isset($autoload['libraries']) && isset($autoload['core']))
{
$autoload['libraries'] = $autoload['core'];
}
// Load libraries
if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
{
// Load the database driver.
if (in_array('database', $autoload['libraries']))
{
$this->database();
$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
}
// Load all other libraries
foreach ($autoload['libraries'] as $item)
{
$this->library($item);
}
}
// Autoload controllers and models
foreach (array('helper', 'controller', 'model') as $type)
{
if (isset($autoload[$type]))
{
// Force item to array
$items = is_array($autoload[$type]) ? $autoload[$type] : array($autoload[$type]);
foreach ($items as $item)
{
$this->$type($item);
}
}
}
}
/**
* Get path from filename
*
* This helper function separates dirname, if present, from file.
* It should only be called internally.
*
* @access protected
* @param string reference to filename (to be modified)
* @return string path name
*/
protected function _get_path(&$file)
{
// Get any leading dirname without a leading slash
$path = dirname(ltrim($file, '/'));
// Strip filename to basename
$file = basename($file, '.php');
// Return leading dirname, if any
return ($path == '.') ? '' : $path.'/';
}
}
/* End of file Loader.php */
/* Location: ./system/core/Loader.php */