/halogy/libraries/Loader.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 1085 lines · 568 code · 147 blank · 370 comment · 84 complexity · 7caa9fdd75741a45e48c9d477f2db901 MD5 · raw file

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