Loader.php 37 KB

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