Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
2
 
3
/**
4
 * Modular Extensions - HMVC
5
 *
6
 * Adapted from the CodeIgniter Core Classes
7
 * @link	http://codeigniter.com
8
 *
9
 * Description:
10
 * This library extends the CodeIgniter CI_Loader class
11
 * and adds features allowing use of modules and the HMVC design pattern.
12
 *
13
 * Install this file as application/third_party/MX/Loader.php
14
 *
15
 * @copyright	Copyright (c) 2011 Wiredesignz
16
 * @version 	5.4
17
 * 
18
 * Permission is hereby granted, free of charge, to any person obtaining a copy
19
 * of this software and associated documentation files (the "Software"), to deal
20
 * in the Software without restriction, including without limitation the rights
21
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
 * copies of the Software, and to permit persons to whom the Software is
23
 * furnished to do so, subject to the following conditions:
24
 * 
25
 * The above copyright notice and this permission notice shall be included in
26
 * all copies or substantial portions of the Software.
27
 * 
28
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34
 * THE SOFTWARE.
35
 **/
36
class MX_Loader extends CI_Loader
37
{
38
	protected $_module;
39
 
40
	public $_ci_plugins = array();
41
	public $_ci_cached_vars = array();
42
 
43
		/**
44
     * Keep track of which sparks are loaded. This will come in handy for being
45
     *  speedy about loading files later.
46
     *
47
     * @var array
48
     */
49
    var $_ci_loaded_sparks = array();
50
 
51
    /**
52
     * Is this version less than CI 2.1.0? If so, accomodate
53
     * @bubbafoley's world-destroying change at: http://bit.ly/sIqR7H
54
     * @var bool
55
     */
56
    var $_is_lt_210 = false;
57
 
58
    /**
59
     * Constructor. Define SPARKPATH if it doesn't exist, initialize parent
60
     */
61
    function __construct()
62
    {
63
        if(!defined('SPARKPATH'))
64
        {
65
            define('SPARKPATH', FCPATH.'sparks/');
66
        }
67
 
68
        $this->_is_lt_210 = (is_callable(array('CI_Loader', 'ci_autoloader'))
69
                               || is_callable(array('CI_Loader', '_ci_autoloader')));
70
 
71
        parent::__construct();
72
    }
73
 
74
	/** Initialize the loader variables **/
75
	public function initialize($controller = NULL) {
76
 
77
		/* set the module name */
78
		$this->_module = CI::$APP->router->fetch_module();
79
 
80
		if (is_a($controller, 'MX_Controller')) {	
81
 
82
			/* reference to the module controller */
83
			$this->controller = $controller;
84
 
85
			/* references to ci loader variables */
86
			foreach (get_class_vars('CI_Loader') as $var => $val) {
87
				if ($var != '_ci_ob_level') {
88
					$this->$var =& CI::$APP->load->$var;
89
				}
90
			}
91
 
92
		} else {
93
			parent::initialize();
94
 
95
			/* autoload module items */
96
			$this->_autoloader(array());
97
		}
98
 
99
		/* add this module path to the loader variables */
100
		$this->_add_module_paths($this->_module);
101
	}
102
 
103
	/** Add a module path loader variables **/
104
	public function _add_module_paths($module = '') {
105
 
106
		if (empty($module)) return;
107
 
108
		foreach (Modules::$locations as $location => $offset) {
109
 
110
			/* only add a module path if it exists */
111
			if (is_dir($module_path = $location.$module.'/') && ! in_array($module_path, $this->_ci_model_paths)) 
112
			{
113
				array_unshift($this->_ci_model_paths, $module_path);
114
			}
115
		}
116
	}	
117
 
118
	/** Load a module config file **/
119
	public function config($file = 'config', $use_sections = FALSE, $fail_gracefully = FALSE) {
120
		return CI::$APP->config->load($file, $use_sections, $fail_gracefully, $this->_module);
121
	}
122
 
123
	/** Load the database drivers **/
124
	public function database($params = '', $return = FALSE, $active_record = NULL) {
125
 
126
		if (class_exists('CI_DB', FALSE) AND $return == FALSE AND $active_record == NULL AND isset(CI::$APP->db) AND is_object(CI::$APP->db)) 
127
			return;
128
 
129
		require_once BASEPATH.'database/DB'.EXT;
130
 
131
		if ($return === TRUE) return DB($params, $active_record);
132
 
133
		CI::$APP->db = DB($params, $active_record);
134
 
135
		return CI::$APP->db;
136
	}
137
 
138
	/** Load a module helper **/
139
	public function helper($helper = array()) {
140
 
141
		if (is_array($helper)) return $this->helpers($helper);
142
 
143
		if (isset($this->_ci_helpers[$helper]))	return;
144
 
145
		list($path, $_helper) = Modules::find($helper.'_helper', $this->_module, 'helpers/');
146
 
147
		if ($path === FALSE) return parent::helper($helper);
148
 
149
		Modules::load_file($_helper, $path);
150
		$this->_ci_helpers[$_helper] = TRUE;
151
	}
152
 
153
	/** Load an array of helpers **/
154
	public function helpers($helpers = array()) {
155
		foreach ($helpers as $_helper) $this->helper($_helper);	
156
	}
157
 
158
	/** Load a module language file **/
159
	public function language($langfile = array(), $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
160
		return CI::$APP->lang->load($langfile, $idiom, $return, $add_suffix, $alt_path, $this->_module);
161
	}
162
 
163
	public function languages($languages) {
164
		foreach($languages as $_language) $this->language($_language);
165
	}
166
 
167
	/** Load a module library **/
168
	public function library($library = '', $params = NULL, $object_name = NULL) {
169
 
170
		if (is_array($library)) return $this->libraries($library);		
171
 
172
		$class = strtolower(basename($library));
173
 
174
		if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
175
			return CI::$APP->$_alias;
176
 
177
		($_alias = strtolower($object_name)) OR $_alias = $class;
178
 
179
		list($path, $_library) = Modules::find($library, $this->_module, 'libraries/');
180
 
181
		/* load library config file as params */
182
		if ($params == NULL) {
183
			list($path2, $file) = Modules::find($_alias, $this->_module, 'config/');	
184
			($path2) AND $params = Modules::load_file($file, $path2, 'config');
185
		}	
186
 
187
		if ($path === FALSE) {
188
 
189
			$this->_ci_load_class($library, $params, $object_name);
190
			$_alias = $this->_ci_classes[$class];
191
 
192
		} else {		
193
 
194
			Modules::load_file($_library, $path);
195
 
196
			$library = ucfirst($_library);
197
			CI::$APP->$_alias = new $library($params);
198
 
199
			$this->_ci_classes[$class] = $_alias;
200
		}
201
 
202
		return CI::$APP->$_alias;
203
    }
204
 
205
	/** Load an array of libraries **/
206
	public function libraries($libraries) {
207
		foreach ($libraries as $_library) $this->library($_library);	
208
	}
209
 
210
	/** Load a module model **/
211
	public function model($model, $object_name = NULL, $connect = FALSE) {
212
 
213
		if (is_array($model)) return $this->models($model);
214
 
215
		($_alias = $object_name) OR $_alias = basename($model);
216
 
217
		if (in_array($_alias, $this->_ci_models, TRUE)) 
218
			return CI::$APP->$_alias;
219
 
220
		/* check module */
221
		list($path, $_model) = Modules::find(strtolower($model), $this->_module, 'models/');
222
 
223
		if ($path == FALSE) {
224
 
225
			/* check application & packages */
226
			parent::model($model, $object_name, $connect);
227
 
228
		} else {
229
 
230
			class_exists('CI_Model', FALSE) OR load_class('Model', 'core');
231
 
232
			if ($connect !== FALSE AND ! class_exists('CI_DB', FALSE)) {
233
				if ($connect === TRUE) $connect = '';
234
				$this->database($connect, FALSE, TRUE);
235
			}
236
 
237
			Modules::load_file($_model, $path);
238
 
239
			$model = ucfirst($_model);
240
			CI::$APP->$_alias = new $model();
241
 
242
			$this->_ci_models[] = $_alias;
243
		}
244
 
245
		return CI::$APP->$_alias;
246
	}
247
 
248
	/** Load an array of models **/
249
	public function models($models) {
250
		foreach ($models as $_model) $this->model($_model);	
251
	}
252
 
253
	/** Load a module controller **/
254
	public function module($module, $params = NULL)	{
255
 
256
		if (is_array($module)) return $this->modules($module);
257
 
258
		$_alias = strtolower(basename($module));
259
		CI::$APP->$_alias = Modules::load(array($module => $params));
260
		return CI::$APP->$_alias;
261
	}
262
 
263
	/** Load an array of controllers **/
264
	public function modules($modules) {
265
		foreach ($modules as $_module) $this->module($_module);	
266
	}
267
 
268
	/** Load a module plugin **/
269
	public function plugin($plugin)	{
270
 
271
		if (is_array($plugin)) return $this->plugins($plugin);		
272
 
273
		if (isset($this->_ci_plugins[$plugin]))	
274
			return;
275
 
276
		list($path, $_plugin) = Modules::find($plugin.'_pi', $this->_module, 'plugins/');	
277
 
278
		if ($path === FALSE AND ! is_file($_plugin = APPPATH.'plugins/'.$_plugin.EXT)) {	
279
			show_error("Unable to locate the plugin file: {$_plugin}");
280
		}
281
 
282
		Modules::load_file($_plugin, $path);
283
		$this->_ci_plugins[$plugin] = TRUE;
284
	}
285
 
286
	/** Load an array of plugins **/
287
	public function plugins($plugins) {
288
		foreach ($plugins as $_plugin) $this->plugin($_plugin);	
289
	}
290
 
291
	/** Load a module view **/
292
	public function view($view, $vars = array(), $return = FALSE) {
293
		list($path, $_view) = Modules::find($view, $this->_module, 'views/');
294
 
295
		if ($path != FALSE) {
296
			$this->_ci_view_paths = array($path => TRUE) + $this->_ci_view_paths;
297
			$view = $_view;
298
		}
299
 
300
		return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
301
	}
302
 
303
	public function _ci_is_instance() {}
304
 
305
	protected function &_ci_get_component($component) {
306
		return CI::$APP->$component;
307
	} 
308
 
309
	public function __get($class) {
310
		return (isset($this->controller)) ? $this->controller->$class : CI::$APP->$class;
311
	}
312
 
313
	public function _ci_load($_ci_data) {
314
 
315
		extract($_ci_data);
316
 
317
		if (isset($_ci_view)) {
318
 
319
			$_ci_path = '';
320
 
321
			/* add file extension if not provided */
322
			$_ci_file = (pathinfo($_ci_view, PATHINFO_EXTENSION)) ? $_ci_view : $_ci_view.EXT;
323
 
324
			foreach ($this->_ci_view_paths as $path => $cascade) {				
325
				if (file_exists($view = $path.$_ci_file)) {
326
					$_ci_path = $view;
327
					break;
328
				}
329
 
330
				if ( ! $cascade) break;
331
			}
332
 
333
		} elseif (isset($_ci_path)) {
334
 
335
			$_ci_file = basename($_ci_path);
336
			if( ! file_exists($_ci_path)) $_ci_path = '';
337
		}
338
 
339
		if (empty($_ci_path)) 
340
			show_error('Unable to load the requested file: '.$_ci_file);
341
 
342
		if (isset($_ci_vars)) 
343
			$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, (array) $_ci_vars);
344
 
345
		extract($this->_ci_cached_vars);
346
 
347
		ob_start();
348
 
349
		if ((bool) @ini_get('short_open_tag') === FALSE AND CI::$APP->config->item('rewrite_short_tags') == TRUE) {
350
			echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
351
		} else {
352
			include($_ci_path); 
353
		}
354
 
355
		log_message('debug', 'File loaded: '.$_ci_path);
356
 
357
		if ($_ci_return == TRUE) return ob_get_clean();
358
 
359
		if (ob_get_level() > $this->_ci_ob_level + 1) {
360
			ob_end_flush();
361
		} else {
362
			CI::$APP->output->append_output(ob_get_clean());
363
		}
364
	}	
365
 
366
	/** Autoload module items **/
367
	public function _autoloader($autoload) {
368
 
369
		$path = FALSE;
370
 
371
		if ($this->_module) {
372
 
373
			list($path, $file) = Modules::find('constants', $this->_module, 'config/');	
374
 
375
			/* module constants file */
376
			if ($path != FALSE) {
377
				include_once $path.$file.EXT;
378
			}
379
 
380
			list($path, $file) = Modules::find('autoload', $this->_module, 'config/');
381
 
382
			/* module autoload file */
383
			if ($path != FALSE) {
384
				$autoload = array_merge(Modules::load_file($file, $path, 'autoload'), $autoload);
385
			}
386
		}
387
 
388
		/* nothing to do */
389
		if (count($autoload) == 0) return;
390
 
391
		/* autoload package paths */
392
		if (isset($autoload['packages'])) {
393
			foreach ($autoload['packages'] as $package_path) {
394
				$this->add_package_path($package_path);
395
			}
396
		}
397
 
398
		/* autoload config */
399
		if (isset($autoload['config'])) {
400
			foreach ($autoload['config'] as $config) {
401
				$this->config($config);
402
			}
403
		}
404
 
405
		/* autoload helpers, plugins, languages */
406
		foreach (array('helper', 'plugin', 'language') as $type) {
407
			if (isset($autoload[$type])){
408
				foreach ($autoload[$type] as $item) {
409
					$this->$type($item);
410
				}
411
			}
412
		}	
413
 
414
		/* autoload database & libraries */
415
		if (isset($autoload['libraries'])) {
416
			if (in_array('database', $autoload['libraries'])) {
417
				/* autoload database */
418
				if ( ! $db = CI::$APP->config->item('database')) {
419
					$db['params'] = 'default';
420
					$db['active_record'] = TRUE;
421
				}
422
				$this->database($db['params'], FALSE, $db['active_record']);
423
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
424
			}
425
 
426
			/* autoload libraries */
427
			foreach ($autoload['libraries'] as $library) {
428
				$this->library($library);
429
			}
430
		}
431
 
432
		/* autoload models */
433
		if (isset($autoload['model'])) {
434
			foreach ($autoload['model'] as $model => $alias) {
435
				(is_numeric($model)) ? $this->model($alias) : $this->model($model, $alias);
436
			}
437
		}
438
 
439
		/* autoload module controllers */
440
		if (isset($autoload['modules'])) {
441
			foreach ($autoload['modules'] as $controller) {
442
				($controller != $this->_module) AND $this->module($controller);
443
			}
444
		}
445
	}
446
 
447
	//spark managemenet
448
 
449
 
450
 
451
 
452
    /**
453
     * Load a spark by it's path within the sparks directory defined by
454
     *  SPARKPATH, such as 'markdown/1.0'
455
     * @param string $spark The spark path withint he sparks directory
456
     * @param <type> $autoload An optional array of items to autoload
457
     *  in the format of:
458
     *   array (
459
     *     'helper' => array('somehelper')
460
     *   )
461
     * @return <type>
462
     */
463
    function spark($spark, $autoload = array())
464
    {
465
        if(is_array($spark))
466
        {
467
            foreach($spark as $s)
468
            {
469
                $this->spark($s);
470
            }
471
        }
472
 
473
        $spark = ltrim($spark, '/');
474
        $spark = rtrim($spark, '/');
475
 
476
        $spark_path = SPARKPATH . $spark . '/';
477
        $parts      = explode('/', $spark);
478
        $spark_slug = strtolower($parts[0]);
479
 
480
        # If we've already loaded this spark, bail
481
        if(array_key_exists($spark_slug, $this->_ci_loaded_sparks))
482
        {
483
            return true;
484
        }
485
 
486
        # Check that it exists. CI Doesn't check package existence by itself
487
        if(!file_exists($spark_path))
488
        {
489
            show_error("Cannot find spark path at $spark_path");
490
        }
491
 
492
        if(count($parts) == 2)
493
        {
494
            $this->_ci_loaded_sparks[$spark_slug] = $spark;
495
        }
496
 
497
        $this->add_package_path($spark_path);
498
 
499
        foreach($autoload as $type => $read)
500
        {
501
            if($type == 'library')
502
                $this->library($read);
503
            elseif($type == 'model')
504
                $this->model($read);
505
            elseif($type == 'config')
506
                $this->config($read);
507
            elseif($type == 'helper')
508
                $this->helper($read);
509
            elseif($type == 'view')
510
                $this->view($read);
511
            else
512
                show_error ("Could not autoload object of type '$type' ($read) for spark $spark");
513
        }
514
 
515
        // Looks for a spark's specific autoloader
516
        $this->ci_autoloader($spark_path);
517
 
518
        return true;
519
    }
520
 
521
	/**
522
	 * Pre-CI 2.0.3 method for backward compatility.
523
	 *
524
	 * @param null $basepath
525
	 * @return void
526
	 */
527
	function _ci_autoloader($basepath = NULL)
528
	{
529
		$this->ci_autoloader($basepath);
530
	}
531
 
532
	/**
533
	 * Specific Autoloader (99% ripped from the parent)
534
	 *
535
	 * The config/autoload.php file contains an array that permits sub-systems,
536
	 * libraries, and helpers to be loaded automatically.
537
	 *
538
	 * @param array|null $basepath
539
	 * @return void
540
	 */
541
	function ci_autoloader($basepath = NULL)
542
	{
543
        if($basepath !== NULL)
544
        {
545
            $autoload_path = $basepath.'config/autoload'.EXT;
546
        }
547
        else
548
        {
549
            $autoload_path = APPPATH.'config/autoload'.EXT;
550
        }
551
 
552
        if(! file_exists($autoload_path))
553
        {
554
            return FALSE;
555
        }
556
 
557
		include($autoload_path);
558
 
559
		if ( ! isset($autoload))
560
		{
561
			return FALSE;
562
		}
563
 
564
        if($this->_is_lt_210 || $basepath !== NULL)
565
        {
566
            // Autoload packages
567
            if (isset($autoload['packages']))
568
            {
569
                foreach ($autoload['packages'] as $package_path)
570
                {
571
                    $this->add_package_path($package_path);
572
                }
573
            }
574
        }
575
 
576
        // Autoload sparks
577
		if (isset($autoload['sparks']))
578
		{
579
			foreach ($autoload['sparks'] as $spark)
580
			{
581
				$this->spark($spark);
582
			}
583
		}
584
 
585
        if($this->_is_lt_210 || $basepath !== NULL)
586
        {
587
            if (isset($autoload['config']))
588
            {
589
                // Load any custom config file
590
                if (count($autoload['config']) > 0)
591
                {
592
                    $CI =& get_instance();
593
                    foreach ($autoload['config'] as $key => $val)
594
                    {
595
                        $CI->config->load($val);
596
                    }
597
                }
598
            }
599
 
600
            // Autoload helpers and languages
601
            foreach (array('helper', 'language') as $type)
602
            {
603
                if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
604
                {
605
                    $this->$type($autoload[$type]);
606
                }
607
            }
608
 
609
            // A little tweak to remain backward compatible
610
            // The $autoload['core'] item was deprecated
611
            if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
612
            {
613
                $autoload['libraries'] = $autoload['core'];
614
            }
615
 
616
            // Load libraries
617
            if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
618
            {
619
                // Load the database driver.
620
                if (in_array('database', $autoload['libraries']))
621
                {
622
                    $this->database();
623
                    $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
624
                }
625
 
626
                // Load all other libraries
627
                foreach ($autoload['libraries'] as $item)
628
                {
629
                    $this->library($item);
630
                }
631
            }
632
 
633
            // Autoload models
634
            if (isset($autoload['model']))
635
            {
636
                $this->model($autoload['model']);
637
            }
638
        }
639
	}
640
	//end of spark manager
641
}
642
 
643
/** load the CI class for Modular Separation **/
644
(class_exists('CI', FALSE)) OR require dirname(__FILE__).'/Ci.php';