PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/application/third_party/MX/Modules.php

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