PageRenderTime 55ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/libraries/MX/Modules.php

http://github.com/pyrocms/pyrocms
PHP | 251 lines | 133 code | 49 blank | 69 comment | 32 complexity | 834eab717f535fed288809e262cf67d1 MD5 | raw file
Possible License(s): CC0-1.0, MIT
  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. /* autoload library classes */
  104. if(is_file($location = APPPATH.'libraries/'.$class.EXT)) {
  105. include_once $location;
  106. return;
  107. }
  108. }
  109. /** Load a module file **/
  110. public static function load_file($file, $path, $type = 'other', $result = TRUE) {
  111. $file = str_replace(EXT, '', $file);
  112. $location = $path.$file.EXT;
  113. if ($type === 'other') {
  114. if (class_exists($file, FALSE)) {
  115. log_message('debug', "File already loaded: {$location}");
  116. return $result;
  117. }
  118. include_once $location;
  119. } else {
  120. /* load config or language array */
  121. include $location;
  122. if ( ! isset($$type) OR ! is_array($$type))
  123. show_error("{$location} does not contain a valid {$type} array");
  124. $result = $$type;
  125. }
  126. log_message('debug', "File loaded: {$location}");
  127. return $result;
  128. }
  129. /**
  130. * Find a file
  131. * Scans for files located within modules directories.
  132. * Also scans application directories for models, plugins and views.
  133. * Generates fatal error if file not found.
  134. **/
  135. public static function find($file, $module, $base) {
  136. $segments = explode('/', $file);
  137. $file = array_pop($segments);
  138. $file_ext = strpos($file, '.') ? $file : $file.EXT;
  139. $path = ltrim(implode('/', $segments).'/', '/');
  140. $module ? $modules[$module] = $path : $modules = array();
  141. if ( ! empty($segments)) {
  142. $modules[array_shift($segments)] = ltrim(implode('/', $segments).'/','/');
  143. }
  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 it a global plugin? */
  153. if ($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. /* is the file in an admin theme? */
  158. if ($base == 'views/') {
  159. if (defined('ADMIN_THEME')) {
  160. // check system folder
  161. if (is_file(APPPATH.'themes/'.ADMIN_THEME.'/'.$base.$path.$file_ext))
  162. {
  163. return array(APPPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  164. }
  165. // check shared addons folder
  166. elseif (is_file(SHARED_ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path.$file_ext))
  167. {
  168. return array(SHARED_ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  169. }
  170. // check addons folder
  171. elseif (is_file(ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path.$file_ext))
  172. {
  173. return array(ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  174. }
  175. }
  176. else {
  177. if (is_file(APPPATH.$base.$path.$file_ext)) return array(APPPATH.$base.$path, $file);
  178. }
  179. show_error("Unable to locate the file: {$path}{$file_ext}");
  180. }
  181. return array(FALSE, $file);
  182. }
  183. /** Parse module routes **/
  184. public static function parse_routes($module, $uri) {
  185. /* load the route file */
  186. if ( ! isset(self::$routes[$module])) {
  187. if (list($path) = self::find('routes', $module, 'config/') AND $path)
  188. self::$routes[$module] = self::load_file('routes', $path, 'route');
  189. }
  190. if ( ! isset(self::$routes[$module])) return;
  191. /* parse module routes */
  192. foreach (self::$routes[$module] as $key => $val) {
  193. $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
  194. if (preg_match('#^'.$key.'$#', $uri)) {
  195. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) {
  196. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  197. }
  198. return explode('/', $module.'/'.$val);
  199. }
  200. }
  201. }
  202. }