PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/MX/Modules.php

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