PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/MX/Loader.php

https://bitbucket.org/behamin_/mundocaravanas
PHP | 487 lines | 328 code | 86 blank | 73 comment | 67 complexity | 474fa3b74ef1b2ccd448b3c7e4018871 MD5 | raw file
Possible License(s): Unlicense, BSD-3-Clause
  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. /**
  3. * Modular Extensions - HMVC
  4. *
  5. * Adapted from the CodeIgniter Core Classes
  6. * @link http://codeigniter.com
  7. *
  8. * Description:
  9. * This library extends the CodeIgniter CI_Loader class
  10. * and adds features allowing use of modules and the HMVC design pattern.
  11. *
  12. * Install this file as application/third_party/MX/Loader.php
  13. *
  14. * @copyright Copyright (c) 2015 Wiredesignz
  15. * @version 5.5
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to deal
  19. * in the Software without restriction, including without limitation the rights
  20. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included in
  25. * all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. * THE SOFTWARE.
  34. **/
  35. class MX_Loader extends CI_Loader
  36. {
  37. protected $_module;
  38. public $_ci_plugins = array();
  39. public $_ci_cached_vars = array();
  40. /** Initialize the loader variables **/
  41. public function initialize($controller = NULL)
  42. {
  43. /* set the module name */
  44. $this->_module = CI::$APP->router->fetch_module();
  45. if ($controller instanceof MX_Controller)
  46. {
  47. /* reference to the module controller */
  48. $this->controller = $controller;
  49. /* references to ci loader variables */
  50. foreach (get_class_vars('CI_Loader') as $var => $val)
  51. {
  52. if ($var != '_ci_ob_level')
  53. {
  54. $this->$var =& CI::$APP->load->$var;
  55. }
  56. }
  57. }
  58. else
  59. {
  60. parent::initialize();
  61. /* autoload module items */
  62. $this->_autoloader(array());
  63. }
  64. /* add this module path to the loader variables */
  65. $this->_add_module_paths($this->_module);
  66. }
  67. /** Add a module path loader variables **/
  68. public function _add_module_paths($module = '')
  69. {
  70. if (empty($module)) return;
  71. foreach (Modules::$locations as $location => $offset)
  72. {
  73. /* only add a module path if it exists */
  74. if (is_dir($module_path = $location.$module.'/') && ! in_array($module_path, $this->_ci_model_paths))
  75. {
  76. array_unshift($this->_ci_model_paths, $module_path);
  77. }
  78. }
  79. }
  80. /** Load a module config file **/
  81. public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
  82. {
  83. return CI::$APP->config->load($file, $use_sections, $fail_gracefully, $this->_module);
  84. }
  85. /** Load the database drivers **/
  86. public function database($params = '', $return = FALSE, $query_builder = NULL)
  87. {
  88. if ($return === FALSE && $query_builder === NULL &&
  89. isset(CI::$APP->db) && is_object(CI::$APP->db) && ! empty(CI::$APP->db->conn_id))
  90. {
  91. return FALSE;
  92. }
  93. require_once BASEPATH.'database/DB'.EXT;
  94. if ($return === TRUE) return DB($params, $query_builder);
  95. CI::$APP->db = DB($params, $query_builder);
  96. return $this;
  97. }
  98. /** Load a module helper **/
  99. public function helper($helper = array())
  100. {
  101. if (is_array($helper)) return $this->helpers($helper);
  102. if (isset($this->_ci_helpers[$helper])) return;
  103. list($path, $_helper) = Modules::find($helper.'_helper', $this->_module, 'helpers/');
  104. if ($path === FALSE) return parent::helper($helper);
  105. Modules::load_file($_helper, $path);
  106. $this->_ci_helpers[$_helper] = TRUE;
  107. return $this;
  108. }
  109. /** Load an array of helpers **/
  110. public function helpers($helpers = array())
  111. {
  112. foreach ($helpers as $_helper) $this->helper($_helper);
  113. return $this;
  114. }
  115. /** Load a module language file **/
  116. public function language($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
  117. {
  118. CI::$APP->lang->load($langfile, $idiom, $return, $add_suffix, $alt_path, $this->_module);
  119. return $this;
  120. }
  121. public function languages($languages)
  122. {
  123. foreach($languages as $_language) $this->language($_language);
  124. return $this;
  125. }
  126. /** Load a module library **/
  127. public function library($library, $params = NULL, $object_name = NULL)
  128. {
  129. if (is_array($library)) return $this->libraries($library);
  130. $class = strtolower(basename($library));
  131. if (isset($this->_ci_classes[$class]) && $_alias = $this->_ci_classes[$class])
  132. return $this;
  133. ($_alias = strtolower($object_name)) OR $_alias = $class;
  134. list($path, $_library) = Modules::find($library, $this->_module, 'libraries/');
  135. /* load library config file as params */
  136. if ($params == NULL)
  137. {
  138. list($path2, $file) = Modules::find($_alias, $this->_module, 'config/');
  139. ($path2) && $params = Modules::load_file($file, $path2, 'config');
  140. }
  141. if ($path === FALSE)
  142. {
  143. $this->_ci_load_library($library, $params, $object_name);
  144. }
  145. else
  146. {
  147. Modules::load_file($_library, $path);
  148. $library = ucfirst($_library);
  149. CI::$APP->$_alias = new $library($params);
  150. $this->_ci_classes[$class] = $_alias;
  151. }
  152. return $this;
  153. }
  154. /** Load an array of libraries **/
  155. public function libraries($libraries)
  156. {
  157. foreach ($libraries as $library => $alias)
  158. {
  159. (is_int($library)) ? $this->library($alias) : $this->library($library, NULL, $alias);
  160. }
  161. return $this;
  162. }
  163. /** Load a module model **/
  164. public function model($model, $object_name = NULL, $connect = FALSE)
  165. {
  166. if (is_array($model)) return $this->models($model);
  167. ($_alias = $object_name) OR $_alias = basename($model);
  168. if (in_array($_alias, $this->_ci_models, TRUE))
  169. return $this;
  170. /* check module */
  171. list($path, $_model) = Modules::find(strtolower($model), $this->_module, 'models/');
  172. if ($path == FALSE)
  173. {
  174. /* check application & packages */
  175. parent::model($model, $object_name, $connect);
  176. }
  177. else
  178. {
  179. class_exists('CI_Model', FALSE) OR load_class('Model', 'core');
  180. if ($connect !== FALSE && ! class_exists('CI_DB', FALSE))
  181. {
  182. if ($connect === TRUE) $connect = '';
  183. $this->database($connect, FALSE, TRUE);
  184. }
  185. Modules::load_file($_model, $path);
  186. $model = ucfirst($_model);
  187. CI::$APP->$_alias = new $model();
  188. $this->_ci_models[] = $_alias;
  189. }
  190. return $this;
  191. }
  192. /** Load an array of models **/
  193. public function models($models)
  194. {
  195. foreach ($models as $model => $alias)
  196. {
  197. (is_int($model)) ? $this->model($alias) : $this->model($model, $alias);
  198. }
  199. return $this;
  200. }
  201. /** Load a module controller **/
  202. public function module($module, $params = NULL)
  203. {
  204. if (is_array($module)) return $this->modules($module);
  205. $_alias = strtolower(basename($module));
  206. CI::$APP->$_alias = Modules::load(array($module => $params));
  207. return $this;
  208. }
  209. /** Load an array of controllers **/
  210. public function modules($modules)
  211. {
  212. foreach ($modules as $_module) $this->module($_module);
  213. return $this;
  214. }
  215. /** Load a module plugin **/
  216. public function plugin($plugin)
  217. {
  218. if (is_array($plugin)) return $this->plugins($plugin);
  219. if (isset($this->_ci_plugins[$plugin]))
  220. return $this;
  221. list($path, $_plugin) = Modules::find($plugin.'_pi', $this->_module, 'plugins/');
  222. if ($path === FALSE && ! is_file($_plugin = APPPATH.'plugins/'.$_plugin.EXT))
  223. {
  224. show_error("Unable to locate the plugin file: {$_plugin}");
  225. }
  226. Modules::load_file($_plugin, $path);
  227. $this->_ci_plugins[$plugin] = TRUE;
  228. return $this;
  229. }
  230. /** Load an array of plugins **/
  231. public function plugins($plugins)
  232. {
  233. foreach ($plugins as $_plugin) $this->plugin($_plugin);
  234. return $this;
  235. }
  236. /** Load a module view **/
  237. public function view($view, $vars = array(), $return = FALSE)
  238. {
  239. list($path, $_view) = Modules::find($view, $this->_module, 'views/');
  240. if ($path != FALSE)
  241. {
  242. $this->_ci_view_paths = array($path => TRUE) + $this->_ci_view_paths;
  243. $view = $_view;
  244. }
  245. if (method_exists($this, '_ci_object_to_array'))
  246. {
  247. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  248. } else {
  249. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
  250. }
  251. }
  252. protected function &_ci_get_component($component)
  253. {
  254. return CI::$APP->$component;
  255. }
  256. public function __get($class)
  257. {
  258. return (isset($this->controller)) ? $this->controller->$class : CI::$APP->$class;
  259. }
  260. public function _ci_load($_ci_data)
  261. {
  262. extract($_ci_data);
  263. if (isset($_ci_view))
  264. {
  265. $_ci_path = '';
  266. /* add file extension if not provided */
  267. $_ci_file = (pathinfo($_ci_view, PATHINFO_EXTENSION)) ? $_ci_view : $_ci_view.EXT;
  268. foreach ($this->_ci_view_paths as $path => $cascade)
  269. {
  270. if (file_exists($view = $path.$_ci_file))
  271. {
  272. $_ci_path = $view;
  273. break;
  274. }
  275. if ( ! $cascade) break;
  276. }
  277. }
  278. elseif (isset($_ci_path))
  279. {
  280. $_ci_file = basename($_ci_path);
  281. if( ! file_exists($_ci_path)) $_ci_path = '';
  282. }
  283. if (empty($_ci_path))
  284. show_error('Unable to load the requested file: '.$_ci_file);
  285. if (isset($_ci_vars))
  286. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, (array) $_ci_vars);
  287. extract($this->_ci_cached_vars);
  288. ob_start();
  289. if ((bool) @ini_get('short_open_tag') === FALSE && CI::$APP->config->item('rewrite_short_tags') == TRUE)
  290. {
  291. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  292. }
  293. else
  294. {
  295. include($_ci_path);
  296. }
  297. log_message('debug', 'File loaded: '.$_ci_path);
  298. if ($_ci_return == TRUE) return ob_get_clean();
  299. if (ob_get_level() > $this->_ci_ob_level + 1)
  300. {
  301. ob_end_flush();
  302. }
  303. else
  304. {
  305. CI::$APP->output->append_output(ob_get_clean());
  306. }
  307. }
  308. /** Autoload module items **/
  309. public function _autoloader($autoload)
  310. {
  311. $path = FALSE;
  312. if ($this->_module)
  313. {
  314. list($path, $file) = Modules::find('constants', $this->_module, 'config/');
  315. /* module constants file */
  316. if ($path != FALSE)
  317. {
  318. include_once $path.$file.EXT;
  319. }
  320. list($path, $file) = Modules::find('autoload', $this->_module, 'config/');
  321. /* module autoload file */
  322. if ($path != FALSE)
  323. {
  324. $autoload = array_merge(Modules::load_file($file, $path, 'autoload'), $autoload);
  325. }
  326. }
  327. /* nothing to do */
  328. if (count($autoload) == 0) return;
  329. /* autoload package paths */
  330. if (isset($autoload['packages']))
  331. {
  332. foreach ($autoload['packages'] as $package_path)
  333. {
  334. $this->add_package_path($package_path);
  335. }
  336. }
  337. /* autoload config */
  338. if (isset($autoload['config']))
  339. {
  340. foreach ($autoload['config'] as $config)
  341. {
  342. $this->config($config);
  343. }
  344. }
  345. /* autoload helpers, plugins, languages */
  346. foreach (array('helper', 'plugin', 'language') as $type)
  347. {
  348. if (isset($autoload[$type]))
  349. {
  350. foreach ($autoload[$type] as $item)
  351. {
  352. $this->$type($item);
  353. }
  354. }
  355. }
  356. // Autoload drivers
  357. if (isset($autoload['drivers']))
  358. {
  359. foreach ($autoload['drivers'] as $item => $alias)
  360. {
  361. (is_int($item)) ? $this->driver($alias) : $this->driver($item, $alias);
  362. }
  363. }
  364. /* autoload database & libraries */
  365. if (isset($autoload['libraries']))
  366. {
  367. if (in_array('database', $autoload['libraries']))
  368. {
  369. /* autoload database */
  370. if ( ! $db = CI::$APP->config->item('database'))
  371. {
  372. $this->database();
  373. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  374. }
  375. }
  376. /* autoload libraries */
  377. foreach ($autoload['libraries'] as $library => $alias)
  378. {
  379. (is_int($library)) ? $this->library($alias) : $this->library($library, NULL, $alias);
  380. }
  381. }
  382. /* autoload models */
  383. if (isset($autoload['model']))
  384. {
  385. foreach ($autoload['model'] as $model => $alias)
  386. {
  387. (is_int($model)) ? $this->model($alias) : $this->model($model, $alias);
  388. }
  389. }
  390. /* autoload module controllers */
  391. if (isset($autoload['modules']))
  392. {
  393. foreach ($autoload['modules'] as $controller)
  394. {
  395. ($controller != $this->_module) && $this->module($controller);
  396. }
  397. }
  398. }
  399. }
  400. /** load the CI class for Modular Separation **/
  401. (class_exists('CI', FALSE)) OR require dirname(__FILE__).'/Ci.php';