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

/fp_ci/application/third_party/MX/Loader.php

https://gitlab.com/jLKisni/furandpaw-frontend
PHP | 481 lines | 323 code | 85 blank | 73 comment | 65 complexity | 04d49ac6ff748fe2698cf20f414aeacd MD5 | raw file
  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. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  246. }
  247. protected function &_ci_get_component($component)
  248. {
  249. return CI::$APP->$component;
  250. }
  251. public function __get($class)
  252. {
  253. return (isset($this->controller)) ? $this->controller->$class : CI::$APP->$class;
  254. }
  255. public function _ci_load($_ci_data)
  256. {
  257. extract($_ci_data);
  258. if (isset($_ci_view))
  259. {
  260. $_ci_path = '';
  261. /* add file extension if not provided */
  262. $_ci_file = (pathinfo($_ci_view, PATHINFO_EXTENSION)) ? $_ci_view : $_ci_view.EXT;
  263. foreach ($this->_ci_view_paths as $path => $cascade)
  264. {
  265. if (file_exists($view = $path.$_ci_file))
  266. {
  267. $_ci_path = $view;
  268. break;
  269. }
  270. if ( ! $cascade) break;
  271. }
  272. }
  273. elseif (isset($_ci_path))
  274. {
  275. $_ci_file = basename($_ci_path);
  276. if( ! file_exists($_ci_path)) $_ci_path = '';
  277. }
  278. if (empty($_ci_path))
  279. show_error('Unable to load the requested file: '.$_ci_file);
  280. if (isset($_ci_vars))
  281. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, (array) $_ci_vars);
  282. extract($this->_ci_cached_vars);
  283. ob_start();
  284. if ((bool) @ini_get('short_open_tag') === FALSE && CI::$APP->config->item('rewrite_short_tags') == TRUE)
  285. {
  286. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  287. }
  288. else
  289. {
  290. include($_ci_path);
  291. }
  292. log_message('debug', 'File loaded: '.$_ci_path);
  293. if ($_ci_return == TRUE) return ob_get_clean();
  294. if (ob_get_level() > $this->_ci_ob_level + 1)
  295. {
  296. ob_end_flush();
  297. }
  298. else
  299. {
  300. CI::$APP->output->append_output(ob_get_clean());
  301. }
  302. }
  303. /** Autoload module items **/
  304. public function _autoloader($autoload)
  305. {
  306. $path = FALSE;
  307. if ($this->_module)
  308. {
  309. list($path, $file) = Modules::find('constants', $this->_module, 'config/');
  310. /* module constants file */
  311. if ($path != FALSE)
  312. {
  313. include_once $path.$file.EXT;
  314. }
  315. list($path, $file) = Modules::find('autoload', $this->_module, 'config/');
  316. /* module autoload file */
  317. if ($path != FALSE)
  318. {
  319. $autoload = array_merge(Modules::load_file($file, $path, 'autoload'), $autoload);
  320. }
  321. }
  322. /* nothing to do */
  323. if (count($autoload) == 0) return;
  324. /* autoload package paths */
  325. if (isset($autoload['packages']))
  326. {
  327. foreach ($autoload['packages'] as $package_path)
  328. {
  329. $this->add_package_path($package_path);
  330. }
  331. }
  332. /* autoload config */
  333. if (isset($autoload['config']))
  334. {
  335. foreach ($autoload['config'] as $config)
  336. {
  337. $this->config($config);
  338. }
  339. }
  340. /* autoload helpers, plugins, languages */
  341. foreach (array('helper', 'plugin', 'language') as $type)
  342. {
  343. if (isset($autoload[$type]))
  344. {
  345. foreach ($autoload[$type] as $item)
  346. {
  347. $this->$type($item);
  348. }
  349. }
  350. }
  351. // Autoload drivers
  352. if (isset($autoload['drivers']))
  353. {
  354. foreach ($autoload['drivers'] as $item => $alias)
  355. {
  356. (is_int($item)) ? $this->driver($alias) : $this->driver($item, $alias);
  357. }
  358. }
  359. /* autoload database & libraries */
  360. if (isset($autoload['libraries']))
  361. {
  362. if (in_array('database', $autoload['libraries']))
  363. {
  364. /* autoload database */
  365. if ( ! $db = CI::$APP->config->item('database'))
  366. {
  367. $this->database();
  368. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  369. }
  370. }
  371. /* autoload libraries */
  372. foreach ($autoload['libraries'] as $library => $alias)
  373. {
  374. (is_int($library)) ? $this->library($alias) : $this->library($library, NULL, $alias);
  375. }
  376. }
  377. /* autoload models */
  378. if (isset($autoload['model']))
  379. {
  380. foreach ($autoload['model'] as $model => $alias)
  381. {
  382. (is_int($model)) ? $this->model($alias) : $this->model($model, $alias);
  383. }
  384. }
  385. /* autoload module controllers */
  386. if (isset($autoload['modules']))
  387. {
  388. foreach ($autoload['modules'] as $controller)
  389. {
  390. ($controller != $this->_module) && $this->module($controller);
  391. }
  392. }
  393. }
  394. }
  395. /** load the CI class for Modular Separation **/
  396. (class_exists('CI', FALSE)) OR require dirname(__FILE__).'/Ci.php';