source: sourcecode/application/third_party/MX/Loader.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 12.2 KB
Line 
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 **/
36class MX_Loader extends CI_Loader
37{
38        protected $_module;
39       
40        public $_ci_plugins = array();
41        public $_ci_cached_vars = array();
42       
43        /** Initialize the loader variables **/
44        public function initialize($controller = NULL) {
45               
46                /* set the module name */
47                $this->_module = CI::$APP->router->fetch_module();
48               
49                if (is_a($controller, 'MX_Controller')) {       
50                       
51                        /* reference to the module controller */
52                        $this->controller = $controller;
53                       
54                        /* references to ci loader variables */
55                        foreach (get_class_vars('CI_Loader') as $var => $val) {
56                                if ($var != '_ci_ob_level') {
57                                        $this->$var =& CI::$APP->load->$var;
58                                }
59                        }
60                       
61                } else {
62                        parent::initialize();
63                       
64                        /* autoload module items */
65                        $this->_autoloader(array());
66                }
67               
68                /* add this module path to the loader variables */
69                $this->_add_module_paths($this->_module);
70        }
71
72        /** Add a module path loader variables **/
73        public function _add_module_paths($module = '') {
74               
75                if (empty($module)) return;
76               
77                foreach (Modules::$locations as $location => $offset) {
78                       
79                        /* only add a module path if it exists */
80                        if (is_dir($module_path = $location.$module.'/') && ! in_array($module_path, $this->_ci_model_paths))
81                        {
82                                array_unshift($this->_ci_model_paths, $module_path);
83                        }
84                }
85        }       
86       
87        /** Load a module config file **/
88        public function config($file = 'config', $use_sections = FALSE, $fail_gracefully = FALSE) {
89                return CI::$APP->config->load($file, $use_sections, $fail_gracefully, $this->_module);
90        }
91
92        /** Load the database drivers **/
93        public function database($params = '', $return = FALSE, $active_record = NULL) {
94               
95                if (class_exists('CI_DB', FALSE) AND $return == FALSE AND $active_record == NULL AND isset(CI::$APP->db) AND is_object(CI::$APP->db))
96                        return;
97
98                require_once BASEPATH.'database/DB'.EXT;
99
100                if ($return === TRUE) return DB($params, $active_record);
101                       
102                CI::$APP->db = DB($params, $active_record);
103               
104                return CI::$APP->db;
105        }
106
107        /** Load a module helper **/
108        public function helper($helper = array()) {
109               
110                if (is_array($helper)) return $this->helpers($helper);
111               
112                if (isset($this->_ci_helpers[$helper])) return;
113
114                list($path, $_helper) = Modules::find($helper.'_helper', $this->_module, 'helpers/');
115
116                if ($path === FALSE) return parent::helper($helper);
117
118                Modules::load_file($_helper, $path);
119                $this->_ci_helpers[$_helper] = TRUE;
120        }
121
122        /** Load an array of helpers **/
123        public function helpers($helpers = array()) {
124                foreach ($helpers as $_helper) $this->helper($_helper);
125        }
126
127        /** Load a module language file **/
128        public function language($langfile = array(), $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
129                return CI::$APP->lang->load($langfile, $idiom, $return, $add_suffix, $alt_path, $this->_module);
130        }
131       
132        public function languages($languages) {
133                foreach($languages as $_language) $this->language($_language);
134        }
135       
136        /** Load a module library **/
137        public function library($library = '', $params = NULL, $object_name = NULL) {
138               
139                if (is_array($library)) return $this->libraries($library);             
140               
141                $class = strtolower(basename($library));
142
143                if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
144                        return CI::$APP->$_alias;
145                       
146                ($_alias = strtolower($object_name)) OR $_alias = $class;
147               
148                list($path, $_library) = Modules::find($library, $this->_module, 'libraries/');
149               
150                /* load library config file as params */
151                if ($params == NULL) {
152                        list($path2, $file) = Modules::find($_alias, $this->_module, 'config/');       
153                        ($path2) AND $params = Modules::load_file($file, $path2, 'config');
154                }       
155                       
156                if ($path === FALSE) {
157                       
158                        $this->_ci_load_class($library, $params, $object_name);
159                        $_alias = $this->_ci_classes[$class];
160                       
161                } else {               
162                       
163                        Modules::load_file($_library, $path);
164                       
165                        $library = ucfirst($_library);
166                        CI::$APP->$_alias = new $library($params);
167                       
168                        $this->_ci_classes[$class] = $_alias;
169                }
170               
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               
182                if (is_array($model)) return $this->models($model);
183
184                ($_alias = $object_name) OR $_alias = basename($model);
185
186                if (in_array($_alias, $this->_ci_models, TRUE))
187                        return CI::$APP->$_alias;
188                       
189                /* check module */
190                list($path, $_model) = Modules::find(strtolower($model), $this->_module, 'models/');
191               
192                if ($path == FALSE) {
193                       
194                        /* check application & packages */
195                        parent::model($model, $object_name, $connect);
196                       
197                } else {
198                       
199                        class_exists('CI_Model', FALSE) OR load_class('Model', 'core');
200                       
201                        if ($connect !== FALSE AND ! class_exists('CI_DB', FALSE)) {
202                                if ($connect === TRUE) $connect = '';
203                                $this->database($connect, FALSE, TRUE);
204                        }
205                       
206                        Modules::load_file($_model, $path);
207                       
208                        $model = ucfirst($_model);
209                        CI::$APP->$_alias = new $model();
210                       
211                        $this->_ci_models[] = $_alias;
212                }
213               
214                return CI::$APP->$_alias;
215        }
216
217        /** Load an array of models **/
218        public function models($models) {
219                foreach ($models as $_model) $this->model($_model);     
220        }
221
222        /** Load a module controller **/
223        public function module($module, $params = NULL) {
224               
225                if (is_array($module)) return $this->modules($module);
226
227                $_alias = strtolower(basename($module));
228                CI::$APP->$_alias = Modules::load(array($module => $params));
229                return CI::$APP->$_alias;
230        }
231
232        /** Load an array of controllers **/
233        public function modules($modules) {
234                foreach ($modules as $_module) $this->module($_module);
235        }
236
237        /** Load a module plugin **/
238        public function plugin($plugin) {
239               
240                if (is_array($plugin)) return $this->plugins($plugin);         
241               
242                if (isset($this->_ci_plugins[$plugin]))
243                        return;
244
245                list($path, $_plugin) = Modules::find($plugin.'_pi', $this->_module, 'plugins/');       
246               
247                if ($path === FALSE AND ! is_file($_plugin = APPPATH.'plugins/'.$_plugin.EXT)) {       
248                        show_error("Unable to locate the plugin file: {$_plugin}");
249                }
250
251                Modules::load_file($_plugin, $path);
252                $this->_ci_plugins[$plugin] = TRUE;
253        }
254
255        /** Load an array of plugins **/
256        public function plugins($plugins) {
257                foreach ($plugins as $_plugin) $this->plugin($_plugin);
258        }
259
260        /** Load a module view **/
261        public function view($view, $vars = array(), $return = FALSE) {
262                list($path, $_view) = Modules::find($view, $this->_module, 'views/');
263               
264                if ($path != FALSE) {
265                        $this->_ci_view_paths = array($path => TRUE) + $this->_ci_view_paths;
266                        $view = $_view;
267                }
268               
269                return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
270        }
271
272        public function _ci_is_instance() {}
273
274        protected function &_ci_get_component($component) {
275                return CI::$APP->$component;
276        }
277
278        public function __get($class) {
279                return (isset($this->controller)) ? $this->controller->$class : CI::$APP->$class;
280        }
281
282        public function _ci_load($_ci_data) {
283               
284                extract($_ci_data);
285               
286                if (isset($_ci_view)) {
287                       
288                        $_ci_path = '';
289                       
290                        /* add file extension if not provided */
291                        $_ci_file = (pathinfo($_ci_view, PATHINFO_EXTENSION)) ? $_ci_view : $_ci_view.EXT;
292                       
293                        foreach ($this->_ci_view_paths as $path => $cascade) {                         
294                                if (file_exists($view = $path.$_ci_file)) {
295                                        $_ci_path = $view;
296                                        break;
297                                }
298                               
299                                if ( ! $cascade) break;
300                        }
301                       
302                } elseif (isset($_ci_path)) {
303                       
304                        $_ci_file = basename($_ci_path);
305                        if( ! file_exists($_ci_path)) $_ci_path = '';
306                }
307
308                if (empty($_ci_path))
309                        show_error('Unable to load the requested file: '.$_ci_file);
310
311                if (isset($_ci_vars))
312                        $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, (array) $_ci_vars);
313               
314                extract($this->_ci_cached_vars);
315
316                ob_start();
317
318                if ((bool) @ini_get('short_open_tag') === FALSE AND CI::$APP->config->item('rewrite_short_tags') == TRUE) {
319                        echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
320                } else {
321                        include($_ci_path);
322                }
323
324                log_message('debug', 'File loaded: '.$_ci_path);
325
326                if ($_ci_return == TRUE) return ob_get_clean();
327
328                if (ob_get_level() > $this->_ci_ob_level + 1) {
329                        ob_end_flush();
330                } else {
331                        CI::$APP->output->append_output(ob_get_clean());
332                }
333        }       
334       
335        /** Autoload module items **/
336        public function _autoloader($autoload) {
337               
338                $path = FALSE;
339               
340                if ($this->_module) {
341                       
342                        list($path, $file) = Modules::find('constants', $this->_module, 'config/');     
343                       
344                        /* module constants file */
345                        if ($path != FALSE) {
346                                include_once $path.$file.EXT;
347                        }
348                                       
349                        list($path, $file) = Modules::find('autoload', $this->_module, 'config/');
350               
351                        /* module autoload file */
352                        if ($path != FALSE) {
353                                $autoload = array_merge(Modules::load_file($file, $path, 'autoload'), $autoload);
354                        }
355                }
356               
357                /* nothing to do */
358                if (count($autoload) == 0) return;
359               
360                /* autoload package paths */
361                if (isset($autoload['packages'])) {
362                        foreach ($autoload['packages'] as $package_path) {
363                                $this->add_package_path($package_path);
364                        }
365                }
366                               
367                /* autoload config */
368                if (isset($autoload['config'])) {
369                        foreach ($autoload['config'] as $config) {
370                                $this->config($config);
371                        }
372                }
373
374                /* autoload helpers, plugins, languages */
375                foreach (array('helper', 'plugin', 'language') as $type) {
376                        if (isset($autoload[$type])){
377                                foreach ($autoload[$type] as $item) {
378                                        $this->$type($item);
379                                }
380                        }
381                }       
382                       
383                /* autoload database & libraries */
384                if (isset($autoload['libraries'])) {
385                        if (in_array('database', $autoload['libraries'])) {
386                                /* autoload database */
387                                if ( ! $db = CI::$APP->config->item('database')) {
388                                        $db['params'] = 'default';
389                                        $db['active_record'] = TRUE;
390                                }
391                                $this->database($db['params'], FALSE, $db['active_record']);
392                                $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
393                        }
394
395                        /* autoload libraries */
396                        foreach ($autoload['libraries'] as $library) {
397                                $this->library($library);
398                        }
399                }
400               
401                /* autoload models */
402                if (isset($autoload['model'])) {
403                        foreach ($autoload['model'] as $model => $alias) {
404                                (is_numeric($model)) ? $this->model($alias) : $this->model($model, $alias);
405                        }
406                }
407               
408                /* autoload module controllers */
409                if (isset($autoload['modules'])) {
410                        foreach ($autoload['modules'] as $controller) {
411                                ($controller != $this->_module) AND $this->module($controller);
412                        }
413                }
414        }
415}
416
417/** load the CI class for Modular Separation **/
418(class_exists('CI', FALSE)) OR require dirname(__FILE__).'/Ci.php';
Note: See TracBrowser for help on using the repository browser.