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