PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/application/third_party/fuel/Loader.php

https://github.com/swampthang/FUEL-CMS
PHP | 453 lines | 244 code | 87 blank | 122 comment | 54 complexity | ba352b9a0e0a93ea1526121e05c6e675 MD5 | raw file
  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @link http://www.getfuelcms.com
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * Some additions to the Awesome Modular Extension Library mostly for Matchbox compatibility
  16. *
  17. * This Library overides the original MX Loader library
  18. *
  19. * @package FUEL CMS
  20. * @subpackage Third Party
  21. * @category Third Party
  22. * @author Changes by David McReynolds @ Daylight Studio. Original Author info is below
  23. */
  24. /**
  25. * Modular Extensions - HMVC
  26. *
  27. * Adapted from the CodeIgniter Core Classes
  28. * @link http://codeigniter.com
  29. *
  30. * Description:
  31. * This library extends the CodeIgniter CI_Loader class
  32. * and adds features allowing use of modules and the HMVC design pattern.
  33. *
  34. * Install this file as application/third_party/MX/Loader.php
  35. *
  36. * @copyright Copyright (c) Wiredesignz 2010-11-12
  37. * @version 5.3.5
  38. *
  39. * Permission is hereby granted, free of charge, to any person obtaining a copy
  40. * of this software and associated documentation files (the "Software"), to deal
  41. * in the Software without restriction, including without limitation the rights
  42. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  43. * copies of the Software, and to permit persons to whom the Software is
  44. * furnished to do so, subject to the following conditions:
  45. *
  46. * The above copyright notice and this permission notice shall be included in
  47. * all copies or substantial portions of the Software.
  48. *
  49. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  50. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  51. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  52. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  53. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  54. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  55. * THE SOFTWARE.
  56. **/
  57. require_once(APPPATH.'third_party/MX/Loader.php');
  58. class Fuel_Loader extends MX_Loader
  59. {
  60. private $_module;
  61. public $_ci_plugins;
  62. public function __construct() {
  63. parent::__construct();
  64. /* set the module name for Modular Separation */
  65. $this->_module = CI::$APP->router->fetch_module();
  66. }
  67. /****************************************************************************
  68. METHODS FOR MATCHBOX COMPATIBILITY
  69. ****************************************************************************/
  70. /** Load config Matchbox style for backwards compatability **/
  71. public function module_config($module, $file = '', $use_sections = FALSE, $fail_gracefully = FALSE) {
  72. return $this->config($file, $use_sections, $fail_gracefully, $module);
  73. }
  74. /** Load helper Matchbox style for backwards compatability **/
  75. public function module_helper($module, $helper)
  76. {
  77. return $this->helper($helper, $module);
  78. }
  79. /** Load Language Matchbox style for backwards compatability **/
  80. public function module_language($module, $langfile, $lang = '', $return = FALSE) {
  81. return $this->language($langfile, $lang, $return, $module);
  82. }
  83. /** Load Library Matchbox style for backwards compatability **/
  84. public function module_library($module, $library, $params = array(), $object_name = NULL)
  85. {
  86. return $this->library($library, $params, $object_name, $module);
  87. }
  88. /** Load Model Matchbox style for backwards compatability **/
  89. public function module_model($module, $model, $object_name = NULL, $connect = FALSE)
  90. {
  91. return $this->model($model, $object_name, $connect, $module);
  92. }
  93. /** Load view Matchbox style for backwards compatability **/
  94. public function module_view($module, $view, $vars = array(), $return = FALSE)
  95. {
  96. return $this->view($view, $vars, $return, $module);
  97. }
  98. /****************************************************************************
  99. OVERWRITTEN METHODS SO WE COULD ADD THE $module parameter
  100. ****************************************************************************/
  101. /** Load a module config file **/
  102. public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE, $module = NULL) {
  103. if (!isset($module)) $module = $this->_module; // FUEL
  104. return CI::$APP->config->load($file, $use_sections, $fail_gracefully, $module);
  105. }
  106. /** Load the database drivers **/
  107. public function database($params = '', $return = FALSE, $active_record = NULL) {
  108. if (class_exists('CI_DB', FALSE) AND $return == FALSE AND $active_record == NULL)
  109. return;
  110. require_once BASEPATH.'database/DB'.EXT;
  111. $db = DB($params, $active_record);
  112. // <!-- FUEL
  113. $my_driver = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
  114. $my_driver_file = APPPATH.'core/'.$my_driver.EXT;
  115. if (file_exists($my_driver_file))
  116. {
  117. require_once($my_driver_file);
  118. $db = new $my_driver(get_object_vars($db));
  119. }
  120. if ($return === TRUE)
  121. {
  122. return $db;
  123. }
  124. // return DB($params, $active_record);
  125. // FUEL -->
  126. CI::$APP->db = '';
  127. //CI::$APP->db = DB($params, $active_record);
  128. CI::$APP->db = $db;
  129. $this->_ci_assign_to_models();
  130. return CI::$APP->db;
  131. }
  132. /** Load a module helper **/
  133. public function helper($helper, $module = NULL) {
  134. if (!isset($module)) $module = $this->_module; // FUEL
  135. if (is_array($helper)) return $this->helpers($helper);
  136. if (isset($this->_ci_helpers[$helper]))
  137. return;
  138. list($path, $_helper) = Modules::find($helper.'_helper', $module, 'helpers/');
  139. if ($path === FALSE)
  140. return parent::helper($helper);
  141. Modules::load_file($_helper, $path);
  142. $this->_ci_helpers[$_helper] = TRUE;
  143. }
  144. /** Load a module language file **/
  145. public function language($langfile, $lang = '', $return = FALSE, $module = NULL) {
  146. if (!isset($module)) $module = $this->_module; // FUEL
  147. if (is_array($langfile)) return $this->languages($langfile);
  148. return CI::$APP->lang->load($langfile, $lang, $return, $module);
  149. }
  150. /** Load a module library **/
  151. public function library($library, $params = NULL, $object_name = NULL, $module = NULL) {
  152. if (!isset($module)) $module = $this->_module; // FUEL
  153. if (is_array($library)) return $this->libraries($library);
  154. $class = end(explode('/', $library));
  155. if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
  156. return CI::$APP->$_alias;
  157. ($_alias = strtolower($object_name)) OR $_alias = strtolower($class);
  158. list($path, $_library) = Modules::find($library, $module, 'libraries/');
  159. /* load library config file as */
  160. if ($params == NULL) {
  161. list($path2, $file) = Modules::find($_alias, $module, 'config/');
  162. ($path2) AND $params = Modules::load_file($file, $path2, 'config');
  163. }
  164. if ($path === FALSE) {
  165. $this->_ci_load_class($library, $params, $object_name);
  166. $_alias = $this->_ci_classes[$class];
  167. } else {
  168. Modules::load_file($_library, $path);
  169. $library = ucfirst($_library);
  170. CI::$APP->$_alias = new $library($params);
  171. $this->_ci_classes[$class] = $_alias;
  172. }
  173. if (CI_VERSION < 2) $this->_ci_assign_to_models();
  174. return CI::$APP->$_alias;
  175. }
  176. /** Load a module model **/
  177. public function model($model, $object_name = NULL, $connect = FALSE, $module = NULL) {
  178. if (!isset($module)) $module = $this->_module; // FUEL
  179. if (is_array($model)) return $this->models($model);
  180. ($_alias = $object_name) OR $_alias = end(explode('/', $model));
  181. if (in_array($_alias, $this->_ci_models, TRUE))
  182. return CI::$APP->$_alias;
  183. list($path, $model) = Modules::find(strtolower($model), $module, 'models/');
  184. (CI_VERSION < 2) ? load_class('Model', FALSE) : load_class('Model', 'core');
  185. if ($connect !== FALSE) {
  186. if ($connect === TRUE) $connect = '';
  187. $this->database($connect, FALSE, TRUE);
  188. }
  189. Modules::load_file($model, $path);
  190. $model = ucfirst($model);
  191. CI::$APP->$_alias = new $model();
  192. if (CI_VERSION < 2) $this->_ci_assign_to_models();
  193. $this->_ci_models[] = $_alias;
  194. return CI::$APP->$_alias;
  195. }
  196. /** Load a module view **/
  197. public function view($view, $vars = array(), $return = FALSE, $module = NULL) {
  198. if (!isset($module)) $module = $this->_module; // FUEL
  199. list($path, $view) = Modules::find($view, $module, 'views/');
  200. $this->_ci_view_path = $path;
  201. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  202. }
  203. function _ci_load($_ci_data) {
  204. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) {
  205. $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
  206. }
  207. if ($_ci_path == '') {
  208. $_ci_file = strpos($_ci_view, '.') ? $_ci_view : $_ci_view.EXT;
  209. $_ci_path = $this->_ci_view_path.$_ci_file;
  210. } else {
  211. $_ci_file = end(explode('/', $_ci_path));
  212. }
  213. if ( ! file_exists($_ci_path))
  214. show_error('Unable to load the requested file: '.$_ci_file);
  215. //if (is_array($_ci_vars)) $this->_ci_cached_vars += $_ci_vars;
  216. //<!-- FUEL FIX
  217. // This allows anything loaded using $this->load (views, files, etc.)
  218. // to become accessible from within the Controller and Model functions.
  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. if (is_array($_ci_vars))
  228. {
  229. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  230. }
  231. // FUEL FIX -->
  232. extract($this->_ci_cached_vars);
  233. ob_start();
  234. if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) {
  235. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  236. } else {
  237. include($_ci_path);
  238. }
  239. log_message('debug', 'File loaded: '.$_ci_path);
  240. if ($_ci_return === TRUE) return ob_get_clean();
  241. if (ob_get_level() > $this->_ci_ob_level + 1) {
  242. ob_end_flush();
  243. } else {
  244. $this->output->append_output(ob_get_clean());
  245. }
  246. }
  247. // --------------------------------------------------------------------
  248. /**
  249. * Load class
  250. *
  251. * This function loads the requested class.
  252. *
  253. * @access private
  254. * @param string the item that is being loaded
  255. * @param mixed any additional parameters
  256. * @param string an optional object name
  257. * @return void
  258. */
  259. function _ci_load_class($class, $params = NULL, $object_name = NULL)
  260. {
  261. // Get the class name, and while we're at it trim any slashes.
  262. // The directory path can be included as part of the class name,
  263. // but we don't want a leading slash
  264. $class = str_replace(EXT, '', trim($class, '/'));
  265. // Was the path included with the class name?
  266. // We look for a slash to determine this
  267. $subdir = '';
  268. if (($last_slash = strrpos($class, '/')) !== FALSE)
  269. {
  270. // Extract the path
  271. $subdir = substr($class, 0, $last_slash + 1);
  272. // Get the filename from the path
  273. $class = substr($class, $last_slash + 1);
  274. }
  275. // We'll test for both lowercase and capitalized versions of the file name
  276. foreach (array(ucfirst($class), strtolower($class)) as $class)
  277. {
  278. //<!-- FUEL
  279. $subclass = MODULES_PATH.$this->_module.'/libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
  280. if (!file_exists($subclass))
  281. {
  282. $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
  283. }
  284. // FUEL -->
  285. // Is this a class extension request?
  286. if (file_exists($subclass))
  287. {
  288. $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
  289. if ( ! file_exists($baseclass))
  290. {
  291. log_message('error', "Unable to load the requested class: ".$class);
  292. show_error("Unable to load the requested class: ".$class);
  293. }
  294. // Safety: Was the class already loaded by a previous call?
  295. if (in_array($subclass, $this->_ci_loaded_files))
  296. {
  297. // Before we deem this to be a duplicate request, let's see
  298. // if a custom object name is being supplied. If so, we'll
  299. // return a new instance of the object
  300. if ( ! is_null($object_name))
  301. {
  302. $CI =& get_instance();
  303. if ( ! isset($CI->$object_name))
  304. {
  305. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  306. }
  307. }
  308. $is_duplicate = TRUE;
  309. log_message('debug', $class." class already loaded. Second attempt ignored.");
  310. return;
  311. }
  312. include_once($baseclass);
  313. include_once($subclass);
  314. $this->_ci_loaded_files[] = $subclass;
  315. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  316. }
  317. // Lets search for the requested library file and load it.
  318. $is_duplicate = FALSE;
  319. foreach ($this->_ci_library_paths as $path)
  320. {
  321. $filepath = $path.'libraries/'.$subdir.$class.EXT;
  322. // Does the file exist? No? Bummer...
  323. if ( ! file_exists($filepath))
  324. {
  325. continue;
  326. }
  327. // Safety: Was the class already loaded by a previous call?
  328. if (in_array($filepath, $this->_ci_loaded_files))
  329. {
  330. // Before we deem this to be a duplicate request, let's see
  331. // if a custom object name is being supplied. If so, we'll
  332. // return a new instance of the object
  333. if ( ! is_null($object_name))
  334. {
  335. $CI =& get_instance();
  336. if ( ! isset($CI->$object_name))
  337. {
  338. return $this->_ci_init_class($class, '', $params, $object_name);
  339. }
  340. }
  341. $is_duplicate = TRUE;
  342. log_message('debug', $class." class already loaded. Second attempt ignored.");
  343. return;
  344. }
  345. include_once($filepath);
  346. $this->_ci_loaded_files[] = $filepath;
  347. return $this->_ci_init_class($class, '', $params, $object_name);
  348. }
  349. } // END FOREACH
  350. // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
  351. if ($subdir == '')
  352. {
  353. $path = strtolower($class).'/'.$class;
  354. return $this->_ci_load_class($path, $params);
  355. }
  356. // If we got this far we were unable to find the requested class.
  357. // We do not issue errors if the load call failed due to a duplicate request
  358. if ($is_duplicate == FALSE)
  359. {
  360. log_message('error', "Unable to load the requested class: ".$class);
  361. show_error("Unable to load the requested class: ".$class);
  362. }
  363. }
  364. }
  365. /** load the CI class for Modular Separation **/
  366. (class_exists('CI', FALSE)) OR require dirname(__FILE__).'/Ci.php';