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

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

https://gitlab.com/sheldonels/pyrocms
PHP | 246 lines | 131 code | 47 blank | 68 comment | 29 complexity | bcec52224f3aa74fe959aa18a6039eeb MD5 | raw file
  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. Modules::$locations = is_array($CFG->item('modules_locations'))
  5. ? $CFG->item('modules_locations')
  6. : array(APPPATH.'modules/' => '../modules/');
  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. {
  51. $method = 'index';
  52. if (($pos = strrpos($module, '/')) != false) {
  53. $method = substr($module, $pos + 1);
  54. $module = substr($module, 0, $pos);
  55. }
  56. if ($class = self::load($module)) {
  57. if (method_exists($class, $method)) {
  58. ob_start();
  59. $args = func_get_args();
  60. $output = call_user_func_array(array($class, $method), array_slice($args, 1));
  61. $buffer = ob_get_clean();
  62. return ($output !== null) ? $output : $buffer;
  63. }
  64. }
  65. log_message('error', "Module controller failed to run: {$module}/{$method}");
  66. }
  67. /** Load a module controller **/
  68. public static function load($module)
  69. {
  70. (is_array($module)) ? list($module, $params) = each($module) : $params = null;
  71. /* get the module path */
  72. $segments = explode('/', $module);
  73. /* get the requested controller class name */
  74. $alias = strtolower(end($segments));
  75. /* return an existing controller from the registry */
  76. if (isset(self::$registry[$alias])) return self::$registry[$alias];
  77. /* find the controller */
  78. list($class) = CI::$APP->router->locate($segments);
  79. /* controller cannot be located */
  80. if (empty($class)) return;
  81. /* set the module directory */
  82. $path = APPPATH.'controllers/'.CI::$APP->router->fetch_directory();
  83. /* load the controller class */
  84. $class = $class.CI::$APP->config->item('controller_suffix');
  85. self::load_file($class, $path);
  86. /* create and register the new controller */
  87. $controller = ucfirst($class);
  88. self::$registry[$alias] = new $controller($params);
  89. return self::$registry[$alias];
  90. }
  91. /** Library base class autoload **/
  92. public static function autoload($class)
  93. {
  94. /* don't autoload CI_ prefixed classes or those using the config subclass_prefix */
  95. if (strstr($class, 'CI_') or strstr($class, config_item('subclass_prefix'))) return;
  96. /* autoload Modular Extensions MX core classes */
  97. if (strstr($class, 'MX_') and is_file($location = dirname(__FILE__).'/'.substr($class, 3).'.php')) {
  98. include_once $location;
  99. return;
  100. }
  101. /* autoload core classes */
  102. if (is_file($location = APPPATH.'core/'.$class.'.php')) {
  103. include_once $location;
  104. return;
  105. }
  106. /* autoload library classes */
  107. if (is_file($location = APPPATH.'libraries/'.$class.'.php')) {
  108. include_once $location;
  109. return;
  110. }
  111. }
  112. /** Load a module file **/
  113. public static function load_file($file, $path, $type = 'other', $result = true)
  114. {
  115. $file = str_replace('.php', '', $file);
  116. $location = $path.$file.'.php';
  117. if ($type === 'other') {
  118. if (class_exists($file, false)) {
  119. log_message('debug', "File already loaded: {$location}");
  120. return $result;
  121. }
  122. include_once $location;
  123. } else {
  124. /* load config or language array */
  125. include $location;
  126. if ( ! isset($$type) or ! is_array($$type))
  127. show_error("{$location} does not contain a valid {$type} array");
  128. $result = $$type;
  129. }
  130. log_message('debug', "File loaded: {$location}");
  131. return $result;
  132. }
  133. /**
  134. * Find a file
  135. * Scans for files located within modules directories.
  136. * Also scans application directories for models, plugins and views.
  137. * Generates fatal error if file not found.
  138. **/
  139. public static function find($file, $module, $base)
  140. {
  141. $segments = explode('/', $file);
  142. $file = array_pop($segments);
  143. $file_ext = strpos($file, '.') ? $file : $file.'.php';
  144. $path = ltrim(implode('/', $segments).'/', '/');
  145. $module ? $modules[$module] = $path : $modules = array();
  146. if ( ! empty($segments)) {
  147. $modules[array_shift($segments)] = ltrim(implode('/', $segments).'/','/');
  148. }
  149. foreach (Modules::$locations as $location => $offset) {
  150. foreach ($modules as $module => $subpath) {
  151. $fullpath = $location.$module.'/'.$base.$subpath;
  152. if (is_file($fullpath.$file_ext)) return array($fullpath, $file);
  153. if ($base == 'libraries/' and is_file($fullpath.ucfirst($file_ext)))
  154. return array($fullpath, ucfirst($file));
  155. }
  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. return array(APPPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  163. // check shared addons folder
  164. } elseif (is_file(SHARED_ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path.$file_ext)) {
  165. return array(SHARED_ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  166. // check addons folder
  167. } elseif (is_file(ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path.$file_ext)) {
  168. return array(ADDONPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  169. } elseif (defined('MSMPATH') and is_file(MSMPATH.'themes/'.ADMIN_THEME.'/'.$base.$path.$file_ext)) {
  170. return array(MSMPATH.'themes/'.ADMIN_THEME.'/'.$base.$path, $file);
  171. }
  172. } else {
  173. if (is_file(APPPATH.$base.$path.$file_ext)) return array(APPPATH.$base.$path, $file);
  174. }
  175. show_error("Unable to locate the file: {$path}{$file_ext}");
  176. }
  177. return array(false, $file);
  178. }
  179. /** Parse module routes **/
  180. public static function parse_routes($module, $uri)
  181. {
  182. /* load the route file */
  183. if ( ! isset(self::$routes[$module])) {
  184. if (list($path) = self::find('routes', $module, 'config/') and $path)
  185. self::$routes[$module] = self::load_file('routes', $path, 'route');
  186. }
  187. if ( ! isset(self::$routes[$module])) return;
  188. /* parse module routes */
  189. foreach (self::$routes[$module] as $key => $val) {
  190. $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
  191. if (preg_match('#^'.$key.'$#', $uri)) {
  192. if (strpos($val, '$') !== false and strpos($key, '(') !== false) {
  193. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  194. }
  195. return explode('/', $module.'/'.$val);
  196. }
  197. }
  198. }
  199. }