[1] | 1 | <?php (defined('BASEPATH')) OR exit('No direct script access allowed'); |
---|
| 2 | |
---|
| 3 | (defined('EXT')) OR define('EXT', '.php'); |
---|
| 4 | |
---|
| 5 | global $CFG; |
---|
| 6 | |
---|
| 7 | /* get module locations from config settings or use the default module location and offset */ |
---|
| 8 | is_array(Modules::$locations = $CFG->item('modules_locations')) OR Modules::$locations = array( |
---|
| 9 | APPPATH.'modules/' => '../modules/', |
---|
| 10 | ); |
---|
| 11 | |
---|
| 12 | /* PHP5 spl_autoload */ |
---|
| 13 | spl_autoload_register('Modules::autoload'); |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * Modular Extensions - HMVC |
---|
| 17 | * |
---|
| 18 | * Adapted from the CodeIgniter Core Classes |
---|
| 19 | * @link http://codeigniter.com |
---|
| 20 | * |
---|
| 21 | * Description: |
---|
| 22 | * This library provides functions to load and instantiate controllers |
---|
| 23 | * and module controllers allowing use of modules and the HMVC design pattern. |
---|
| 24 | * |
---|
| 25 | * Install this file as application/third_party/MX/Modules.php |
---|
| 26 | * |
---|
| 27 | * @copyright Copyright (c) 2011 Wiredesignz |
---|
| 28 | * @version 5.4 |
---|
| 29 | * |
---|
| 30 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 31 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 32 | * in the Software without restriction, including without limitation the rights |
---|
| 33 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 34 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 35 | * furnished to do so, subject to the following conditions: |
---|
| 36 | * |
---|
| 37 | * The above copyright notice and this permission notice shall be included in |
---|
| 38 | * all copies or substantial portions of the Software. |
---|
| 39 | * |
---|
| 40 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 41 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 42 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 43 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 44 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 45 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 46 | * THE SOFTWARE. |
---|
| 47 | **/ |
---|
| 48 | class Modules |
---|
| 49 | { |
---|
| 50 | public static $routes, $registry, $locations; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Run a module controller method |
---|
| 54 | * Output from module is buffered and returned. |
---|
| 55 | **/ |
---|
| 56 | public static function run($module) { |
---|
| 57 | |
---|
| 58 | $method = 'index'; |
---|
| 59 | |
---|
| 60 | if(($pos = strrpos($module, '/')) != FALSE) { |
---|
| 61 | $method = substr($module, $pos + 1); |
---|
| 62 | $module = substr($module, 0, $pos); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if($class = self::load($module)) { |
---|
| 66 | |
---|
| 67 | if (method_exists($class, $method)) { |
---|
| 68 | ob_start(); |
---|
| 69 | $args = func_get_args(); |
---|
| 70 | $output = call_user_func_array(array($class, $method), array_slice($args, 1)); |
---|
| 71 | $buffer = ob_get_clean(); |
---|
| 72 | return ($output !== NULL) ? $output : $buffer; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | log_message('error', "Module controller failed to run: {$module}/{$method}"); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** Load a module controller **/ |
---|
| 80 | public static function load($module) { |
---|
| 81 | |
---|
| 82 | (is_array($module)) ? list($module, $params) = each($module) : $params = NULL; |
---|
| 83 | |
---|
| 84 | /* get the requested controller class name */ |
---|
| 85 | $alias = strtolower(basename($module)); |
---|
| 86 | |
---|
| 87 | /* create or return an existing controller from the registry */ |
---|
| 88 | if ( ! isset(self::$registry[$alias])) { |
---|
| 89 | |
---|
| 90 | /* find the controller */ |
---|
| 91 | list($class) = CI::$APP->router->locate(explode('/', $module)); |
---|
| 92 | |
---|
| 93 | /* controller cannot be located */ |
---|
| 94 | if (empty($class)) return; |
---|
| 95 | |
---|
| 96 | /* set the module directory */ |
---|
| 97 | $path = APPPATH.'controllers/'.CI::$APP->router->fetch_directory(); |
---|
| 98 | |
---|
| 99 | /* load the controller class */ |
---|
| 100 | $class = $class.CI::$APP->config->item('controller_suffix'); |
---|
| 101 | self::load_file($class, $path); |
---|
| 102 | |
---|
| 103 | /* create and register the new controller */ |
---|
| 104 | $controller = ucfirst($class); |
---|
| 105 | self::$registry[$alias] = new $controller($params); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | return self::$registry[$alias]; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** Library base class autoload **/ |
---|
| 112 | public static function autoload($class) { |
---|
| 113 | |
---|
| 114 | /* don't autoload CI_ prefixed classes or those using the config subclass_prefix */ |
---|
| 115 | if (strstr($class, 'CI_') OR strstr($class, config_item('subclass_prefix'))) return; |
---|
| 116 | |
---|
| 117 | /* autoload Modular Extensions MX core classes */ |
---|
| 118 | if (strstr($class, 'MX_') AND is_file($location = dirname(__FILE__).'/'.substr($class, 3).EXT)) { |
---|
| 119 | include_once $location; |
---|
| 120 | return; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /* autoload core classes */ |
---|
| 124 | if(is_file($location = APPPATH.'core/'.$class.EXT)) { |
---|
| 125 | include_once $location; |
---|
| 126 | return; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /* autoload library classes */ |
---|
| 130 | if(is_file($location = APPPATH.'libraries/'.$class.EXT)) { |
---|
| 131 | include_once $location; |
---|
| 132 | return; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** Load a module file **/ |
---|
| 137 | public static function load_file($file, $path, $type = 'other', $result = TRUE) { |
---|
| 138 | |
---|
| 139 | $file = str_replace(EXT, '', $file); |
---|
| 140 | $location = $path.$file.EXT; |
---|
| 141 | |
---|
| 142 | if ($type === 'other') { |
---|
| 143 | if (class_exists($file, FALSE)) { |
---|
| 144 | log_message('debug', "File already loaded: {$location}"); |
---|
| 145 | return $result; |
---|
| 146 | } |
---|
| 147 | include_once $location; |
---|
| 148 | } else { |
---|
| 149 | |
---|
| 150 | /* load config or language array */ |
---|
| 151 | include $location; |
---|
| 152 | |
---|
| 153 | if ( ! isset($$type) OR ! is_array($$type)) |
---|
| 154 | show_error("{$location} does not contain a valid {$type} array"); |
---|
| 155 | |
---|
| 156 | $result = $$type; |
---|
| 157 | } |
---|
| 158 | log_message('debug', "File loaded: {$location}"); |
---|
| 159 | return $result; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Find a file |
---|
| 164 | * Scans for files located within modules directories. |
---|
| 165 | * Also scans application directories for models, plugins and views. |
---|
| 166 | * Generates fatal error if file not found. |
---|
| 167 | **/ |
---|
| 168 | public static function find($file, $module, $base) { |
---|
| 169 | |
---|
| 170 | $segments = explode('/', $file); |
---|
| 171 | |
---|
| 172 | $file = array_pop($segments); |
---|
| 173 | $file_ext = (pathinfo($file, PATHINFO_EXTENSION)) ? $file : $file.EXT; |
---|
| 174 | |
---|
| 175 | $path = ltrim(implode('/', $segments).'/', '/'); |
---|
| 176 | $module ? $modules[$module] = $path : $modules = array(); |
---|
| 177 | |
---|
| 178 | if ( ! empty($segments)) { |
---|
| 179 | $modules[array_shift($segments)] = ltrim(implode('/', $segments).'/','/'); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | foreach (Modules::$locations as $location => $offset) { |
---|
| 183 | foreach($modules as $module => $subpath) { |
---|
| 184 | $fullpath = $location.$module.'/'.$base.$subpath; |
---|
| 185 | |
---|
| 186 | if ($base == 'libraries/' AND is_file($fullpath.ucfirst($file_ext))) |
---|
| 187 | return array($fullpath, ucfirst($file)); |
---|
| 188 | |
---|
| 189 | if (is_file($fullpath.$file_ext)) return array($fullpath, $file); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | return array(FALSE, $file); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** Parse module routes **/ |
---|
| 197 | public static function parse_routes($module, $uri) { |
---|
| 198 | |
---|
| 199 | /* load the route file */ |
---|
| 200 | if ( ! isset(self::$routes[$module])) { |
---|
| 201 | if (list($path) = self::find('routes', $module, 'config/') AND $path) |
---|
| 202 | self::$routes[$module] = self::load_file('routes', $path, 'route'); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if ( ! isset(self::$routes[$module])) return; |
---|
| 206 | |
---|
| 207 | /* parse module routes */ |
---|
| 208 | foreach (self::$routes[$module] as $key => $val) { |
---|
| 209 | |
---|
| 210 | $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key); |
---|
| 211 | |
---|
| 212 | if (preg_match('#^'.$key.'$#', $uri)) { |
---|
| 213 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) { |
---|
| 214 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | return explode('/', $module.'/'.$val); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|