PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/system/cms/libraries/MX/Loader.php

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