PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/third_party/MX/Loader.php

https://bitbucket.org/nanomites_webdev/heroframework
PHP | 380 lines | 214 code | 97 blank | 69 comment | 54 complexity | 8997acff98f0767a5b425dfd92e92747 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  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) 2011 Wiredesignz
  15. * @version 5.4
  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. private $_module;
  38. public $_ci_plugins;
  39. public function __construct() {
  40. parent::__construct();
  41. /* set the module name */
  42. $this->_module = CI::$APP->router->fetch_module();
  43. /* add this module path to the loader variables */
  44. $this->_add_module_paths($this->_module);
  45. }
  46. /** Initialize the module **/
  47. public function _init($controller) {
  48. /* references to ci loader variables */
  49. foreach (get_class_vars('CI_Loader') as $var => $val) {
  50. if ($var != '_ci_ob_level') $this->$var =& CI::$APP->load->$var;
  51. }
  52. /* set a reference to the module controller */
  53. $this->controller = $controller;
  54. $this->__construct();
  55. }
  56. /** Add a module path loader variables **/
  57. public function _add_module_paths($module = '') {
  58. if (empty($module)) return;
  59. foreach (Modules::$locations as $location => $offset) {
  60. /* only add a module path if it exists */
  61. if (is_dir($module_path = $location.$module.'/')) {
  62. array_unshift($this->_ci_model_paths, $module_path);
  63. }
  64. }
  65. }
  66. /** Load a module config file **/
  67. public function config($file = 'config', $use_sections = FALSE, $fail_gracefully = FALSE) {
  68. return CI::$APP->config->load($file, $use_sections, $fail_gracefully, $this->_module);
  69. }
  70. /** Load the database drivers **/
  71. public function database($params = '', $return = FALSE, $active_record = NULL) {
  72. if (class_exists('CI_DB', FALSE) AND $return == FALSE AND $active_record == NULL AND isset(CI::$APP->db) AND is_object(CI::$APP->db))
  73. return;
  74. require_once BASEPATH.'database/DB'.EXT;
  75. if ($return === TRUE) return DB($params, $active_record);
  76. CI::$APP->db = DB($params, $active_record);
  77. return CI::$APP->db;
  78. }
  79. /** Load a module helper **/
  80. public function helper($helper) {
  81. if (is_array($helper)) return $this->helpers($helper);
  82. if (isset($this->_ci_helpers[$helper])) return;
  83. list($path, $_helper) = Modules::find($helper.'_helper', $this->_module, 'helpers/');
  84. if ($path === FALSE) return parent::helper($helper);
  85. Modules::load_file($_helper, $path);
  86. $this->_ci_helpers[$_helper] = TRUE;
  87. }
  88. /** Load an array of helpers **/
  89. public function helpers($helpers) {
  90. foreach ($helpers as $_helper) $this->helper($_helper);
  91. }
  92. /** Load a module language file **/
  93. public function language($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
  94. return CI::$APP->lang->load($langfile, $idiom, $return, $add_suffix, $alt_path, $this->_module);
  95. }
  96. public function languages($languages) {
  97. foreach($languages as $_language) $this->language($language);
  98. }
  99. /** Load a module library **/
  100. public function library($library, $params = NULL, $object_name = NULL) {
  101. if (is_array($library)) return $this->libraries($library);
  102. $class = strtolower(end(explode('/', $library)));
  103. if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
  104. return CI::$APP->$_alias;
  105. ($_alias = strtolower($object_name)) OR $_alias = $class;
  106. list($path, $_library) = Modules::find($library, $this->_module, 'libraries/');
  107. /* load library config file as params */
  108. if ($params == NULL) {
  109. list($path2, $file) = Modules::find($_alias, $this->_module, 'config/');
  110. ($path2) AND $params = Modules::load_file($file, $path2, 'config');
  111. }
  112. if ($path === FALSE) {
  113. $this->_ci_load_class($library, $params, $object_name);
  114. $_alias = $this->_ci_classes[$class];
  115. } else {
  116. Modules::load_file($_library, $path);
  117. $library = ucfirst($_library);
  118. CI::$APP->$_alias = new $library($params);
  119. $this->_ci_classes[$class] = $_alias;
  120. }
  121. return CI::$APP->$_alias;
  122. }
  123. /** Load an array of libraries **/
  124. public function libraries($libraries) {
  125. foreach ($libraries as $_library) $this->library($_library);
  126. }
  127. /** Load a module model **/
  128. public function model($model, $object_name = NULL, $connect = FALSE) {
  129. if (is_array($model)) return $this->models($model);
  130. ($_alias = $object_name) OR $_alias = end(explode('/', $model));
  131. if (in_array($_alias, $this->_ci_models, TRUE))
  132. return CI::$APP->$_alias;
  133. /* check module */
  134. list($path, $_model) = Modules::find(strtolower($model), $this->_module, 'models/');
  135. if ($path == FALSE) {
  136. /* check application & packages */
  137. parent::model($model, $object_name);
  138. } else {
  139. class_exists('CI_Model', FALSE) OR load_class('Model', 'core');
  140. if ($connect !== FALSE AND ! class_exists('CI_DB', FALSE)) {
  141. if ($connect === TRUE) $connect = '';
  142. $this->database($connect, FALSE, TRUE);
  143. }
  144. Modules::load_file($_model, $path);
  145. $model = ucfirst($_model);
  146. CI::$APP->$_alias = new $model();
  147. $this->_ci_models[] = $_alias;
  148. }
  149. return CI::$APP->$_alias;
  150. }
  151. /** Load an array of models **/
  152. function models($models) {
  153. foreach ($models as $_model) $this->model($_model);
  154. }
  155. /** Load a module controller **/
  156. public function module($module, $params = NULL) {
  157. if (is_array($module)) return $this->modules($module);
  158. $_alias = strtolower(end(explode('/', $module)));
  159. CI::$APP->$_alias = Modules::load(array($module => $params));
  160. return CI::$APP->$_alias;
  161. }
  162. /** Load an array of controllers **/
  163. public function modules($modules) {
  164. foreach ($modules as $_module) $this->module($_module);
  165. }
  166. /** Load a module plugin **/
  167. public function plugin($plugin) {
  168. if (is_array($plugin)) return $this->plugins($plugin);
  169. if (isset($this->_ci_plugins[$plugin]))
  170. return;
  171. list($path, $_plugin) = Modules::find($plugin.'_pi', $this->_module, 'plugins/');
  172. if ($path === FALSE) return;
  173. Modules::load_file($_plugin, $path);
  174. $this->_ci_plugins[$plugin] = TRUE;
  175. }
  176. /** Load an array of plugins **/
  177. public function plugins($plugins) {
  178. foreach ($plugins as $_plugin) $this->plugin($_plugin);
  179. }
  180. /** Load a module view **/
  181. public function view($view, $vars = array(), $return = FALSE) {
  182. list($path, $view) = Modules::find($view, $this->_module, 'views/');
  183. $this->_ci_view_path = $path;
  184. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  185. }
  186. public function _ci_is_instance() {}
  187. public function _ci_get_component($component) {
  188. return CI::$APP->$component;
  189. }
  190. public function __get($class) {
  191. return (isset($this->controller)) ? $this->controller->$class : CI::$APP->$class;
  192. }
  193. function _ci_load($_ci_data) {
  194. foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) {
  195. $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
  196. }
  197. if ($_ci_path == '') {
  198. $_ci_file = strpos($_ci_view, '.') ? $_ci_view : $_ci_view.EXT;
  199. $_ci_path = $this->_ci_view_path.$_ci_file;
  200. } else {
  201. $_ci_file = end(explode('/', $_ci_path));
  202. }
  203. if ( ! file_exists($_ci_path))
  204. show_error('Unable to load the requested file: '.$_ci_file);
  205. if (is_array($_ci_vars))
  206. $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
  207. extract($this->_ci_cached_vars);
  208. ob_start();
  209. if ((bool) @ini_get('short_open_tag') === FALSE AND CI::$APP->config->item('rewrite_short_tags') == TRUE) {
  210. echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  211. } else {
  212. include($_ci_path);
  213. }
  214. log_message('debug', 'File loaded: '.$_ci_path);
  215. if ($_ci_return == TRUE) return ob_get_clean();
  216. if (ob_get_level() > $this->_ci_ob_level + 1) {
  217. ob_end_flush();
  218. } else {
  219. CI::$APP->output->append_output(ob_get_clean());
  220. }
  221. }
  222. /** Autoload module items **/
  223. public function _autoloader($autoload) {
  224. $path = FALSE;
  225. if ($this->_module)
  226. list($path, $file) = Modules::find('autoload', $this->_module, 'config/');
  227. /* module autoload file */
  228. if ($path != FALSE)
  229. $autoload = array_merge(Modules::load_file($file, $path, 'autoload'), $autoload);
  230. /* nothing to do */
  231. if (count($autoload) == 0) return;
  232. /* autoload package paths */
  233. if (isset($autoload['packages'])){
  234. foreach ($autoload['packages'] as $package_path){
  235. $this->add_package_path($package_path);
  236. }
  237. }
  238. /* autoload config */
  239. if (isset($autoload['config'])){
  240. foreach ($autoload['config'] as $config){
  241. $this->config($config);
  242. }
  243. }
  244. /* autoload helpers, plugins, languages */
  245. foreach (array('helper', 'plugin', 'language') as $type){
  246. if (isset($autoload[$type])){
  247. foreach ($autoload[$type] as $item){
  248. $this->$type($item);
  249. }
  250. }
  251. }
  252. /* autoload database & libraries */
  253. if (isset($autoload['libraries'])){
  254. if (in_array('database', $autoload['libraries'])){
  255. /* autoload database */
  256. if ( ! $db = CI::$APP->config->item('database')){
  257. $db['params'] = 'default';
  258. $db['active_record'] = TRUE;
  259. }
  260. $this->database($db['params'], FALSE, $db['active_record']);
  261. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  262. }
  263. /* autoload libraries */
  264. foreach ($autoload['libraries'] as $library){
  265. $this->library($library);
  266. }
  267. }
  268. /* autoload models */
  269. if (isset($autoload['model'])){
  270. foreach ($autoload['model'] as $model => $alias){
  271. (is_numeric($model)) ? $this->model($alias) : $this->model($model, $alias);
  272. }
  273. }
  274. /* autoload module controllers */
  275. if (isset($autoload['modules'])){
  276. foreach ($autoload['modules'] as $controller) {
  277. ($controller != $this->_module) AND $this->module($controller);
  278. }
  279. }
  280. }
  281. }
  282. /** load the CI class for Modular Separation **/
  283. (class_exists('CI', FALSE)) OR require dirname(__FILE__).'/Ci.php';