Advertisement
Guest User

De bestand

a guest
Jan 18th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.33 KB | None | 0 0
  1. public function language($file = array(), $lang = '')
  2. {
  3. $CI =& get_instance();
  4.  
  5. if ( ! is_array($file))
  6. {
  7. $file = array($file);
  8. }
  9.  
  10. foreach ($file as $langfile)
  11. {
  12. $CI->lang->load($langfile, $lang);
  13. }
  14. }
  15.  
  16. // --------------------------------------------------------------------
  17.  
  18. /**
  19. * Loads a config file
  20. *
  21. * @param string
  22. * @param bool
  23. * @param bool
  24. * @return void
  25. */
  26. public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  27. {
  28. $CI =& get_instance();
  29. $CI->config->load($file, $use_sections, $fail_gracefully);
  30. }
  31.  
  32. // --------------------------------------------------------------------
  33.  
  34. /**
  35. * Driver
  36. *
  37. * Loads a driver library
  38. *
  39. * @param string the name of the class
  40. * @param mixed the optional parameters
  41. * @param string an optional object name
  42. * @return void
  43. */
  44. public function driver($library = '', $params = NULL, $object_name = NULL)
  45. {
  46. if ( ! class_exists('CI_Driver_Library'))
  47. {
  48. // we aren't instantiating an object here, that'll be done by the Library itself
  49. require BASEPATH.'libraries/Driver.php';
  50. }
  51.  
  52. if ($library == '')
  53. {
  54. return FALSE;
  55. }
  56.  
  57. // We can save the loader some time since Drivers will *always* be in a subfolder,
  58. // and typically identically named to the library
  59. if ( ! strpos($library, '/'))
  60. {
  61. $library = ucfirst($library).'/'.$library;
  62. }
  63.  
  64. return $this->library($library, $params, $object_name);
  65. }
  66.  
  67. // --------------------------------------------------------------------
  68.  
  69. /**
  70. * Add Package Path
  71. *
  72. * Prepends a parent path to the library, model, helper, and config path arrays
  73. *
  74. * @param string
  75. * @param boolean
  76. * @return void
  77. */
  78. public function add_package_path($path, $view_cascade=TRUE)
  79. {
  80. $path = rtrim($path, '/').'/';
  81.  
  82. array_unshift($this->_ci_library_paths, $path);
  83. array_unshift($this->_ci_model_paths, $path);
  84. array_unshift($this->_ci_helper_paths, $path);
  85.  
  86. $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
  87.  
  88. // Add config file path
  89. $config =& $this->_ci_get_component('config');
  90. array_unshift($config->_config_paths, $path);
  91. }
  92.  
  93. // --------------------------------------------------------------------
  94.  
  95. /**
  96. * Get Package Paths
  97. *
  98. * Return a list of all package paths, by default it will ignore BASEPATH.
  99. *
  100. * @param string
  101. * @return void
  102. */
  103. public function get_package_paths($include_base = FALSE)
  104. {
  105. return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
  106. }
  107.  
  108. // --------------------------------------------------------------------
  109.  
  110. /**
  111. * Remove Package Path
  112. *
  113. * Remove a path from the library, model, and helper path arrays if it exists
  114. * If no path is provided, the most recently added path is removed.
  115. *
  116. * @param type
  117. * @param bool
  118. * @return type
  119. */
  120. public function remove_package_path($path = '', $remove_config_path = TRUE)
  121. {
  122. $config =& $this->_ci_get_component('config');
  123.  
  124. if ($path == '')
  125. {
  126. $void = array_shift($this->_ci_library_paths);
  127. $void = array_shift($this->_ci_model_paths);
  128. $void = array_shift($this->_ci_helper_paths);
  129. $void = array_shift($this->_ci_view_paths);
  130. $void = array_shift($config->_config_paths);
  131. }
  132. else
  133. {
  134. $path = rtrim($path, '/').'/';
  135. foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
  136. {
  137. if (($key = array_search($path, $this->{$var})) !== FALSE)
  138. {
  139. unset($this->{$var}[$key]);
  140. }
  141. }
  142.  
  143. if (isset($this->_ci_view_paths[$path.'views/']))
  144. {
  145. unset($this->_ci_view_paths[$path.'views/']);
  146. }
  147.  
  148. if (($key = array_search($path, $config->_config_paths)) !== FALSE)
  149. {
  150. unset($config->_config_paths[$key]);
  151. }
  152. }
  153.  
  154. // make sure the application default paths are still in the array
  155. $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
  156. $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
  157. $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
  158. $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
  159. $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
  160. }
  161.  
  162. // --------------------------------------------------------------------
  163.  
  164. /**
  165. * Loader
  166. *
  167. * This function is used to load views and files.
  168. * Variables are prefixed with _ci_ to avoid symbol collision with
  169. * variables made available to view files
  170. *
  171. * @param array
  172. * @return void
  173. */
  174. protected function _ci_load($_ci_data)
  175. {
  176. // Set the default data variables
  177. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
  178. {
  179. $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
  180. }
  181.  
  182. $file_exists = FALSE;
  183.  
  184. // Set the path to the requested file
  185. if ($_ci_path != '')
  186. {
  187. $_ci_x = explode('/', $_ci_path);
  188. $_ci_file = end($_ci_x);
  189. }
  190. else
  191. {
  192. $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
  193. $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
  194.  
  195. foreach ($this->_ci_view_paths as $view_file => $cascade)
  196. {
  197. if (file_exists($view_file.$_ci_file))
  198. {
  199. $_ci_path = $view_file.$_ci_file;
  200. $file_exists = TRUE;
  201. break;
  202. }
  203.  
  204. if ( ! $cascade)
  205. {
  206. break;
  207. }
  208. }
  209. }
  210.  
  211. if ( ! $file_exists && ! file_exists($_ci_path))
  212. {
  213. show_error('Unable to load the requested file: '.$_ci_file);
  214. }
  215.  
  216. // This allows anything loaded using $this->load (views, files, etc.)
  217. // to become accessible from within the Controller and Model functions.
  218.  
  219. $_ci_CI =& get_instance();
  220. foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
  221. {
  222. if ( ! isset($this->$_ci_key))
  223. {
  224. $this->$_ci_key =& $_ci_CI->$_ci_key;
  225. }
  226. }
  227.  
  228. /*
  229. * Extract and cache variables
  230. *
  231. * You can either set variables using the dedicated $this->load_vars()
  232. * function or via the second parameter of this function. We'll merge
  233. * the two types and cache them so that views that are embedded within
  234. * other views can have access to these variables.
  235. */
  236. if (is_array($_ci_vars))
  237. {
  238. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  239. }
  240. extract($this->_ci_cached_vars);
  241.  
  242. /*
  243. * Buffer the output
  244. *
  245. * We buffer the output for two reasons:
  246. * 1. Speed. You get a significant speed boost.
  247. * 2. So that the final rendered template can be
  248. * post-processed by the output class. Why do we
  249. * need post processing? For one thing, in order to
  250. * show the elapsed page load time. Unless we
  251. * can intercept the content right before it's sent to
  252. * the browser and then stop the timer it won't be accurate.
  253. */
  254. ob_start();
  255.  
  256. // If the PHP installation does not support short tags we'll
  257. // do a little string replacement, changing the short tags
  258. // to standard PHP echo statements.
  259.  
  260. if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
  261. {
  262. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  263. }
  264. else
  265. {
  266. include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  267. }
  268.  
  269. log_message('debug', 'File loaded: '.$_ci_path);
  270.  
  271. // Return the file data if requested
  272. if ($_ci_return === TRUE)
  273. {
  274. $buffer = ob_get_contents();
  275. @ob_end_clean();
  276. return $buffer;
  277. }
  278.  
  279. /*
  280. * Flush the buffer... or buff the flusher?
  281. *
  282. * In order to permit views to be nested within
  283. * other views, we need to flush the content back out whenever
  284. * we are beyond the first level of output buffering so that
  285. * it can be seen and included properly by the first included
  286. * template and any subsequent ones. Oy!
  287. *
  288. */
  289. if (ob_get_level() > $this->_ci_ob_level + 1)
  290. {
  291. ob_end_flush();
  292. }
  293. else
  294. {
  295. $_ci_CI->output->append_output(ob_get_contents());
  296. @ob_end_clean();
  297. }
  298. }
  299.  
  300. // --------------------------------------------------------------------
  301.  
  302. /**
  303. * Load class
  304. *
  305. * This function loads the requested class.
  306. *
  307. * @param string the item that is being loaded
  308. * @param mixed any additional parameters
  309. * @param string an optional object name
  310. * @return void
  311. */
  312. protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
  313. {
  314. // Get the class name, and while we're at it trim any slashes.
  315. // The directory path can be included as part of the class name,
  316. // but we don't want a leading slash
  317. $class = str_replace('.php', '', trim($class, '/'));
  318.  
  319. // Was the path included with the class name?
  320. // We look for a slash to determine this
  321. $subdir = '';
  322. if (($last_slash = strrpos($class, '/')) !== FALSE)
  323. {
  324. // Extract the path
  325. $subdir = substr($class, 0, $last_slash + 1);
  326.  
  327. // Get the filename from the path
  328. $class = substr($class, $last_slash + 1);
  329. }
  330.  
  331. // We'll test for both lowercase and capitalized versions of the file name
  332. foreach (array(ucfirst($class), strtolower($class)) as $class)
  333. {
  334. $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
  335.  
  336. // Is this a class extension request?
  337. if (file_exists($subclass))
  338. {
  339. $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
  340.  
  341. if ( ! file_exists($baseclass))
  342. {
  343. log_message('error', "Unable to load the requested class: ".$class);
  344. show_error("Unable to load the requested class: ".$class);
  345. }
  346.  
  347. // Safety: Was the class already loaded by a previous call?
  348. if (in_array($subclass, $this->_ci_loaded_files))
  349. {
  350. // Before we deem this to be a duplicate request, let's see
  351. // if a custom object name is being supplied. If so, we'll
  352. // return a new instance of the object
  353. if ( ! is_null($object_name))
  354. {
  355. $CI =& get_instance();
  356. if ( ! isset($CI->$object_name))
  357. {
  358. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  359. }
  360. }
  361.  
  362. $is_duplicate = TRUE;
  363. log_message('debug', $class." class already loaded. Second attempt ignored.");
  364. return;
  365. }
  366.  
  367. include_once($baseclass);
  368. include_once($subclass);
  369. $this->_ci_loaded_files[] = $subclass;
  370.  
  371. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  372. }
  373.  
  374. // Lets search for the requested library file and load it.
  375. $is_duplicate = FALSE;
  376. foreach ($this->_ci_library_paths as $path)
  377. {
  378. $filepath = $path.'libraries/'.$subdir.$class.'.php';
  379.  
  380. // Does the file exist? No? Bummer...
  381. if ( ! file_exists($filepath))
  382. {
  383. continue;
  384. }
  385.  
  386. // Safety: Was the class already loaded by a previous call?
  387. if (in_array($filepath, $this->_ci_loaded_files))
  388. {
  389. // Before we deem this to be a duplicate request, let's see
  390. // if a custom object name is being supplied. If so, we'll
  391. // return a new instance of the object
  392. if ( ! is_null($object_name))
  393. {
  394. $CI =& get_instance();
  395. if ( ! isset($CI->$object_name))
  396. {
  397. return $this->_ci_init_class($class, '', $params, $object_name);
  398. }
  399. }
  400.  
  401. $is_duplicate = TRUE;
  402. log_message('debug', $class." class already loaded. Second attempt ignored.");
  403. return;
  404. }
  405.  
  406. include_once($filepath);
  407. $this->_ci_loaded_files[] = $filepath;
  408. return $this->_ci_init_class($class, '', $params, $object_name);
  409. }
  410.  
  411. } // END FOREACH
  412.  
  413. // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
  414. if ($subdir == '')
  415. {
  416. $path = strtolower($class).'/'.$class;
  417. return $this->_ci_load_class($path, $params);
  418. }
  419.  
  420. // If we got this far we were unable to find the requested class.
  421. // We do not issue errors if the load call failed due to a duplicate request
  422. if ($is_duplicate == FALSE)
  423. {
  424. log_message('error', "Unable to load the requested class: ".$class);
  425. show_error("Unable to load the requested class: ".$class);
  426. }
  427. }
  428.  
  429. // --------------------------------------------------------------------
  430.  
  431. /**
  432. * Instantiates a class
  433. *
  434. * @param string
  435. * @param string
  436. * @param bool
  437. * @param string an optional object name
  438. * @return null
  439. */
  440. protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
  441. {
  442. // Is there an associated config file for this class? Note: these should always be lowercase
  443. if ($config === NULL)
  444. {
  445. // Fetch the config paths containing any package paths
  446. $config_component = $this->_ci_get_component('config');
  447.  
  448. if (is_array($config_component->_config_paths))
  449. {
  450. // Break on the first found file, thus package files
  451. // are not overridden by default paths
  452. foreach ($config_component->_config_paths as $path)
  453. {
  454. // We test for both uppercase and lowercase, for servers that
  455. // are case-sensitive with regard to file names. Check for environment
  456. // first, global next
  457. if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
  458. {
  459. include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
  460. break;
  461. }
  462. elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
  463. {
  464. include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
  465. break;
  466. }
  467. elseif (file_exists($path .'config/'.strtolower($class).'.php'))
  468. {
  469. include($path .'config/'.strtolower($class).'.php');
  470. break;
  471. }
  472. elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
  473. {
  474. include($path .'config/'.ucfirst(strtolower($class)).'.php');
  475. break;
  476. }
  477. }
  478. }
  479. }
  480.  
  481. if ($prefix == '')
  482. {
  483. if (class_exists('CI_'.$class))
  484. {
  485. $name = 'CI_'.$class;
  486. }
  487. elseif (class_exists(config_item('subclass_prefix').$class))
  488. {
  489. $name = config_item('subclass_prefix').$class;
  490. }
  491. else
  492. {
  493. $name = $class;
  494. }
  495. }
  496. else
  497. {
  498. $name = $prefix.$class;
  499. }
  500.  
  501. // Is the class name valid?
  502. if ( ! class_exists($name))
  503. {
  504. log_message('error', "Non-existent class: ".$name);
  505. show_error("Non-existent class: ".$class);
  506. }
  507.  
  508. // Set the variable name we will assign the class to
  509. // Was a custom class name supplied? If so we'll use it
  510. $class = strtolower($class);
  511.  
  512. if (is_null($object_name))
  513. {
  514. $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
  515. }
  516. else
  517. {
  518. $classvar = $object_name;
  519. }
  520.  
  521. // Save the class name and object name
  522. $this->_ci_classes[$class] = $classvar;
  523.  
  524. // Instantiate the class
  525. $CI =& get_instance();
  526. if ($config !== NULL)
  527. {
  528. $CI->$classvar = new $name($config);
  529. }
  530. else
  531. {
  532. $CI->$classvar = new $name;
  533. }
  534. }
  535.  
  536. // --------------------------------------------------------------------
  537.  
  538. /**
  539. * Autoloader
  540. *
  541. * The config/autoload.php file contains an array that permits sub-systems,
  542. * libraries, and helpers to be loaded automatically.
  543. *
  544. * @param array
  545. * @return void
  546. */
  547. private function _ci_autoloader()
  548. {
  549. if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
  550. {
  551. include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
  552. }
  553. else
  554. {
  555. include(APPPATH.'config/autoload.php');
  556. }
  557.  
  558. if ( ! isset($autoload))
  559. {
  560. return FALSE;
  561. }
  562.  
  563. // Autoload packages
  564. if (isset($autoload['packages']))
  565. {
  566. foreach ($autoload['packages'] as $package_path)
  567. {
  568. $this->add_package_path($package_path);
  569. }
  570. }
  571.  
  572. // Load any custom config file
  573. if (count($autoload['config']) > 0)
  574. {
  575. $CI =& get_instance();
  576. foreach ($autoload['config'] as $key => $val)
  577. {
  578. $CI->config->load($val);
  579. }
  580. }
  581.  
  582. // Autoload helpers and languages
  583. foreach (array('helper', 'language') as $type)
  584. {
  585. if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
  586. {
  587. $this->$type($autoload[$type]);
  588. }
  589. }
  590.  
  591. // A little tweak to remain backward compatible
  592. // The $autoload['core'] item was deprecated
  593. if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
  594. {
  595. $autoload['libraries'] = $autoload['core'];
  596. }
  597.  
  598. // Load libraries
  599. if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
  600. {
  601. // Load the database driver.
  602. if (in_array('database', $autoload['libraries']))
  603. {
  604. $this->database();
  605. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  606. }
  607.  
  608. // Load all other libraries
  609. foreach ($autoload['libraries'] as $item)
  610. {
  611. $this->library($item);
  612. }
  613. }
  614.  
  615. // Autoload models
  616. if (isset($autoload['model']))
  617. {
  618. $this->model($autoload['model']);
  619. }
  620. }
  621.  
  622. // --------------------------------------------------------------------
  623.  
  624. /**
  625. * Object to Array
  626. *
  627. * Takes an object as input and converts the class variables to array key/vals
  628. *
  629. * @param object
  630. * @return array
  631. */
  632. protected function _ci_object_to_array($object)
  633. {
  634. return (is_object($object)) ? get_object_vars($object) : $object;
  635. }
  636.  
  637. // --------------------------------------------------------------------
  638.  
  639. /**
  640. * Get a reference to a specific library or model
  641. *
  642. * @param string
  643. * @return bool
  644. */
  645. protected function &_ci_get_component($component)
  646. {
  647. $CI =& get_instance();
  648. return $CI->$component;
  649. }
  650.  
  651. // --------------------------------------------------------------------
  652.  
  653. /**
  654. * Prep filename
  655. *
  656. * This function preps the name of various items to make loading them more reliable.
  657. *
  658. * @param mixed
  659. * @param string
  660. * @return array
  661. */
  662. protected function _ci_prep_filename($filename, $extension)
  663. {
  664. if ( ! is_array($filename))
  665. {
  666. return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
  667. }
  668. else
  669. {
  670. foreach ($filename as $key => $val)
  671. {
  672. $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
  673. }
  674.  
  675. return $filename;
  676. }
  677. }
  678. }
  679.  
  680. /* End of file Loader.php */
  681. /* Location: ./system/core/Loader.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement