Menu

[143ffb]: / application / third_party / MX / Modules.php  Maximize  Restore  History

Download this file

242 lines (206 with data), 7.0 kB

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