PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/application/libraries/Controller.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 431 lines | 267 code | 89 blank | 75 comment | 53 complexity | a64264e80f00bc0444aa7f63227b2d0e MD5 | raw file
  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. /* load the core loader class */
  3. require_once BASEPATH.'libraries/Loader'.EXT;
  4. /**
  5. * Modular Extensions - PHP5
  6. *
  7. * Adapted from the CodeIgniter Core Classes
  8. * @link http://codeigniter.com
  9. *
  10. * Description:
  11. * This library replaces the CodeIgniter Controller class
  12. * and adds features allowing use of modules and the HMVC design pattern.
  13. *
  14. * Install this file as application/libraries/Controller.php
  15. *
  16. * @copyright Copyright (c) Wiredesignz 2011-01-18
  17. * @version 5.2.31
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  35. * THE SOFTWARE.
  36. **/
  37. class CI extends CI_Base
  38. {
  39. public static $APP;
  40. public function __construct() {
  41. parent::__construct();
  42. /* assign the application instance */
  43. self::$APP = CI_Base::get_instance();
  44. /* assign the core loader */
  45. $this->load = new CI_Loader();
  46. /* use modular config and language */
  47. $this->config = new MX_Config();
  48. $this->lang = new MX_Language();
  49. /* the core classes */
  50. $classes = array(
  51. 'input' => 'Input',
  52. 'benchmark' => 'Benchmark',
  53. 'uri' => 'URI',
  54. 'output' => 'Output',
  55. 'router' => 'Router',
  56. );
  57. /* assign the core classes */
  58. foreach ($classes as $key => $class) {
  59. $this->$key = load_class($class);
  60. }
  61. /* autoload application items */
  62. $this->load->_ci_autoloader();
  63. /* re-assign the core loader to use modules */
  64. $this->load = (class_exists('MX_Loader', FALSE)) ? new MX_Loader() : new Loader();
  65. }
  66. }
  67. class Loader extends CI_Loader
  68. {
  69. private $_module;
  70. public function __construct() {
  71. /* this module name */
  72. $this->_module = CI::$APP->router->fetch_module();
  73. /* ci loader references */
  74. foreach (get_class_vars('CI_Loader') as $var => $val) {
  75. $this->$var =& CI::$APP->load->$var;
  76. }
  77. }
  78. /** Load a module config file **/
  79. public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) {
  80. return CI::$APP->config->load($file, $use_sections, $fail_gracefully, $this->_module);
  81. }
  82. /** Load the database drivers **/
  83. public function database($params = '', $return = FALSE, $active_record = FALSE) {
  84. if (class_exists('CI_DB', FALSE) AND $return == FALSE AND $active_record == FALSE)
  85. return;
  86. require_once BASEPATH.'database/DB'.EXT;
  87. if ($return === TRUE)
  88. return DB($params, $active_record);
  89. CI::$APP->db = DB($params, $active_record);
  90. $this->_ci_assign_to_models();
  91. return CI::$APP->db;
  92. }
  93. /** Load a module helper **/
  94. public function helper($helper) {
  95. if (is_array($helper))
  96. return $this->helpers($helper);
  97. if (isset($this->_ci_helpers[$helper]))
  98. return;
  99. list($path, $_helper) = Modules::find($helper.'_helper', $this->_module, 'helpers/');
  100. if ($path === FALSE)
  101. return parent::helper($helper);
  102. Modules::load_file($_helper, $path);
  103. $this->_ci_helpers[$_helper] = TRUE;
  104. }
  105. /** Load an array of helpers **/
  106. public function helpers($helpers) {
  107. foreach ($helpers as $_helper) $this->helper($_helper);
  108. }
  109. /** Load a module language file **/
  110. public function language($langfile, $lang = '', $return = FALSE) {
  111. return CI::$APP->lang->load($langfile, $lang, $return, $this->_module);
  112. }
  113. /** Load a module library **/
  114. public function library($library, $params = NULL, $object_name = NULL) {
  115. if (is_array($library))
  116. return $this->libraries($library);
  117. $class = strtolower(end(explode('/', $library)));
  118. if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
  119. return CI::$APP->$_alias;
  120. ($_alias = $object_name) OR $_alias = $class;
  121. list($path, $_library) = Modules::find($library, $this->_module, 'libraries/');
  122. /* load library config file as params */
  123. if ($params == NULL) {
  124. list($path2, $file) = Modules::find($_alias, $this->_module, 'config/');
  125. ($path2) AND $params = Modules::load_file($file, $path2, 'config');
  126. }
  127. if ($path === FALSE) {
  128. parent::_ci_load_class($library, $params, $object_name);
  129. $_alias = $this->_ci_classes[$class];
  130. } else {
  131. Modules::load_file($_library, $path);
  132. $library = ucfirst($_library);
  133. CI::$APP->$_alias = new $library($params);
  134. $this->_ci_classes[$class] = $_alias;
  135. }
  136. $this->_ci_assign_to_models();
  137. return CI::$APP->$_alias;
  138. }
  139. /** Load an array of libraries **/
  140. public function libraries($libraries) {
  141. foreach ($libraries as $_library) $this->library($_library);
  142. }
  143. /** Load a module model **/
  144. public function model($model, $object_name = NULL, $connect = FALSE) {
  145. if (is_array($model))
  146. return $this->models($model);
  147. ($_alias = $object_name) OR $_alias = end(explode('/', $model));
  148. if (in_array($_alias, $this->_ci_models, TRUE))
  149. return CI::$APP->$_alias;
  150. list($path, $model) = Modules::find($model, $this->_module, 'models/');
  151. (class_exists('Model', FALSE)) OR load_class('Model', FALSE);
  152. if ($connect !== FALSE) {
  153. if ($connect === TRUE) $connect = '';
  154. $this->database($connect, FALSE, TRUE);
  155. }
  156. Modules::load_file($model, $path);
  157. $model = ucfirst($model);
  158. CI::$APP->$_alias = new $model();
  159. $this->_ci_assign_to_models();
  160. $this->_ci_models[] = $_alias;
  161. return CI::$APP->$_alias;
  162. }
  163. /** Load an array of models **/
  164. function models($models) {
  165. foreach ($models as $_model) $this->model($_model);
  166. }
  167. /** Load a module controller **/
  168. public function module($module, $params = NULL) {
  169. if (is_array($module))
  170. return $this->modules($module);
  171. $_alias = strtolower(end(explode('/', $module)));
  172. CI::$APP->$_alias = Modules::load(array($module => $params));
  173. return CI::$APP->$_alias;
  174. }
  175. /** Load an array of controllers **/
  176. public function modules($modules) {
  177. foreach ($modules as $_module) $this->module($_module);
  178. }
  179. /** Load a module plugin **/
  180. public function plugin($plugin) {
  181. if (isset($this->_ci_plugins[$plugin]))
  182. return;
  183. list($path, $_plugin) = Modules::find($plugin.'_pi', $this->_module, 'plugins/');
  184. if ($path === FALSE)
  185. return parent::plugin($plugin);
  186. Modules::load_file($_plugin, $path);
  187. $this->_ci_plugins[$plugin] = TRUE;
  188. }
  189. /** Load a module view **/
  190. public function view($view, $vars = array(), $return = FALSE) {
  191. list($path, $view) = Modules::find($view, $this->_module, 'views/');
  192. $this->_ci_view_path = $path;
  193. return parent::_ci_load(array('_ci_view' => $view, '_ci_vars' => parent::_ci_object_to_array($vars), '_ci_return' => $return));
  194. }
  195. /** Assign libraries to models **/
  196. public function _ci_assign_to_models() {
  197. foreach ($this->_ci_models as $model) {
  198. CI::$APP->$model->_assign_libraries();
  199. }
  200. }
  201. public function _ci_is_instance() {}
  202. public function __get($var) {
  203. return CI::$APP->$var;
  204. }
  205. /** Autload items **/
  206. public function _ci_autoloader($autoload) {
  207. $path = FALSE;
  208. if ($this->_module)
  209. list($path, $file) = Modules::find('autoload', $this->_module, 'config/');
  210. /* module autoload file */
  211. if ($path != FALSE)
  212. $autoload = array_merge(Modules::load_file($file, $path, 'autoload'), $autoload);
  213. /* nothing to do */
  214. if (count($autoload) == 0) return;
  215. /* autoload config */
  216. if (isset($autoload['config'])){
  217. foreach ($autoload['config'] as $key => $val){
  218. $this->config($val);
  219. }
  220. }
  221. /* autoload helpers, plugins, languages */
  222. foreach (array('helper', 'plugin', 'language') as $type){
  223. if (isset($autoload[$type])){
  224. foreach ($autoload[$type] as $item){
  225. $this->$type($item);
  226. }
  227. }
  228. }
  229. /* autoload database & libraries */
  230. if (isset($autoload['libraries'])){
  231. if (in_array('database', $autoload['libraries'])){
  232. /* autoload database */
  233. if ( ! $db = CI::$APP->config->item('database')){
  234. $db['params'] = 'default';
  235. $db['active_record'] = TRUE;
  236. }
  237. $this->database($db['params'], FALSE, $db['active_record']);
  238. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  239. }
  240. /* autoload libraries */
  241. foreach ($autoload['libraries'] as $library){
  242. $this->library($library);
  243. }
  244. }
  245. /* autoload models */
  246. if (isset($autoload['model'])){
  247. foreach ($autoload['model'] as $model => $alias){
  248. (is_numeric($model)) ? $this->model($alias) : $this->model($model, $alias);
  249. }
  250. }
  251. /* autoload module controllers */
  252. if (isset($autoload['modules'])){
  253. foreach ($autoload['modules'] as $controller) {
  254. ($controller != $this->_module) AND $this->module($controller);
  255. }
  256. }
  257. }
  258. }
  259. if (is_file($location = APPPATH.'libraries/MX_Loader'.EXT)) {
  260. include_once $location;
  261. }
  262. class Controller
  263. {
  264. public $autoload = array();
  265. /** PHP4 compatibility **/
  266. public function Controller() {
  267. /* use the MX_Loader extension if it exists */
  268. $this->load = (class_exists('MX_Loader', FALSE)) ? new MX_Loader() : new Loader();
  269. $class = str_replace($this->config->item('controller_suffix'),'',get_class($this));
  270. log_message('debug', $class." Controller Initialized");
  271. /* register this controller */
  272. Modules::$registry[strtolower($class)] = $this;
  273. /* autoload module items */
  274. $this->load->_ci_autoloader($this->autoload);
  275. }
  276. public function __get($var) {
  277. return CI::$APP->$var;
  278. }
  279. }
  280. if (is_file($location = APPPATH.'libraries/MX_Controller'.EXT)) {
  281. include_once $location;
  282. }
  283. class MX_Config extends CI_Config
  284. {
  285. public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE, $_module = NULL) {
  286. ($file == '') AND $file = 'config';
  287. if (in_array($file, $this->is_loaded, TRUE))
  288. return $this->item($file);
  289. $_module || $_module = CI::$APP->router->fetch_module();
  290. list($path, $file) = Modules::find($file, $_module, 'config/');
  291. if ($path === FALSE) {
  292. parent::load($file, $use_sections, $fail_gracefully);
  293. return $this->item($file);
  294. }
  295. if ($config = Modules::load_file($file, $path, 'config')) {
  296. /* reference to the config array */
  297. $current_config =& $this->config;
  298. if ($use_sections === TRUE) {
  299. if (isset($current_config[$file])) {
  300. $current_config[$file] = array_merge($current_config[$file], $config);
  301. } else {
  302. $current_config[$file] = $config;
  303. }
  304. } else {
  305. $current_config = array_merge($current_config, $config);
  306. }
  307. $this->is_loaded[] = $file;
  308. unset($config);
  309. return $this->item($file);
  310. }
  311. }
  312. }
  313. class MX_Language extends CI_Language
  314. {
  315. public function load($langfile, $lang = '', $return = FALSE, $_module = NULL) {
  316. if (is_array($langfile))
  317. return $this->load_multi($langfile);
  318. $deft_lang = CI::$APP->config->item('language');
  319. $idiom = ($lang == '') ? $deft_lang : $lang;
  320. if (in_array($langfile.'_lang', $this->is_loaded, TRUE))
  321. return $this->language;
  322. $_module || $_module = CI::$APP->router->fetch_module();
  323. list($path, $_langfile) = Modules::find($langfile.'_lang', $_module, 'language/', $idiom);
  324. if ($path === FALSE) {
  325. if ($lang = parent::load($langfile, $lang, $return)) return $lang;
  326. } else {
  327. if($lang = Modules::load_file($_langfile, $path, 'lang')) {
  328. if ($return) return $lang;
  329. $this->language = array_merge($this->language, $lang);
  330. $this->is_loaded[] = $langfile.'_lang';
  331. unset($lang);
  332. }
  333. }
  334. return $this->language;
  335. }
  336. /** Load an array of language files **/
  337. private function load_multi($languages) {
  338. foreach ($languages as $_langfile) $this->load($_langfile);
  339. }
  340. }
  341. /* create the application object */
  342. $APP = new CI();