PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/system/core/Loader.php

https://bitbucket.org/my30dc/my30dc.com
PHP | 1104 lines | 575 code | 153 blank | 376 comment | 82 complexity | dac1a7b3aded752dd38e1c776dce459b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Loader Class
  18. *
  19. * Loads views and files
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @author ExpressionEngine Dev Team
  24. * @category Loader
  25. * @link http://codeigniter.com/user_guide/libraries/loader.html
  26. */
  27. class CI_Loader {
  28. // All these are set automatically. Don't mess with them.
  29. var $_ci_ob_level;
  30. var $_ci_view_path = '';
  31. var $_ci_library_paths = array();
  32. var $_ci_model_paths = array();
  33. var $_ci_helper_paths = array();
  34. var $_base_classes = array(); // Set by the controller class
  35. var $_ci_cached_vars = array();
  36. var $_ci_classes = array();
  37. var $_ci_loaded_files = array();
  38. var $_ci_models = array();
  39. var $_ci_helpers = array();
  40. var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
  41. /**
  42. * Constructor
  43. *
  44. * Sets the path to the view files and gets the initial output buffering level
  45. *
  46. * @access public
  47. */
  48. function __construct()
  49. {
  50. $this->_ci_view_path = APPPATH.'views/';
  51. $this->_ci_ob_level = ob_get_level();
  52. $this->_ci_library_paths = array(APPPATH, BASEPATH);
  53. $this->_ci_helper_paths = array(APPPATH, BASEPATH);
  54. $this->_ci_model_paths = array(APPPATH);
  55. log_message('debug', "Loader Class Initialized");
  56. }
  57. // --------------------------------------------------------------------
  58. /**
  59. * Class Loader
  60. *
  61. * This function lets users load and instantiate classes.
  62. * It is designed to be called from a user's app controllers.
  63. *
  64. * @access public
  65. * @param string the name of the class
  66. * @param mixed the optional parameters
  67. * @param string an optional object name
  68. * @return void
  69. */
  70. function library($library = '', $params = NULL, $object_name = NULL)
  71. {
  72. if (is_array($library))
  73. {
  74. foreach ($library as $class)
  75. {
  76. $this->library($class, $params);
  77. }
  78. return;
  79. }
  80. if ($library == '' OR isset($this->_base_classes[$library]))
  81. {
  82. return FALSE;
  83. }
  84. if ( ! is_null($params) && ! is_array($params))
  85. {
  86. $params = NULL;
  87. }
  88. $this->_ci_load_class($library, $params, $object_name);
  89. }
  90. // --------------------------------------------------------------------
  91. /**
  92. * Model Loader
  93. *
  94. * This function lets users load and instantiate models.
  95. *
  96. * @access public
  97. * @param string the name of the class
  98. * @param string name for the model
  99. * @param bool database connection
  100. * @return void
  101. */
  102. function model($model, $name = '', $db_conn = FALSE)
  103. {
  104. if (is_array($model))
  105. {
  106. foreach ($model as $babe)
  107. {
  108. $this->model($babe);
  109. }
  110. return;
  111. }
  112. if ($model == '')
  113. {
  114. return;
  115. }
  116. $path = '';
  117. // Is the model in a sub-folder? If so, parse out the filename and path.
  118. if (($last_slash = strrpos($model, '/')) !== FALSE)
  119. {
  120. // The path is in front of the last slash
  121. $path = substr($model, 0, $last_slash + 1);
  122. // And the model name behind it
  123. $model = substr($model, $last_slash + 1);
  124. }
  125. if ($name == '')
  126. {
  127. $name = $model;
  128. }
  129. if (in_array($name, $this->_ci_models, TRUE))
  130. {
  131. return;
  132. }
  133. $CI =& get_instance();
  134. if (isset($CI->$name))
  135. {
  136. show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
  137. }
  138. $model = strtolower($model);
  139. foreach ($this->_ci_model_paths as $mod_path)
  140. {
  141. if ( ! file_exists($mod_path.'models/'.$path.$model.EXT))
  142. {
  143. continue;
  144. }
  145. if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
  146. {
  147. if ($db_conn === TRUE)
  148. {
  149. $db_conn = '';
  150. }
  151. $CI->load->database($db_conn, FALSE, TRUE);
  152. }
  153. if ( ! class_exists('CI_Model'))
  154. {
  155. load_class('Model', 'core');
  156. }
  157. require_once($mod_path.'models/'.$path.$model.EXT);
  158. $model = ucfirst($model);
  159. $CI->$name = new $model();
  160. $this->_ci_models[] = $name;
  161. return;
  162. }
  163. // couldn't find the model
  164. show_error('Unable to locate the model you have specified: '.$model);
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Database Loader
  169. *
  170. * @access public
  171. * @param string the DB credentials
  172. * @param bool whether to return the DB object
  173. * @param bool whether to enable active record (this allows us to override the config setting)
  174. * @return object
  175. */
  176. function database($params = '', $return = FALSE, $active_record = NULL)
  177. {
  178. // Grab the super object
  179. $CI =& get_instance();
  180. // Do we even need to load the database class?
  181. if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
  182. {
  183. return FALSE;
  184. }
  185. require_once(BASEPATH.'database/DB'.EXT);
  186. if ($return === TRUE)
  187. {
  188. return DB($params, $active_record);
  189. }
  190. // Initialize the db variable. Needed to prevent
  191. // reference errors with some configurations
  192. $CI->db = '';
  193. // Load the DB class
  194. $CI->db =& DB($params, $active_record);
  195. }
  196. // --------------------------------------------------------------------
  197. /**
  198. * Load the Utilities Class
  199. *
  200. * @access public
  201. * @return string
  202. */
  203. function dbutil()
  204. {
  205. if ( ! class_exists('CI_DB'))
  206. {
  207. $this->database();
  208. }
  209. $CI =& get_instance();
  210. // for backwards compatibility, load dbforge so we can extend dbutils off it
  211. // this use is deprecated and strongly discouraged
  212. $CI->load->dbforge();
  213. require_once(BASEPATH.'database/DB_utility'.EXT);
  214. require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
  215. $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
  216. $CI->dbutil = new $class();
  217. }
  218. // --------------------------------------------------------------------
  219. /**
  220. * Load the Database Forge Class
  221. *
  222. * @access public
  223. * @return string
  224. */
  225. function dbforge()
  226. {
  227. if ( ! class_exists('CI_DB'))
  228. {
  229. $this->database();
  230. }
  231. $CI =& get_instance();
  232. require_once(BASEPATH.'database/DB_forge'.EXT);
  233. require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
  234. $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
  235. $CI->dbforge = new $class();
  236. }
  237. // --------------------------------------------------------------------
  238. /**
  239. * Load View
  240. *
  241. * This function is used to load a "view" file. It has three parameters:
  242. *
  243. * 1. The name of the "view" file to be included.
  244. * 2. An associative array of data to be extracted for use in the view.
  245. * 3. TRUE/FALSE - whether to return the data or load it. In
  246. * some cases it's advantageous to be able to return data so that
  247. * a developer can process it in some way.
  248. *
  249. * @access public
  250. * @param string
  251. * @param array
  252. * @param bool
  253. * @return void
  254. */
  255. function view($view, $vars = array(), $return = FALSE)
  256. {
  257. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  258. }
  259. // --------------------------------------------------------------------
  260. /**
  261. * Load File
  262. *
  263. * This is a generic file loader
  264. *
  265. * @access public
  266. * @param string
  267. * @param bool
  268. * @return string
  269. */
  270. function file($path, $return = FALSE)
  271. {
  272. return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
  273. }
  274. // --------------------------------------------------------------------
  275. /**
  276. * Set Variables
  277. *
  278. * Once variables are set they become available within
  279. * the controller class and its "view" files.
  280. *
  281. * @access public
  282. * @param array
  283. * @return void
  284. */
  285. function vars($vars = array(), $val = '')
  286. {
  287. if ($val != '' AND is_string($vars))
  288. {
  289. $vars = array($vars => $val);
  290. }
  291. $vars = $this->_ci_object_to_array($vars);
  292. if (is_array($vars) AND count($vars) > 0)
  293. {
  294. foreach ($vars as $key => $val)
  295. {
  296. $this->_ci_cached_vars[$key] = $val;
  297. }
  298. }
  299. }
  300. // --------------------------------------------------------------------
  301. /**
  302. * Load Helper
  303. *
  304. * This function loads the specified helper file.
  305. *
  306. * @access public
  307. * @param mixed
  308. * @return void
  309. */
  310. function helper($helpers = array())
  311. {
  312. foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
  313. {
  314. if (isset($this->_ci_helpers[$helper]))
  315. {
  316. continue;
  317. }
  318. $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
  319. // Is this a helper extension request?
  320. if (file_exists($ext_helper))
  321. {
  322. $base_helper = BASEPATH.'helpers/'.$helper.EXT;
  323. if ( ! file_exists($base_helper))
  324. {
  325. show_error('Unable to load the requested file: helpers/'.$helper.EXT);
  326. }
  327. include_once($ext_helper);
  328. include_once($base_helper);
  329. $this->_ci_helpers[$helper] = TRUE;
  330. log_message('debug', 'Helper loaded: '.$helper);
  331. continue;
  332. }
  333. // Try to load the helper
  334. foreach ($this->_ci_helper_paths as $path)
  335. {
  336. if (file_exists($path.'helpers/'.$helper.EXT))
  337. {
  338. include_once($path.'helpers/'.$helper.EXT);
  339. $this->_ci_helpers[$helper] = TRUE;
  340. log_message('debug', 'Helper loaded: '.$helper);
  341. break;
  342. }
  343. }
  344. // unable to load the helper
  345. if ( ! isset($this->_ci_helpers[$helper]))
  346. {
  347. show_error('Unable to load the requested file: helpers/'.$helper.EXT);
  348. }
  349. }
  350. }
  351. // --------------------------------------------------------------------
  352. /**
  353. * Load Helpers
  354. *
  355. * This is simply an alias to the above function in case the
  356. * user has written the plural form of this function.
  357. *
  358. * @access public
  359. * @param array
  360. * @return void
  361. */
  362. function helpers($helpers = array())
  363. {
  364. $this->helper($helpers);
  365. }
  366. // --------------------------------------------------------------------
  367. /**
  368. * Loads a language file
  369. *
  370. * @access public
  371. * @param array
  372. * @param string
  373. * @return void
  374. */
  375. function language($file = array(), $lang = '')
  376. {
  377. $CI =& get_instance();
  378. if ( ! is_array($file))
  379. {
  380. $file = array($file);
  381. }
  382. foreach ($file as $langfile)
  383. {
  384. $CI->lang->load($langfile, $lang);
  385. }
  386. }
  387. // --------------------------------------------------------------------
  388. /**
  389. * Loads a config file
  390. *
  391. * @access public
  392. * @param string
  393. * @return void
  394. */
  395. function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  396. {
  397. $CI =& get_instance();
  398. $CI->config->load($file, $use_sections, $fail_gracefully);
  399. }
  400. // --------------------------------------------------------------------
  401. /**
  402. * Driver
  403. *
  404. * Loads a driver library
  405. *
  406. * @param string the name of the class
  407. * @param mixed the optional parameters
  408. * @param string an optional object name
  409. * @return void
  410. */
  411. function driver($library = '', $params = NULL, $object_name = NULL)
  412. {
  413. if ( ! class_exists('CI_Driver_Library'))
  414. {
  415. // we aren't instantiating an object here, that'll be done by the Library itself
  416. require BASEPATH.'libraries/Driver'.EXT;
  417. }
  418. // We can save the loader some time since Drivers will *always* be in a subfolder,
  419. // and typically identically named to the library
  420. if ( ! strpos($library, '/'))
  421. {
  422. $library = ucfirst($library).'/'.$library;
  423. }
  424. return $this->library($library, $params, $object_name);
  425. }
  426. // --------------------------------------------------------------------
  427. /**
  428. * Add Package Path
  429. *
  430. * Prepends a parent path to the library, model, helper, and config path arrays
  431. *
  432. * @access public
  433. * @param string
  434. * @return void
  435. */
  436. function add_package_path($path)
  437. {
  438. $path = rtrim($path, '/').'/';
  439. array_unshift($this->_ci_library_paths, $path);
  440. array_unshift($this->_ci_model_paths, $path);
  441. array_unshift($this->_ci_helper_paths, $path);
  442. // Add config file path
  443. $config =& $this->_ci_get_component('config');
  444. array_unshift($config->_config_paths, $path);
  445. }
  446. // --------------------------------------------------------------------
  447. /**
  448. * Get Package Paths
  449. *
  450. * Return a list of all package paths, by default it will ignore BASEPATH.
  451. *
  452. * @access public
  453. * @param string
  454. * @return void
  455. */
  456. function get_package_paths($include_base = FALSE)
  457. {
  458. return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
  459. }
  460. // --------------------------------------------------------------------
  461. /**
  462. * Remove Package Path
  463. *
  464. * Remove a path from the library, model, and helper path arrays if it exists
  465. * If no path is provided, the most recently added path is removed.
  466. *
  467. * @access public
  468. * @param type
  469. * @return type
  470. */
  471. function remove_package_path($path = '', $remove_config_path = TRUE)
  472. {
  473. $config =& $this->_ci_get_component('config');
  474. if ($path == '')
  475. {
  476. $void = array_shift($this->_ci_library_paths);
  477. $void = array_shift($this->_ci_model_paths);
  478. $void = array_shift($this->_ci_helper_paths);
  479. $void = array_shift($config->_config_paths);
  480. }
  481. else
  482. {
  483. $path = rtrim($path, '/').'/';
  484. foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
  485. {
  486. if (($key = array_search($path, $this->{$var})) !== FALSE)
  487. {
  488. unset($this->{$var}[$key]);
  489. }
  490. }
  491. if (($key = array_search($path, $config->_config_paths)) !== FALSE)
  492. {
  493. unset($config->_config_paths[$key]);
  494. }
  495. }
  496. // make sure the application default paths are still in the array
  497. $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
  498. $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
  499. $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
  500. $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
  501. }
  502. // --------------------------------------------------------------------
  503. /**
  504. * Loader
  505. *
  506. * This function is used to load views and files.
  507. * Variables are prefixed with _ci_ to avoid symbol collision with
  508. * variables made available to view files
  509. *
  510. * @access private
  511. * @param array
  512. * @return void
  513. */
  514. function _ci_load($_ci_data)
  515. {
  516. // Set the default data variables
  517. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
  518. {
  519. $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
  520. }
  521. // Set the path to the requested file
  522. if ($_ci_path == '')
  523. {
  524. $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
  525. $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
  526. $_ci_path = $this->_ci_view_path.$_ci_file;
  527. }
  528. else
  529. {
  530. $_ci_x = explode('/', $_ci_path);
  531. $_ci_file = end($_ci_x);
  532. }
  533. if ( ! file_exists($_ci_path))
  534. {
  535. show_error('Unable to load the requested file: '.$_ci_file);
  536. }
  537. // This allows anything loaded using $this->load (views, files, etc.)
  538. // to become accessible from within the Controller and Model functions.
  539. $_ci_CI =& get_instance();
  540. foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
  541. {
  542. if ( ! isset($this->$_ci_key))
  543. {
  544. $this->$_ci_key =& $_ci_CI->$_ci_key;
  545. }
  546. }
  547. /*
  548. * Extract and cache variables
  549. *
  550. * You can either set variables using the dedicated $this->load_vars()
  551. * function or via the second parameter of this function. We'll merge
  552. * the two types and cache them so that views that are embedded within
  553. * other views can have access to these variables.
  554. */
  555. if (is_array($_ci_vars))
  556. {
  557. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  558. }
  559. extract($this->_ci_cached_vars);
  560. /*
  561. * Buffer the output
  562. *
  563. * We buffer the output for two reasons:
  564. * 1. Speed. You get a significant speed boost.
  565. * 2. So that the final rendered template can be
  566. * post-processed by the output class. Why do we
  567. * need post processing? For one thing, in order to
  568. * show the elapsed page load time. Unless we
  569. * can intercept the content right before it's sent to
  570. * the browser and then stop the timer it won't be accurate.
  571. */
  572. ob_start();
  573. // If the PHP installation does not support short tags we'll
  574. // do a little string replacement, changing the short tags
  575. // to standard PHP echo statements.
  576. if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
  577. {
  578. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  579. }
  580. else
  581. {
  582. include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  583. }
  584. log_message('debug', 'File loaded: '.$_ci_path);
  585. // Return the file data if requested
  586. if ($_ci_return === TRUE)
  587. {
  588. $buffer = ob_get_contents();
  589. @ob_end_clean();
  590. return $buffer;
  591. }
  592. /*
  593. * Flush the buffer... or buff the flusher?
  594. *
  595. * In order to permit views to be nested within
  596. * other views, we need to flush the content back out whenever
  597. * we are beyond the first level of output buffering so that
  598. * it can be seen and included properly by the first included
  599. * template and any subsequent ones. Oy!
  600. *
  601. */
  602. if (ob_get_level() > $this->_ci_ob_level + 1)
  603. {
  604. ob_end_flush();
  605. }
  606. else
  607. {
  608. $_ci_CI->output->append_output(ob_get_contents());
  609. @ob_end_clean();
  610. }
  611. }
  612. // --------------------------------------------------------------------
  613. /**
  614. * Load class
  615. *
  616. * This function loads the requested class.
  617. *
  618. * @access private
  619. * @param string the item that is being loaded
  620. * @param mixed any additional parameters
  621. * @param string an optional object name
  622. * @return void
  623. */
  624. function _ci_load_class($class, $params = NULL, $object_name = NULL)
  625. {
  626. // Get the class name, and while we're at it trim any slashes.
  627. // The directory path can be included as part of the class name,
  628. // but we don't want a leading slash
  629. $class = str_replace(EXT, '', trim($class, '/'));
  630. // Was the path included with the class name?
  631. // We look for a slash to determine this
  632. $subdir = '';
  633. if (($last_slash = strrpos($class, '/')) !== FALSE)
  634. {
  635. // Extract the path
  636. $subdir = substr($class, 0, $last_slash + 1);
  637. // Get the filename from the path
  638. $class = substr($class, $last_slash + 1);
  639. }
  640. // We'll test for both lowercase and capitalized versions of the file name
  641. foreach (array(ucfirst($class), strtolower($class)) as $class)
  642. {
  643. $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
  644. // Is this a class extension request?
  645. if (file_exists($subclass))
  646. {
  647. $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
  648. if ( ! file_exists($baseclass))
  649. {
  650. log_message('error', "Unable to load the requested class: ".$class);
  651. show_error("Unable to load the requested class: ".$class);
  652. }
  653. // Safety: Was the class already loaded by a previous call?
  654. if (in_array($subclass, $this->_ci_loaded_files))
  655. {
  656. // Before we deem this to be a duplicate request, let's see
  657. // if a custom object name is being supplied. If so, we'll
  658. // return a new instance of the object
  659. if ( ! is_null($object_name))
  660. {
  661. $CI =& get_instance();
  662. if ( ! isset($CI->$object_name))
  663. {
  664. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  665. }
  666. }
  667. $is_duplicate = TRUE;
  668. log_message('debug', $class." class already loaded. Second attempt ignored.");
  669. return;
  670. }
  671. include_once($baseclass);
  672. include_once($subclass);
  673. $this->_ci_loaded_files[] = $subclass;
  674. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  675. }
  676. // Lets search for the requested library file and load it.
  677. $is_duplicate = FALSE;
  678. foreach ($this->_ci_library_paths as $path)
  679. {
  680. $filepath = $path.'libraries/'.$subdir.$class.EXT;
  681. // Does the file exist? No? Bummer...
  682. if ( ! file_exists($filepath))
  683. {
  684. continue;
  685. }
  686. // Safety: Was the class already loaded by a previous call?
  687. if (in_array($filepath, $this->_ci_loaded_files))
  688. {
  689. // Before we deem this to be a duplicate request, let's see
  690. // if a custom object name is being supplied. If so, we'll
  691. // return a new instance of the object
  692. if ( ! is_null($object_name))
  693. {
  694. $CI =& get_instance();
  695. if ( ! isset($CI->$object_name))
  696. {
  697. return $this->_ci_init_class($class, '', $params, $object_name);
  698. }
  699. }
  700. $is_duplicate = TRUE;
  701. log_message('debug', $class." class already loaded. Second attempt ignored.");
  702. return;
  703. }
  704. include_once($filepath);
  705. $this->_ci_loaded_files[] = $filepath;
  706. return $this->_ci_init_class($class, '', $params, $object_name);
  707. }
  708. } // END FOREACH
  709. // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
  710. if ($subdir == '')
  711. {
  712. $path = strtolower($class).'/'.$class;
  713. return $this->_ci_load_class($path, $params);
  714. }
  715. // If we got this far we were unable to find the requested class.
  716. // We do not issue errors if the load call failed due to a duplicate request
  717. if ($is_duplicate == FALSE)
  718. {
  719. log_message('error', "Unable to load the requested class: ".$class);
  720. show_error("Unable to load the requested class: ".$class);
  721. }
  722. }
  723. // --------------------------------------------------------------------
  724. /**
  725. * Instantiates a class
  726. *
  727. * @access private
  728. * @param string
  729. * @param string
  730. * @param string an optional object name
  731. * @return null
  732. */
  733. function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
  734. {
  735. // Is there an associated config file for this class? Note: these should always be lowercase
  736. if ($config === NULL)
  737. {
  738. // Fetch the config paths containing any package paths
  739. $config_component = $this->_ci_get_component('config');
  740. if (is_array($config_component->_config_paths))
  741. {
  742. // Break on the first found file, thus package files
  743. // are not overridden by default paths
  744. foreach ($config_component->_config_paths as $path)
  745. {
  746. // We test for both uppercase and lowercase, for servers that
  747. // are case-sensitive with regard to file names. Check for environment
  748. // first, global next
  749. if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).EXT))
  750. {
  751. include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).EXT);
  752. break;
  753. }
  754. elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).EXT))
  755. {
  756. include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).EXT);
  757. break;
  758. }
  759. elseif (file_exists($path .'config/'.strtolower($class).EXT))
  760. {
  761. include_once($path .'config/'.strtolower($class).EXT);
  762. break;
  763. }
  764. elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).EXT))
  765. {
  766. include_once($path .'config/'.ucfirst(strtolower($class)).EXT);
  767. break;
  768. }
  769. }
  770. }
  771. }
  772. if ($prefix == '')
  773. {
  774. if (class_exists('CI_'.$class))
  775. {
  776. $name = 'CI_'.$class;
  777. }
  778. elseif (class_exists(config_item('subclass_prefix').$class))
  779. {
  780. $name = config_item('subclass_prefix').$class;
  781. }
  782. else
  783. {
  784. $name = $class;
  785. }
  786. }
  787. else
  788. {
  789. $name = $prefix.$class;
  790. }
  791. // Is the class name valid?
  792. if ( ! class_exists($name))
  793. {
  794. log_message('error', "Non-existent class: ".$name);
  795. show_error("Non-existent class: ".$class);
  796. }
  797. // Set the variable name we will assign the class to
  798. // Was a custom class name supplied? If so we'll use it
  799. $class = strtolower($class);
  800. if (is_null($object_name))
  801. {
  802. $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
  803. }
  804. else
  805. {
  806. $classvar = $object_name;
  807. }
  808. // Save the class name and object name
  809. $this->_ci_classes[$class] = $classvar;
  810. // Instantiate the class
  811. $CI =& get_instance();
  812. if ($config !== NULL)
  813. {
  814. $CI->$classvar = new $name($config);
  815. }
  816. else
  817. {
  818. $CI->$classvar = new $name;
  819. }
  820. }
  821. // --------------------------------------------------------------------
  822. /**
  823. * Autoloader
  824. *
  825. * The config/autoload.php file contains an array that permits sub-systems,
  826. * libraries, and helpers to be loaded automatically.
  827. *
  828. * @access private
  829. * @param array
  830. * @return void
  831. */
  832. function _ci_autoloader()
  833. {
  834. if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload'.EXT))
  835. {
  836. include_once(APPPATH.'config/'.ENVIRONMENT.'/autoload'.EXT);
  837. }
  838. else
  839. {
  840. include_once(APPPATH.'config/autoload'.EXT);
  841. }
  842. if ( ! isset($autoload))
  843. {
  844. return FALSE;
  845. }
  846. // Autoload packages
  847. if (isset($autoload['packages']))
  848. {
  849. foreach ($autoload['packages'] as $package_path)
  850. {
  851. $this->add_package_path($package_path);
  852. }
  853. }
  854. // Load any custom config file
  855. if (count($autoload['config']) > 0)
  856. {
  857. $CI =& get_instance();
  858. foreach ($autoload['config'] as $key => $val)
  859. {
  860. $CI->config->load($val);
  861. }
  862. }
  863. // Autoload helpers and languages
  864. foreach (array('helper', 'language') as $type)
  865. {
  866. if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
  867. {
  868. $this->$type($autoload[$type]);
  869. }
  870. }
  871. // A little tweak to remain backward compatible
  872. // The $autoload['core'] item was deprecated
  873. if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
  874. {
  875. $autoload['libraries'] = $autoload['core'];
  876. }
  877. // Load libraries
  878. if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
  879. {
  880. // Load the database driver.
  881. if (in_array('database', $autoload['libraries']))
  882. {
  883. $this->database();
  884. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  885. }
  886. // Load all other libraries
  887. foreach ($autoload['libraries'] as $item)
  888. {
  889. $this->library($item);
  890. }
  891. }
  892. // Autoload models
  893. if (isset($autoload['model']))
  894. {
  895. $this->model($autoload['model']);
  896. }
  897. }
  898. // --------------------------------------------------------------------
  899. /**
  900. * Object to Array
  901. *
  902. * Takes an object as input and converts the class variables to array key/vals
  903. *
  904. * @access private
  905. * @param object
  906. * @return array
  907. */
  908. function _ci_object_to_array($object)
  909. {
  910. return (is_object($object)) ? get_object_vars($object) : $object;
  911. }
  912. // --------------------------------------------------------------------
  913. /**
  914. * Get a reference to a specific library or model
  915. *
  916. * @access private
  917. * @return bool
  918. */
  919. function &_ci_get_component($component)
  920. {
  921. $CI =& get_instance();
  922. return $CI->$component;
  923. }
  924. // --------------------------------------------------------------------
  925. /**
  926. * Prep filename
  927. *
  928. * This function preps the name of various items to make loading them more reliable.
  929. *
  930. * @access private
  931. * @param mixed
  932. * @return array
  933. */
  934. function _ci_prep_filename($filename, $extension)
  935. {
  936. if ( ! is_array($filename))
  937. {
  938. return array(strtolower(str_replace(EXT, '', str_replace($extension, '', $filename)).$extension));
  939. }
  940. else
  941. {
  942. foreach ($filename as $key => $val)
  943. {
  944. $filename[$key] = strtolower(str_replace(EXT, '', str_replace($extension, '', $val)).$extension);
  945. }
  946. return $filename;
  947. }
  948. }
  949. }
  950. /* End of file Loader.php */
  951. /* Location: ./system/core/Loader.php */