PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Loader.php

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