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

/application/libraries/Modules.php

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