PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/ci-improved-1.0/core/CII_Loader.php

https://bitbucket.org/di3g0/code-igniter-improved
PHP | 614 lines | 350 code | 89 blank | 175 comment | 77 complexity | 0df5699d55995d4fb2a40febbd67fbd4 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class CII_Loader extends CI_Loader
  3. {
  4. // Config instance holder
  5. public $config;
  6. // Controller instance holder (set at CII_Controller)
  7. public $controller;
  8. // Global vars array for views
  9. public $vars = array();
  10. // Global vars array for views
  11. public $_use_cache = TRUE;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->config =& $this->_ci_get_component('config');
  16. $paths = array_keys( CII::paths() ); // @todo CORREGIRRRRRRRRRRRRRRRRRRRRRRRRRR!!!!!!!!!!!!!
  17. $this->_ci_library_paths = $paths;
  18. $this->_ci_helper_paths = $paths;
  19. $this->_ci_model_paths = $paths;
  20. array_shift( $paths ); // Get rid of BASEPATH
  21. $this->config->_config_paths = $paths;
  22. $this->_ci_view_paths = array_reverse( $paths );
  23. }
  24. /**
  25. * Load View used to load a "view" file
  26. *
  27. * @param string $view The name of the "view" file to be included
  28. * @param array $vars An associative array of data to be extracted for use in the view
  29. * @param bool $return Whether to return the data or load it
  30. * @param bool $cache Whether to cache $vars for future use or not
  31. * @return void|string Data loaded if $return=TRUE
  32. */
  33. function view($view, $vars = array(), $return = FALSE, $cache = TRUE )
  34. {
  35. if ( ! $cache ) $old_cache_status = $this->cache( FALSE );
  36. $rs = parent::view( $view, $vars, $return );
  37. if ( ! $cache ) $this->cache( $old_cache_status );
  38. return $rs;
  39. }
  40. /**
  41. * Load View used to load a "view" file with a full path
  42. *
  43. * @param string $path The path were the "view" file belongs
  44. * @param string $view The name of the "view" file to be included
  45. * @param array $vars An associative array of data to be extracted for use in the view
  46. * @param bool $return Whether to return the data or load it
  47. * @param bool $cache Whether to cache $vars for future use or not
  48. * @return void|string Data loaded if $return=TRUE
  49. */
  50. function viewFullPath ( $path, $view, $vars = array(), $return = FALSE, $cache = TRUE )
  51. {
  52. if ( ! $cache ) $old_cache_status = $this->cache( FALSE );
  53. $rs = $this->_ci_load(array('_ci_path' => $path, '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  54. if ( ! $cache ) $this->cache( $old_cache_status );
  55. return $rs;
  56. }
  57. /**
  58. * View loader for current theme
  59. *
  60. * @param String $view The view to load from current theme
  61. * @param Array $vars An array of variables to parse
  62. * @param Bool $return Returns as string or parses to browser
  63. * @return mixed
  64. */
  65. public function theme ( $view, $vars=array(), $return=FALSE, $cache = TRUE )
  66. {
  67. return $this->view( get_theme()."/".$view, $vars, $return, $cache );
  68. }
  69. /**
  70. * Form loader
  71. *
  72. * @param String $form The form view to load
  73. * @param Array $vars An array of variables to parse
  74. * @param Bool $return Returns as string or parses to browser
  75. * @return mixed
  76. */
  77. public function form ( $form, $vars=array(), $return=FALSE, $cache = TRUE )
  78. {
  79. return $this->view( "forms/".$form, $vars, $return, $cache );
  80. }
  81. /**
  82. * Library loader
  83. *
  84. * @param String $library The library file name to load
  85. * @param Mixed $params Optional parameters for class instance
  86. * If FALSE or NULL, then just includes the file
  87. * @param String $object_name An optional object name
  88. * @return void
  89. */
  90. public function library ( $library, $params = NULL, $object_name = NULL )
  91. {
  92. if ( is_array($library) )
  93. {
  94. foreach ( $library as $class )
  95. $this->library( $class, $params );
  96. return;
  97. }
  98. if ( $library == '' || isset($this->_base_classes[$library]) )
  99. return TRUE;
  100. // Safety: Was the class already loaded by a previous call?
  101. if ( in_array($library,$this->_ci_loaded_files) )
  102. {
  103. // Before we deem this to be a duplicate request, let's see
  104. // if a custom object name is being supplied. If so, we'll
  105. // return a new instance of the object
  106. if ( ! is_null($object_name) )
  107. {
  108. $CI =& get_instance();
  109. if ( ! isset($CI->$object_name) )
  110. return $this->_ci_init_class( $this->_ci_library_paths, 'libraries/'.$library, $params, $object_name );
  111. }
  112. log_message('debug', $library." class already loaded.");
  113. return TRUE;
  114. }
  115. $rs = $this->_ci_init_class( $this->_ci_library_paths, 'libraries/'.$library, $params, $object_name );
  116. if ( ! $rs )
  117. show_error('Unable to locate the library you have specified: '.$library);
  118. $this->_ci_loaded_files[] = $library;
  119. return $rs;
  120. }
  121. /**
  122. * Model Loader
  123. *
  124. * @param String $model The library file name to load
  125. * @param String $name Optional name for the model
  126. * @param Bool $db_con Database connection
  127. * @param Bool $instantiate Whether to instantiate the class or just include it
  128. * @return void
  129. */
  130. function model ( $model, $name = NULL, $params = array(), $db_conn = FALSE )
  131. {
  132. if ( is_array($model) )
  133. {
  134. foreach ( $model as $babe )
  135. $this->model( $babe, $name, $params, $db_conn );
  136. return;
  137. }
  138. $CI =& get_instance();
  139. if ( $name && isset($CI->$name) )
  140. show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
  141. // Is the model in a sub-folder? If so, parse out the filename and path.
  142. $path = '';
  143. if (($last_slash = strrpos($model, '/')) !== FALSE)
  144. {
  145. // The path is in front of the last slash
  146. $path = substr($model, 0, $last_slash + 1);
  147. // And the model name behind it
  148. $model = substr($model, $last_slash + 1);
  149. }
  150. $instanciate = FALSE;
  151. if ( $name )
  152. $instanciate = TRUE;
  153. else
  154. $name = $model;
  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. load_class('Model', 'core', NULL, FALSE );
  165. // We don't need an instance!
  166. if ( ! $instanciate )
  167. return _load_class( CII::paths(), $model, 'models/'.$path, FALSE );
  168. // OK, let's instanciate the model
  169. $instance = _load_class( CII::paths(), $model, 'models/'.$path, $params );
  170. if ( ! $instance ) // couldn't find the model
  171. show_error('Unable to locate the model you have specified: '.$model);
  172. $this->_ci_models[] = $name;
  173. return $instance;
  174. }
  175. /**
  176. * Load Helper
  177. *
  178. * This function loads the specified helper file.
  179. *
  180. * @access public
  181. * @param mixed
  182. * @return void
  183. */
  184. function helper ( $helpers = array() )
  185. {
  186. foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
  187. {
  188. if (isset($this->_ci_helpers[$helper]))
  189. continue;
  190. if ( _load_class( array_reverse(CII::paths()), $helper, 'helpers', FALSE ) )
  191. {
  192. $this->_ci_helpers[$helper] = TRUE;
  193. log_message('debug', 'Helper loaded: '.$helper);
  194. }
  195. // unable to load the helper
  196. if ( ! isset($this->_ci_helpers[$helper]))
  197. show_error('Unable to load the requested file: helpers/'.$helper.EXT);
  198. }
  199. }
  200. /**
  201. * Database Loader
  202. *
  203. * @access public
  204. * @param string the DB credentials
  205. * @param bool whether to return the DB object
  206. * @param bool whether to enable active record (this allows us to override the config setting)
  207. * @return object
  208. */
  209. function database( $params = '', $return = FALSE, $active_record = NULL , $new_instance = FALSE )
  210. {
  211. // Grab the super object
  212. $CI =& get_instance();
  213. // Do we even need to load the database class?
  214. if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
  215. {
  216. return FALSE;
  217. }
  218. // Include the DB main function
  219. load_one_file( 'DB', 'database' );
  220. if ($return === TRUE)
  221. {
  222. return DB($params, $active_record, $new_instance);
  223. }
  224. // Initialize the db variable. Needed to prevent
  225. // reference errors with some configurations
  226. $CI->db = '';
  227. // Load the DB class
  228. $CI->db =& DB($params, $active_record, $new_instance);
  229. }
  230. /**
  231. * Driver
  232. *
  233. * Loads a driver library
  234. *
  235. * @param string the name of the class
  236. * @param mixed the optional parameters
  237. * @param string an optional object name
  238. * @return void
  239. */
  240. function driver($library = '', $params = NULL, $object_name = NULL)
  241. {
  242. if ( ! class_exists('CI_Driver_Library'))
  243. {
  244. // we aren't instantiating an object here, that'll be done by the Library itself
  245. load_file( 'Driver', 'libraries' );
  246. }
  247. // We can save the loader some time since Drivers will *always* be in a subfolder,
  248. // and typically identically named to the library
  249. if ( ! strpos($library, '/'))
  250. {
  251. $library = ucfirst($library).'/'.$library;
  252. }
  253. return $this->library($library, $params, $object_name);
  254. }
  255. /**
  256. * Set Variables
  257. *
  258. * Once variables are set they become available within
  259. * the controller class and its "view" files.
  260. *
  261. * @param array
  262. * @return array
  263. */
  264. function vars( $vars = array(), $val = '' )
  265. {
  266. if ( ! $vars )
  267. return $this->_ci_cached_vars;
  268. if ( ! $val && is_string($vars) )
  269. return $this->_ci_cached_vars[ $vars ];
  270. return parent::vars( $vars, $val );
  271. }
  272. /**
  273. * Enables or disables the internal vars cache
  274. *
  275. * @param bool $enable
  276. * @return bool The original cache status
  277. */
  278. function cache ( $enable )
  279. {
  280. $old = $this->_use_cache;
  281. $this->_use_cache = (bool)$enable;
  282. return $old;
  283. }
  284. function _ci_load ( $_ci_data )
  285. {
  286. // Set the default data variables
  287. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
  288. {
  289. $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
  290. }
  291. // Set the path to the requested file
  292. if ($_ci_path == '')
  293. {
  294. $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
  295. $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
  296. foreach ( $this->_ci_view_paths as $path )
  297. {
  298. $_ci_path = $path . 'views/' . $_ci_file;
  299. if ( file_exists($_ci_path) )
  300. break;
  301. }
  302. }
  303. else if ( $_ci_path && $_ci_view ) // Full path support
  304. {
  305. $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
  306. $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
  307. $_ci_path .= "/".$_ci_file;
  308. }
  309. else
  310. {
  311. $_ci_x = explode('/', $_ci_path);
  312. $_ci_file = end($_ci_x);
  313. }
  314. if ( ! file_exists($_ci_path))
  315. {
  316. show_error('Unable to load the requested file: '.$_ci_file);
  317. }
  318. // This allows anything loaded using $this->load (views, files, etc.)
  319. // to become accessible from within the Controller and Model functions.
  320. $_ci_CI =& get_instance();
  321. foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
  322. {
  323. if ( ! isset($this->$_ci_key))
  324. {
  325. $this->$_ci_key =& $_ci_CI->$_ci_key;
  326. }
  327. }
  328. /*
  329. * Extract and cache variables
  330. *
  331. * You can either set variables using the dedicated $this->load_vars()
  332. * function or via the second parameter of this function. We'll merge
  333. * the two types and cache them so that views that are embedded within
  334. * other views can have access to these variables.
  335. */
  336. if ( $this->_use_cache && is_array($_ci_vars) )
  337. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  338. if ( ( $vars = ($this->_use_cache ? $this->_ci_cached_vars : $_ci_vars) ) )
  339. extract( $vars );unset( $vars );
  340. /*
  341. * Buffer the output
  342. *
  343. * We buffer the output for two reasons:
  344. * 1. Speed. You get a significant speed boost.
  345. * 2. So that the final rendered template can be
  346. * post-processed by the output class. Why do we
  347. * need post processing? For one thing, in order to
  348. * show the elapsed page load time. Unless we
  349. * can intercept the content right before it's sent to
  350. * the browser and then stop the timer it won't be accurate.
  351. */
  352. ob_start();
  353. // If the PHP installation does not support short tags we'll
  354. // do a little string replacement, changing the short tags
  355. // to standard PHP echo statements.
  356. if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
  357. {
  358. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  359. }
  360. else
  361. {
  362. include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  363. }
  364. log_message('debug', 'File loaded: '.$_ci_path);
  365. // Return the file data if requested
  366. if ($_ci_return === TRUE)
  367. {
  368. $buffer = ob_get_contents();
  369. @ob_end_clean();
  370. return $buffer;
  371. }
  372. /*
  373. * Flush the buffer... or buff the flusher?
  374. *
  375. * In order to permit views to be nested within
  376. * other views, we need to flush the content back out whenever
  377. * we are beyond the first level of output buffering so that
  378. * it can be seen and included properly by the first included
  379. * template and any subsequent ones. Oy!
  380. *
  381. */
  382. if (ob_get_level() > $this->_ci_ob_level + 1)
  383. {
  384. ob_end_flush();
  385. }
  386. else
  387. {
  388. $_ci_CI->output->append_output(ob_get_contents());
  389. @ob_end_clean();
  390. }
  391. return TRUE;
  392. }
  393. function _ci_init_class ( $paths, $class, $config = NULL, $object_name = NULL )
  394. {
  395. list( $path, $class ) = $this->_get_path_class( $class );
  396. $name = strtolower( $class );
  397. // Is there an associated config file for this class? Note: these should always be lowercase
  398. if ( $config === NULL )
  399. {
  400. foreach ( (array)$this->config->_config_paths as $cpath )
  401. {
  402. if ( defined('ENVIRONMENT') && file_exists($cpath .'config/'.ENVIRONMENT.'/'.$name.EXT) )
  403. require( $cpath .'config/'.ENVIRONMENT.'/'.$name.EXT );
  404. else if ( file_exists($cpath .'config/'.$name.EXT) )
  405. require( $cpath .'config/'.$name.EXT );
  406. }
  407. if ( ! $config && ${$name} )
  408. $config = ${$name};
  409. }
  410. if ( $config ) // Just one parameter for class constructor (an array with config values)
  411. $config = array( $config );
  412. else if ( $object_name !== FALSE )
  413. $config = array();
  414. $scopes = CII::paths();
  415. foreach ( (array)$paths as $apath )
  416. if ( $scopes[$apath] )
  417. $load_paths[ $apath ] = $scopes[ $apath ];
  418. $instance =& _load_class( $load_paths, ucfirst($class), $path, $config );
  419. if ( ! $instance ) // No instance found? Try without ucfirst!
  420. $instance =& _load_class( $load_paths, $class, $path, $config );
  421. if ( ! $instance )
  422. {
  423. log_message('error', "Non-existent class: ".$path.$class);
  424. return $instance;
  425. }
  426. // Set the variable name we will assign the class to
  427. // Was a custom class name supplied? If so we'll use it
  428. $class = strtolower($class);
  429. if ( $object_name === FALSE ) // Just load the class, don't instanciate!
  430. return TRUE;
  431. if (is_null($object_name))
  432. {
  433. $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
  434. }
  435. else
  436. {
  437. $classvar = $object_name;
  438. }
  439. // Save the class name and object name
  440. $this->_ci_classes[$class] = $classvar;
  441. // Map the class
  442. $CI =& get_instance();
  443. $CI->$classvar = $instance;
  444. return $instance;
  445. }
  446. function _ci_autoloader ( )
  447. {
  448. $autoload =& load_file_vars( 'autoload', 'config'.(defined('ENVIRONMENT')?'/'.ENVIRONMENT:'') );
  449. if ( ! isset($autoload) )
  450. return FALSE;
  451. // Autoload packages
  452. if ( isset($autoload['packages']) )
  453. foreach ( $autoload['packages'] as $package_path )
  454. $this->add_package_path( $package_path );
  455. // Load any custom config file
  456. if ( count($autoload['config']) > 0 )
  457. {
  458. $CI =& get_instance();
  459. foreach ( $autoload['config'] as $key => $val )
  460. $CI->config->load($val);
  461. }
  462. // Autoload helpers and languages
  463. foreach ( array('helper', 'language') as $type )
  464. if ( isset($autoload[$type]) AND count($autoload[$type]) > 0 )
  465. $this->$type( $autoload[$type] );
  466. // A little tweak to remain backward compatible
  467. // The $autoload['core'] item was deprecated
  468. if ( ! isset($autoload['libraries']) AND isset($autoload['core']) )
  469. $autoload['libraries'] = $autoload['core'];
  470. // Load libraries
  471. if ( isset($autoload['libraries']) AND count($autoload['libraries']) > 0 )
  472. {
  473. // Load the database driver.
  474. if ( in_array('database', $autoload['libraries']) )
  475. {
  476. $this->database();
  477. $autoload['libraries'] = array_diff( $autoload['libraries'], array('database') );
  478. }
  479. // Load all other libraries
  480. foreach ( $autoload['libraries'] as $item )
  481. $this->library($item);
  482. }
  483. // Autoload models
  484. if ( isset($autoload['model']) )
  485. $this->model( $autoload['model'] );
  486. }
  487. function add_package_path ( $path )
  488. {
  489. $path = rtrim($path, '/').'/';
  490. $this->_ci_library_paths[ ] = $path;
  491. $this->_ci_model_paths[ ] = $path;
  492. $this->_ci_helper_paths[ ] = $path;
  493. // Add config file path
  494. $this->config->_config_paths[ ] = $path;
  495. }
  496. function remove_package_path ( $path, $remove_config_path = TRUE )
  497. {
  498. $path = rtrim($path, '/').'/';
  499. foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
  500. if ( ($pos=array_search($path,$this->{$var})) !== FALSE )
  501. unset( $this->{$var}[ $pos ] );
  502. if ( ($pos=array_search($path,$this->config->_config_paths)) !== FALSE )
  503. unset( $this->config->_config_paths[$pos] );
  504. // make sure the application default paths are still in the array
  505. $paths = CII::paths();
  506. $this->_ci_library_paths = array_keys(array_unique(array_merge($this->_ci_library_paths, $paths)));
  507. $this->_ci_helper_paths = array_keys(array_unique(array_merge($this->_ci_helper_paths, $paths)));
  508. $this->_ci_model_paths = array_keys(array_unique(array_merge($this->_ci_model_paths, $paths)));
  509. array_shift( $paths ); // Get rid of BASEPATH
  510. $this->config->_config_paths = array_keys(array_unique(array_merge($this->config->_config_paths, $paths)));
  511. $this->_ci_view_paths = array_reverse(array_keys(array_unique(array_merge($this->_ci_view_paths, $paths))));
  512. }
  513. private function _get_path_class ( $class )
  514. {
  515. $path = '';
  516. if ( ($last_slash = strrpos($class, '/')) !== FALSE )
  517. {
  518. // The path is in front of the last slash
  519. $path = substr( $class, 0, $last_slash + 1);
  520. // And the class name behind it
  521. $class = substr( $class, $last_slash + 1);
  522. }
  523. return array( trim($path,'/'), $class );
  524. }
  525. }