PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/system/core/Loader.php

https://github.com/igravity/CodeIgniter
PHP | 1296 lines | 647 code | 165 blank | 484 comment | 88 complexity | 54eb1cf80c391574d6d66f415fa899e4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. defined('BASEPATH') OR exit('No direct script access allowed');
  28. /**
  29. * Loader Class
  30. *
  31. * Loads framework components.
  32. *
  33. * @package CodeIgniter
  34. * @subpackage Libraries
  35. * @category Loader
  36. * @author EllisLab Dev Team
  37. * @link http://codeigniter.com/user_guide/libraries/loader.html
  38. */
  39. class CI_Loader {
  40. // All these are set automatically. Don't mess with them.
  41. /**
  42. * Nesting level of the output buffering mechanism
  43. *
  44. * @var int
  45. */
  46. protected $_ci_ob_level;
  47. /**
  48. * List of paths to load views from
  49. *
  50. * @var array
  51. */
  52. protected $_ci_view_paths = array(VIEWPATH => TRUE);
  53. /**
  54. * List of paths to load libraries from
  55. *
  56. * @var array
  57. */
  58. protected $_ci_library_paths = array(APPPATH, BASEPATH);
  59. /**
  60. * List of paths to load models from
  61. *
  62. * @var array
  63. */
  64. protected $_ci_model_paths = array(APPPATH);
  65. /**
  66. * List of paths to load helpers from
  67. *
  68. * @var array
  69. */
  70. protected $_ci_helper_paths = array(APPPATH, BASEPATH);
  71. /**
  72. * List of cached variables
  73. *
  74. * @var array
  75. */
  76. protected $_ci_cached_vars = array();
  77. /**
  78. * List of loaded classes
  79. *
  80. * @var array
  81. */
  82. protected $_ci_classes = array();
  83. /**
  84. * List of loaded models
  85. *
  86. * @var array
  87. */
  88. protected $_ci_models = array();
  89. /**
  90. * List of loaded helpers
  91. *
  92. * @var array
  93. */
  94. protected $_ci_helpers = array();
  95. /**
  96. * List of class name mappings
  97. *
  98. * @var array
  99. */
  100. protected $_ci_varmap = array(
  101. 'unit_test' => 'unit',
  102. 'user_agent' => 'agent'
  103. );
  104. // --------------------------------------------------------------------
  105. /**
  106. * Class constructor
  107. *
  108. * Sets component load paths, gets the initial output buffering level.
  109. *
  110. * @return void
  111. */
  112. public function __construct()
  113. {
  114. $this->_ci_ob_level = ob_get_level();
  115. $this->_ci_classes =& is_loaded();
  116. log_message('debug', 'Loader Class Initialized');
  117. }
  118. // --------------------------------------------------------------------
  119. /**
  120. * Initializer
  121. *
  122. * @todo Figure out a way to move this to the constructor
  123. * without breaking *package_path*() methods.
  124. * @uses CI_Loader::_ci_autoloader()
  125. * @used-by CI_Controller::__construct()
  126. * @return void
  127. */
  128. public function initialize()
  129. {
  130. $this->_ci_autoloader();
  131. }
  132. // --------------------------------------------------------------------
  133. /**
  134. * Is Loaded
  135. *
  136. * A utility method to test if a class is in the self::$_ci_classes array.
  137. *
  138. * @used-by Mainly used by Form Helper function _get_validation_object().
  139. *
  140. * @param string $class Class name to check for
  141. * @return string|bool Class object name if loaded or FALSE
  142. */
  143. public function is_loaded($class)
  144. {
  145. return array_search(ucfirst($class), $this->_ci_classes, TRUE);
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * Library Loader
  150. *
  151. * Loads and instantiates libraries.
  152. * Designed to be called from application controllers.
  153. *
  154. * @param string $library Library name
  155. * @param array $params Optional parameters to pass to the library class constructor
  156. * @param string $object_name An optional object name to assign to
  157. * @return void
  158. */
  159. public function library($library = '', $params = NULL, $object_name = NULL)
  160. {
  161. if (empty($library))
  162. {
  163. return;
  164. }
  165. elseif (is_array($library))
  166. {
  167. foreach ($library as $class)
  168. {
  169. $this->library($class, $params);
  170. }
  171. return;
  172. }
  173. if ($params !== NULL && ! is_array($params))
  174. {
  175. $params = NULL;
  176. }
  177. $this->_ci_load_class($library, $params, $object_name);
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Model Loader
  182. *
  183. * Loads and instantiates libraries.
  184. *
  185. * @param string $model Model name
  186. * @param string $name An optional object name to assign to
  187. * @param bool $db_conn An optional database connection configuration to initialize
  188. * @return void
  189. */
  190. public function model($model, $name = '', $db_conn = FALSE)
  191. {
  192. if (empty($model))
  193. {
  194. return;
  195. }
  196. elseif (is_array($model))
  197. {
  198. foreach ($model as $key => $value)
  199. {
  200. $this->model(is_int($key) ? $value : $key, $value);
  201. }
  202. return;
  203. }
  204. $path = '';
  205. // Is the model in a sub-folder? If so, parse out the filename and path.
  206. if (($last_slash = strrpos($model, '/')) !== FALSE)
  207. {
  208. // The path is in front of the last slash
  209. $path = substr($model, 0, ++$last_slash);
  210. // And the model name behind it
  211. $model = substr($model, $last_slash);
  212. }
  213. if (empty($name))
  214. {
  215. $name = $model;
  216. }
  217. if (in_array($name, $this->_ci_models, TRUE))
  218. {
  219. return;
  220. }
  221. $CI =& get_instance();
  222. if (isset($CI->$name))
  223. {
  224. show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
  225. }
  226. if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
  227. {
  228. if ($db_conn === TRUE)
  229. {
  230. $db_conn = '';
  231. }
  232. $CI->load->database($db_conn, FALSE, TRUE);
  233. }
  234. if ( ! class_exists('CI_Model', FALSE))
  235. {
  236. load_class('Model', 'core');
  237. }
  238. $model = ucfirst(strtolower($model));
  239. foreach ($this->_ci_model_paths as $mod_path)
  240. {
  241. if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
  242. {
  243. continue;
  244. }
  245. require_once($mod_path.'models/'.$path.$model.'.php');
  246. $CI->$name = new $model();
  247. $this->_ci_models[] = $name;
  248. return;
  249. }
  250. // couldn't find the model
  251. show_error('Unable to locate the model you have specified: '.$model);
  252. }
  253. // --------------------------------------------------------------------
  254. /**
  255. * Database Loader
  256. *
  257. * @param mixed $params Database configuration options
  258. * @param bool $return Whether to return the database object
  259. * @param bool $query_builder Whether to enable Query Builder
  260. * (overrides the configuration setting)
  261. *
  262. * @return void|object|bool Database object if $return is set to TRUE,
  263. * FALSE on failure, void in any other case
  264. */
  265. public function database($params = '', $return = FALSE, $query_builder = NULL)
  266. {
  267. // Grab the super object
  268. $CI =& get_instance();
  269. // Do we even need to load the database class?
  270. if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
  271. {
  272. return FALSE;
  273. }
  274. require_once(BASEPATH.'database/DB.php');
  275. if ($return === TRUE)
  276. {
  277. return DB($params, $query_builder);
  278. }
  279. // Initialize the db variable. Needed to prevent
  280. // reference errors with some configurations
  281. $CI->db = '';
  282. // Load the DB class
  283. $CI->db =& DB($params, $query_builder);
  284. }
  285. // --------------------------------------------------------------------
  286. /**
  287. * Load the Database Utilities Class
  288. *
  289. * @param object $db Database object
  290. * @param bool $return Whether to return the DB Forge class object or not
  291. * @return void|object
  292. */
  293. public function dbutil($db = NULL, $return = FALSE)
  294. {
  295. $CI =& get_instance();
  296. if ( ! is_object($db) OR ! ($db instanceof CI_DB))
  297. {
  298. class_exists('CI_DB', FALSE) OR $this->database();
  299. $db =& $CI->db;
  300. }
  301. require_once(BASEPATH.'database/DB_utility.php');
  302. require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
  303. $class = 'CI_DB_'.$db->dbdriver.'_utility';
  304. if ($return === TRUE)
  305. {
  306. return new $class($db);
  307. }
  308. $CI->dbutil = new $class($db);
  309. }
  310. // --------------------------------------------------------------------
  311. /**
  312. * Load the Database Forge Class
  313. *
  314. * @param object $db Database object
  315. * @param bool $return Whether to return the DB Forge class object or not
  316. * @return void|object
  317. */
  318. public function dbforge($db = NULL, $return = FALSE)
  319. {
  320. $CI =& get_instance();
  321. if ( ! is_object($db) OR ! ($db instanceof CI_DB))
  322. {
  323. class_exists('CI_DB', FALSE) OR $this->database();
  324. $db =& $CI->db;
  325. }
  326. require_once(BASEPATH.'database/DB_forge.php');
  327. require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
  328. if ( ! empty($db->subdriver))
  329. {
  330. $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
  331. if (file_exists($driver_path))
  332. {
  333. require_once($driver_path);
  334. $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
  335. }
  336. }
  337. else
  338. {
  339. $class = 'CI_DB_'.$db->dbdriver.'_forge';
  340. }
  341. if ($return === TRUE)
  342. {
  343. return new $class($db);
  344. }
  345. $CI->dbforge = new $class($db);
  346. }
  347. // --------------------------------------------------------------------
  348. /**
  349. * View Loader
  350. *
  351. * Loads "view" files.
  352. *
  353. * @param string $view View name
  354. * @param array $vars An associative array of data
  355. * to be extracted for use in the view
  356. * @param bool $return Whether to return the view output
  357. * or leave it to the Output class
  358. * @return void
  359. */
  360. public function view($view, $vars = array(), $return = FALSE)
  361. {
  362. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  363. }
  364. // --------------------------------------------------------------------
  365. /**
  366. * Generic File Loader
  367. *
  368. * @param string $path File path
  369. * @param bool $return Whether to return the file output
  370. * @return void|string
  371. */
  372. public function file($path, $return = FALSE)
  373. {
  374. return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
  375. }
  376. // --------------------------------------------------------------------
  377. /**
  378. * Set Variables
  379. *
  380. * Once variables are set they become available within
  381. * the controller class and its "view" files.
  382. *
  383. * @param array|object|string $vars
  384. * An associative array or object containing values
  385. * to be set, or a value's name if string
  386. * @param string $val Value to set, only used if $vars is a string
  387. * @return void
  388. */
  389. public function vars($vars = array(), $val = '')
  390. {
  391. if (is_string($vars))
  392. {
  393. $vars = array($vars => $val);
  394. }
  395. $vars = $this->_ci_object_to_array($vars);
  396. if (is_array($vars) && count($vars) > 0)
  397. {
  398. foreach ($vars as $key => $val)
  399. {
  400. $this->_ci_cached_vars[$key] = $val;
  401. }
  402. }
  403. }
  404. // --------------------------------------------------------------------
  405. /**
  406. * Get Variable
  407. *
  408. * Check if a variable is set and retrieve it.
  409. *
  410. * @param string $key Variable name
  411. * @return mixed The variable or NULL if not found
  412. */
  413. public function get_var($key)
  414. {
  415. return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
  416. }
  417. // --------------------------------------------------------------------
  418. /**
  419. * Get Variables
  420. *
  421. * Retrieves all loaded variables.
  422. *
  423. * @return array
  424. */
  425. public function get_vars()
  426. {
  427. return $this->_ci_cached_vars;
  428. }
  429. // --------------------------------------------------------------------
  430. /**
  431. * Helper Loader
  432. *
  433. * @param string|string[] $helpers Helper name(s)
  434. * @return void
  435. */
  436. public function helper($helpers = array())
  437. {
  438. foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
  439. {
  440. if (isset($this->_ci_helpers[$helper]))
  441. {
  442. continue;
  443. }
  444. // Is this a helper extension request?
  445. $ext_helper = config_item('subclass_prefix').$helper;
  446. $ext_loaded = FALSE;
  447. foreach ($this->_ci_helper_paths as $path)
  448. {
  449. if (file_exists($path.'helpers/'.$ext_helper.'.php'))
  450. {
  451. include_once($path.'helpers/'.$ext_helper.'.php');
  452. $ext_loaded = TRUE;
  453. }
  454. }
  455. // If we have loaded extensions - check if the base one is here
  456. if ($ext_loaded === TRUE)
  457. {
  458. $base_helper = BASEPATH.'helpers/'.$helper.'.php';
  459. if ( ! file_exists($base_helper))
  460. {
  461. show_error('Unable to load the requested file: helpers/'.$helper.'.php');
  462. }
  463. include_once($base_helper);
  464. $this->_ci_helpers[$helper] = TRUE;
  465. log_message('debug', 'Helper loaded: '.$helper);
  466. continue;
  467. }
  468. // No extensions found ... try loading regular helpers and/or overrides
  469. foreach ($this->_ci_helper_paths as $path)
  470. {
  471. if (file_exists($path.'helpers/'.$helper.'.php'))
  472. {
  473. include_once($path.'helpers/'.$helper.'.php');
  474. $this->_ci_helpers[$helper] = TRUE;
  475. log_message('debug', 'Helper loaded: '.$helper);
  476. break;
  477. }
  478. }
  479. // unable to load the helper
  480. if ( ! isset($this->_ci_helpers[$helper]))
  481. {
  482. show_error('Unable to load the requested file: helpers/'.$helper.'.php');
  483. }
  484. }
  485. }
  486. // --------------------------------------------------------------------
  487. /**
  488. * Load Helpers
  489. *
  490. * An alias for the helper() method in case the developer has
  491. * written the plural form of it.
  492. *
  493. * @uses CI_Loader::helper()
  494. * @param string|string[] $helpers Helper name(s)
  495. * @return void
  496. */
  497. public function helpers($helpers = array())
  498. {
  499. $this->helper($helpers);
  500. }
  501. // --------------------------------------------------------------------
  502. /**
  503. * Language Loader
  504. *
  505. * Loads language files.
  506. *
  507. * @param string|string[] $files List of language file names to load
  508. * @param string Language name
  509. * @return void
  510. */
  511. public function language($files = array(), $lang = '')
  512. {
  513. $CI =& get_instance();
  514. is_array($files) OR $files = array($files);
  515. foreach ($files as $langfile)
  516. {
  517. $CI->lang->load($langfile, $lang);
  518. }
  519. }
  520. // --------------------------------------------------------------------
  521. /**
  522. * Config Loader
  523. *
  524. * Loads a config file (an alias for CI_Config::load()).
  525. *
  526. * @uses CI_Config::load()
  527. * @param string $file Configuration file name
  528. * @param bool $use_sections Whether configuration values should be loaded into their own section
  529. * @param bool $fail_gracefully Whether to just return FALSE or display an error message
  530. * @return bool TRUE if the file was loaded correctly or FALSE on failure
  531. */
  532. public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  533. {
  534. $CI =& get_instance();
  535. return $CI->config->load($file, $use_sections, $fail_gracefully);
  536. }
  537. // --------------------------------------------------------------------
  538. /**
  539. * Driver Loader
  540. *
  541. * Loads a driver library.
  542. *
  543. * @param string|string[] $library Driver name(s)
  544. * @param array $params Optional parameters to pass to the driver
  545. * @param string $object_name An optional object name to assign to
  546. *
  547. * @return void|object|bool Object or FALSE on failure if $library is a string
  548. * and $object_name is set. void otherwise.
  549. */
  550. public function driver($library = '', $params = NULL, $object_name = NULL)
  551. {
  552. if (is_array($library))
  553. {
  554. foreach ($library as $driver)
  555. {
  556. $this->driver($driver);
  557. }
  558. return;
  559. }
  560. if ($library === '')
  561. {
  562. return FALSE;
  563. }
  564. if ( ! class_exists('CI_Driver_Library', FALSE))
  565. {
  566. // We aren't instantiating an object here, just making the base class available
  567. require BASEPATH.'libraries/Driver.php';
  568. }
  569. // We can save the loader some time since Drivers will *always* be in a subfolder,
  570. // and typically identically named to the library
  571. if ( ! strpos($library, '/'))
  572. {
  573. $library = ucfirst($library).'/'.$library;
  574. }
  575. return $this->library($library, $params, $object_name);
  576. }
  577. // --------------------------------------------------------------------
  578. /**
  579. * Add Package Path
  580. *
  581. * Prepends a parent path to the library, model, helper and config
  582. * path arrays.
  583. *
  584. * @see CI_Loader::$_ci_library_paths
  585. * @see CI_Loader::$_ci_model_paths
  586. * @see CI_Loader::$_ci_helper_paths
  587. * @see CI_Config::$_config_paths
  588. *
  589. * @param string $path Path to add
  590. * @param bool $view_cascade (default: TRUE)
  591. * @return void
  592. */
  593. public function add_package_path($path, $view_cascade = TRUE)
  594. {
  595. $path = rtrim($path, '/').'/';
  596. array_unshift($this->_ci_library_paths, $path);
  597. array_unshift($this->_ci_model_paths, $path);
  598. array_unshift($this->_ci_helper_paths, $path);
  599. $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
  600. // Add config file path
  601. $config =& $this->_ci_get_component('config');
  602. $config->_config_paths[] = $path;
  603. }
  604. // --------------------------------------------------------------------
  605. /**
  606. * Get Package Paths
  607. *
  608. * Return a list of all package paths.
  609. *
  610. * @param bool $include_base Whether to include BASEPATH (default: FALSE)
  611. * @return array
  612. */
  613. public function get_package_paths($include_base = FALSE)
  614. {
  615. return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
  616. }
  617. // --------------------------------------------------------------------
  618. /**
  619. * Remove Package Path
  620. *
  621. * Remove a path from the library, model, helper and/or config
  622. * path arrays if it exists. If no path is provided, the most recently
  623. * added path will be removed removed.
  624. *
  625. * @param string $path Path to remove
  626. * @return void
  627. */
  628. public function remove_package_path($path = '')
  629. {
  630. $config =& $this->_ci_get_component('config');
  631. if ($path === '')
  632. {
  633. array_shift($this->_ci_library_paths);
  634. array_shift($this->_ci_model_paths);
  635. array_shift($this->_ci_helper_paths);
  636. array_shift($this->_ci_view_paths);
  637. array_pop($config->_config_paths);
  638. }
  639. else
  640. {
  641. $path = rtrim($path, '/').'/';
  642. foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
  643. {
  644. if (($key = array_search($path, $this->{$var})) !== FALSE)
  645. {
  646. unset($this->{$var}[$key]);
  647. }
  648. }
  649. if (isset($this->_ci_view_paths[$path.'views/']))
  650. {
  651. unset($this->_ci_view_paths[$path.'views/']);
  652. }
  653. if (($key = array_search($path, $config->_config_paths)) !== FALSE)
  654. {
  655. unset($config->_config_paths[$key]);
  656. }
  657. }
  658. // make sure the application default paths are still in the array
  659. $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
  660. $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
  661. $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
  662. $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
  663. $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
  664. }
  665. // --------------------------------------------------------------------
  666. /**
  667. * Internal CI Data Loader
  668. *
  669. * Used to load views and files.
  670. *
  671. * Variables are prefixed with _ci_ to avoid symbol collision with
  672. * variables made available to view files.
  673. *
  674. * @used-by CI_Loader::view()
  675. * @used-by CI_Loader::file()
  676. * @param array $_ci_data Data to load
  677. * @return void
  678. */
  679. protected function _ci_load($_ci_data)
  680. {
  681. // Set the default data variables
  682. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
  683. {
  684. $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
  685. }
  686. $file_exists = FALSE;
  687. // Set the path to the requested file
  688. if (is_string($_ci_path) && $_ci_path !== '')
  689. {
  690. $_ci_x = explode('/', $_ci_path);
  691. $_ci_file = end($_ci_x);
  692. }
  693. else
  694. {
  695. $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
  696. $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
  697. foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
  698. {
  699. if (file_exists($_ci_view_file.$_ci_file))
  700. {
  701. $_ci_path = $_ci_view_file.$_ci_file;
  702. $file_exists = TRUE;
  703. break;
  704. }
  705. if ( ! $cascade)
  706. {
  707. break;
  708. }
  709. }
  710. }
  711. if ( ! $file_exists && ! file_exists($_ci_path))
  712. {
  713. show_error('Unable to load the requested file: '.$_ci_file);
  714. }
  715. // This allows anything loaded using $this->load (views, files, etc.)
  716. // to become accessible from within the Controller and Model functions.
  717. $_ci_CI =& get_instance();
  718. foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
  719. {
  720. if ( ! isset($this->$_ci_key))
  721. {
  722. $this->$_ci_key =& $_ci_CI->$_ci_key;
  723. }
  724. }
  725. /*
  726. * Extract and cache variables
  727. *
  728. * You can either set variables using the dedicated $this->load->vars()
  729. * function or via the second parameter of this function. We'll merge
  730. * the two types and cache them so that views that are embedded within
  731. * other views can have access to these variables.
  732. */
  733. if (is_array($_ci_vars))
  734. {
  735. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  736. }
  737. extract($this->_ci_cached_vars);
  738. /*
  739. * Buffer the output
  740. *
  741. * We buffer the output for two reasons:
  742. * 1. Speed. You get a significant speed boost.
  743. * 2. So that the final rendered template can be post-processed by
  744. * the output class. Why do we need post processing? For one thing,
  745. * in order to show the elapsed page load time. Unless we can
  746. * intercept the content right before it's sent to the browser and
  747. * then stop the timer it won't be accurate.
  748. */
  749. ob_start();
  750. // If the PHP installation does not support short tags we'll
  751. // do a little string replacement, changing the short tags
  752. // to standard PHP echo statements.
  753. if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
  754. && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
  755. )
  756. {
  757. echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  758. }
  759. else
  760. {
  761. include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  762. }
  763. log_message('debug', 'File loaded: '.$_ci_path);
  764. // Return the file data if requested
  765. if ($_ci_return === TRUE)
  766. {
  767. $buffer = ob_get_contents();
  768. @ob_end_clean();
  769. return $buffer;
  770. }
  771. /*
  772. * Flush the buffer... or buff the flusher?
  773. *
  774. * In order to permit views to be nested within
  775. * other views, we need to flush the content back out whenever
  776. * we are beyond the first level of output buffering so that
  777. * it can be seen and included properly by the first included
  778. * template and any subsequent ones. Oy!
  779. */
  780. if (ob_get_level() > $this->_ci_ob_level + 1)
  781. {
  782. ob_end_flush();
  783. }
  784. else
  785. {
  786. $_ci_CI->output->append_output(ob_get_contents());
  787. @ob_end_clean();
  788. }
  789. }
  790. // --------------------------------------------------------------------
  791. /**
  792. * Internal CI Class Loader
  793. *
  794. * @used-by CI_Loader::library()
  795. * @uses CI_Loader::_ci_init_class()
  796. *
  797. * @param string $class Class name to load
  798. * @param mixed $params Optional parameters to pass to the class constructor
  799. * @param string $object_name Optional object name to assign to
  800. * @return void
  801. */
  802. protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
  803. {
  804. // Get the class name, and while we're at it trim any slashes.
  805. // The directory path can be included as part of the class name,
  806. // but we don't want a leading slash
  807. $class = str_replace('.php', '', trim($class, '/'));
  808. // Was the path included with the class name?
  809. // We look for a slash to determine this
  810. if (($last_slash = strrpos($class, '/')) !== FALSE)
  811. {
  812. // Extract the path
  813. $subdir = substr($class, 0, ++$last_slash);
  814. // Get the filename from the path
  815. $class = substr($class, $last_slash);
  816. }
  817. else
  818. {
  819. $subdir = '';
  820. }
  821. $class = ucfirst($class);
  822. $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
  823. // Is this a class extension request?
  824. if (file_exists($subclass))
  825. {
  826. $baseclass = BASEPATH.'libraries/'.$subdir.$class.'.php';
  827. if ( ! file_exists($baseclass))
  828. {
  829. log_message('error', 'Unable to load the requested class: '.$class);
  830. show_error('Unable to load the requested class: '.$class);
  831. }
  832. // Safety: Was the class already loaded by a previous call?
  833. if (class_exists(config_item('subclass_prefix').$class, FALSE))
  834. {
  835. // Before we deem this to be a duplicate request, let's see
  836. // if a custom object name is being supplied. If so, we'll
  837. // return a new instance of the object
  838. if ($object_name !== NULL)
  839. {
  840. $CI =& get_instance();
  841. if ( ! isset($CI->$object_name))
  842. {
  843. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  844. }
  845. }
  846. log_message('debug', $class.' class already loaded. Second attempt ignored.');
  847. return;
  848. }
  849. include_once($baseclass);
  850. include_once($subclass);
  851. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  852. }
  853. // Let's search for the requested library file and load it.
  854. foreach ($this->_ci_library_paths as $path)
  855. {
  856. $filepath = $path.'libraries/'.$subdir.$class.'.php';
  857. // Safety: Was the class already loaded by a previous call?
  858. if (class_exists($class, FALSE))
  859. {
  860. // Before we deem this to be a duplicate request, let's see
  861. // if a custom object name is being supplied. If so, we'll
  862. // return a new instance of the object
  863. if ($object_name !== NULL)
  864. {
  865. $CI =& get_instance();
  866. if ( ! isset($CI->$object_name))
  867. {
  868. return $this->_ci_init_class($class, '', $params, $object_name);
  869. }
  870. }
  871. log_message('debug', $class.' class already loaded. Second attempt ignored.');
  872. return;
  873. }
  874. // Does the file exist? No? Bummer...
  875. elseif ( ! file_exists($filepath))
  876. {
  877. continue;
  878. }
  879. include_once($filepath);
  880. return $this->_ci_init_class($class, '', $params, $object_name);
  881. }
  882. // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
  883. if ($subdir === '')
  884. {
  885. return $this->_ci_load_class($class.'/'.$class, $params, $object_name);
  886. }
  887. // If we got this far we were unable to find the requested class.
  888. log_message('error', 'Unable to load the requested class: '.$class);
  889. show_error('Unable to load the requested class: '.$class);
  890. }
  891. // --------------------------------------------------------------------
  892. /**
  893. * Internal CI Class Instantiator
  894. *
  895. * @used-by CI_Loader::_ci_load_class()
  896. *
  897. * @param string $class Class name
  898. * @param string $prefix Class name prefix
  899. * @param array|null|bool $config Optional configuration to pass to the class constructor:
  900. * FALSE to skip;
  901. * NULL to search in config paths;
  902. * array containing configuration data
  903. * @param string $object_name Optional object name to assign to
  904. * @return void
  905. */
  906. protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
  907. {
  908. // Is there an associated config file for this class? Note: these should always be lowercase
  909. if ($config === NULL)
  910. {
  911. // Fetch the config paths containing any package paths
  912. $config_component = $this->_ci_get_component('config');
  913. if (is_array($config_component->_config_paths))
  914. {
  915. // Break on the first found file, thus package files
  916. // are not overridden by default paths
  917. foreach ($config_component->_config_paths as $path)
  918. {
  919. // We test for both uppercase and lowercase, for servers that
  920. // are case-sensitive with regard to file names. Check for environment
  921. // first, global next
  922. if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
  923. {
  924. include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
  925. break;
  926. }
  927. elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
  928. {
  929. include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
  930. break;
  931. }
  932. elseif (file_exists($path.'config/'.strtolower($class).'.php'))
  933. {
  934. include($path.'config/'.strtolower($class).'.php');
  935. break;
  936. }
  937. elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
  938. {
  939. include($path.'config/'.ucfirst(strtolower($class)).'.php');
  940. break;
  941. }
  942. }
  943. }
  944. }
  945. if ($prefix === '')
  946. {
  947. if (class_exists('CI_'.$class, FALSE))
  948. {
  949. $name = 'CI_'.$class;
  950. }
  951. elseif (class_exists(config_item('subclass_prefix').$class, FALSE))
  952. {
  953. $name = config_item('subclass_prefix').$class;
  954. }
  955. else
  956. {
  957. $name = $class;
  958. }
  959. }
  960. else
  961. {
  962. $name = $prefix.$class;
  963. }
  964. // Is the class name valid?
  965. if ( ! class_exists($name, FALSE))
  966. {
  967. log_message('error', 'Non-existent class: '.$name);
  968. show_error('Non-existent class: '.$name);
  969. }
  970. // Set the variable name we will assign the class to
  971. // Was a custom class name supplied? If so we'll use it
  972. if (empty($object_name))
  973. {
  974. $object_name = strtolower($class);
  975. if (isset($this->_ci_varmap[$object_name]))
  976. {
  977. $object_name = $this->_ci_varmap[$object_name];
  978. }
  979. }
  980. // Don't overwrite existing properties
  981. $CI =& get_instance();
  982. if (isset($CI->$object_name))
  983. {
  984. if ($CI->$object_name instanceof $name)
  985. {
  986. log_message('debug', $class." has already been instantiated as '".$object_name."'. Second attempt aborted.");
  987. return;
  988. }
  989. show_error("Resource '".$object_name."' already exists and is not a ".$class." instance.");
  990. }
  991. // Save the class name and object name
  992. $this->_ci_classes[$object_name] = $class;
  993. // Instantiate the class
  994. $CI->$object_name = isset($config)
  995. ? new $name($config)
  996. : new $name();
  997. }
  998. // --------------------------------------------------------------------
  999. /**
  1000. * CI Autoloader
  1001. *
  1002. * Loads component listed in the config/autoload.php file.
  1003. *
  1004. * @used-by CI_Loader::initialize()
  1005. * @return void
  1006. */
  1007. protected function _ci_autoloader()
  1008. {
  1009. if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
  1010. {
  1011. include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
  1012. }
  1013. else
  1014. {
  1015. include(APPPATH.'config/autoload.php');
  1016. }
  1017. if ( ! isset($autoload))
  1018. {
  1019. return FALSE;
  1020. }
  1021. // Autoload packages
  1022. if (isset($autoload['packages']))
  1023. {
  1024. foreach ($autoload['packages'] as $package_path)
  1025. {
  1026. $this->add_package_path($package_path);
  1027. }
  1028. }
  1029. // Load any custom config file
  1030. if (count($autoload['config']) > 0)
  1031. {
  1032. $CI =& get_instance();
  1033. foreach ($autoload['config'] as $key => $val)
  1034. {
  1035. $CI->config->load($val);
  1036. }
  1037. }
  1038. // Autoload helpers and languages
  1039. foreach (array('helper', 'language') as $type)
  1040. {
  1041. if (isset($autoload[$type]) && count($autoload[$type]) > 0)
  1042. {
  1043. $this->$type($autoload[$type]);
  1044. }
  1045. }
  1046. // Autoload drivers
  1047. if (isset($autoload['drivers']))
  1048. {
  1049. foreach ($autoload['drivers'] as $item)
  1050. {
  1051. $this->driver($item);
  1052. }
  1053. }
  1054. // Load libraries
  1055. if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
  1056. {
  1057. // Load the database driver.
  1058. if (in_array('database', $autoload['libraries']))
  1059. {
  1060. $this->database();
  1061. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  1062. }
  1063. // Load all other libraries
  1064. foreach ($autoload['libraries'] as $item)
  1065. {
  1066. $this->library($item);
  1067. }
  1068. }
  1069. // Autoload models
  1070. if (isset($autoload['model']))
  1071. {
  1072. $this->model($autoload['model']);
  1073. }
  1074. }
  1075. // --------------------------------------------------------------------
  1076. /**
  1077. * CI Object to Array translator
  1078. *
  1079. * Takes an object as input and converts the class variables to
  1080. * an associative array with key/value pairs.
  1081. *
  1082. * @param object $object Object data to translate
  1083. * @return array
  1084. */
  1085. protected function _ci_object_to_array($object)
  1086. {
  1087. return is_object($object) ? get_object_vars($object) : $object;
  1088. }
  1089. // --------------------------------------------------------------------
  1090. /**
  1091. * CI Component getter
  1092. *
  1093. * Get a reference to a specific library or model.
  1094. *
  1095. * @param string $component Component name
  1096. * @return bool
  1097. */
  1098. protected function &_ci_get_component($component)
  1099. {
  1100. $CI =& get_instance();
  1101. return $CI->$component;
  1102. }
  1103. // --------------------------------------------------------------------
  1104. /**
  1105. * Prep filename
  1106. *
  1107. * This function prepares filenames of various items to
  1108. * make their loading more reliable.
  1109. *
  1110. * @param string|string[] $filename Filename(s)
  1111. * @param string $extension Filename extension
  1112. * @return array
  1113. */
  1114. protected function _ci_prep_filename($filename, $extension)
  1115. {
  1116. if ( ! is_array($filename))
  1117. {
  1118. return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
  1119. }
  1120. else
  1121. {
  1122. foreach ($filename as $key => $val)
  1123. {
  1124. $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
  1125. }
  1126. return $filename;
  1127. }
  1128. }
  1129. }
  1130. /* End of file Loader.php */
  1131. /* Location: ./system/core/Loader.php */