PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/MX/Router.php

https://gitlab.com/carlosambiado89/IntranetPlanEvalWeb
PHP | 241 lines | 160 code | 30 blank | 51 comment | 31 complexity | a27a536d87073209e3cb69e52cb67825 MD5 | raw file
  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. /* load the MX core module class */
  3. require dirname(__FILE__).'/Modules.php';
  4. /**
  5. * Modular Extensions - HMVC
  6. *
  7. * Adapted from the CodeIgniter Core Classes
  8. * @link http://codeigniter.com
  9. *
  10. * Description:
  11. * This library extends the CodeIgniter router class.
  12. *
  13. * Install this file as application/third_party/MX/Router.php
  14. *
  15. * @copyright Copyright (c) 2015 Wiredesignz
  16. * @version 5.5
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy
  19. * of this software and associated documentation files (the "Software"), to deal
  20. * in the Software without restriction, including without limitation the rights
  21. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. * copies of the Software, and to permit persons to whom the Software is
  23. * furnished to do so, subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in
  26. * all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  34. * THE SOFTWARE.
  35. **/
  36. class MX_Router extends CI_Router
  37. {
  38. public $module;
  39. private $located = 0;
  40. public function fetch_module()
  41. {
  42. return $this->module;
  43. }
  44. protected function _set_request($segments = array())
  45. {
  46. if ($this->translate_uri_dashes === TRUE)
  47. {
  48. foreach(range(0, 2) as $v)
  49. {
  50. isset($segments[$v]) && $segments[$v] = str_replace('-', '_', $segments[$v]);
  51. }
  52. }
  53. $segments = $this->locate($segments);
  54. if($this->located == -1)
  55. {
  56. $this->_set_404override_controller();
  57. return;
  58. }
  59. if(empty($segments))
  60. {
  61. $this->_set_default_controller();
  62. return;
  63. }
  64. $this->set_class($segments[0]);
  65. if (isset($segments[1]))
  66. {
  67. $this->set_method($segments[1]);
  68. }
  69. else
  70. {
  71. $segments[1] = 'index';
  72. }
  73. array_unshift($segments, NULL);
  74. unset($segments[0]);
  75. $this->uri->rsegments = $segments;
  76. }
  77. protected function _set_404override_controller()
  78. {
  79. $this->_set_module_path($this->routes['404_override']);
  80. }
  81. protected function _set_default_controller()
  82. {
  83. if (empty($this->directory))
  84. {
  85. /* set the default controller module path */
  86. $this->_set_module_path($this->default_controller);
  87. }
  88. parent::_set_default_controller();
  89. if(empty($this->class))
  90. {
  91. $this->_set_404override_controller();
  92. }
  93. }
  94. /** Locate the controller **/
  95. public function locate($segments)
  96. {
  97. $this->located = 0;
  98. $ext = $this->config->item('controller_suffix').EXT;
  99. /* use module route if available */
  100. if (isset($segments[0]) && $routes = Modules::parse_routes($segments[0], implode('/', $segments)))
  101. {
  102. $segments = $routes;
  103. }
  104. /* get the segments array elements */
  105. list($module, $directory, $controller) = array_pad($segments, 3, NULL);
  106. /* check modules */
  107. foreach (Modules::$locations as $location => $offset)
  108. {
  109. /* module exists? */
  110. if (is_dir($source = $location.$module.'/controllers/'))
  111. {
  112. $this->module = $module;
  113. $this->directory = $offset.$module.'/controllers/';
  114. /* module sub-controller exists? */
  115. if($directory)
  116. {
  117. /* module sub-directory exists? */
  118. if(is_dir($source.$directory.'/'))
  119. {
  120. $source .= $directory.'/';
  121. $this->directory .= $directory.'/';
  122. /* module sub-directory controller exists? */
  123. if($controller)
  124. {
  125. if(is_file($source.ucfirst($controller).$ext))
  126. {
  127. $this->located = 3;
  128. return array_slice($segments, 2);
  129. }
  130. else $this->located = -1;
  131. }
  132. }
  133. else
  134. if(is_file($source.ucfirst($directory).$ext))
  135. {
  136. $this->located = 2;
  137. return array_slice($segments, 1);
  138. }
  139. else $this->located = -1;
  140. }
  141. /* module controller exists? */
  142. if(is_file($source.ucfirst($module).$ext))
  143. {
  144. $this->located = 1;
  145. return $segments;
  146. }
  147. }
  148. }
  149. if( ! empty($this->directory)) return;
  150. /* application sub-directory controller exists? */
  151. if($directory)
  152. {
  153. if(is_file(APPPATH.'controllers/'.$module.'/'.ucfirst($directory).$ext))
  154. {
  155. $this->directory = $module.'/';
  156. return array_slice($segments, 1);
  157. }
  158. /* application sub-sub-directory controller exists? */
  159. if($controller)
  160. {
  161. if(is_file(APPPATH.'controllers/'.$module.'/'.$directory.'/'.ucfirst($controller).$ext))
  162. {
  163. $this->directory = $module.'/'.$directory.'/';
  164. return array_slice($segments, 2);
  165. }
  166. }
  167. }
  168. /* application controllers sub-directory exists? */
  169. if (is_dir(APPPATH.'controllers/'.$module.'/'))
  170. {
  171. $this->directory = $module.'/';
  172. return array_slice($segments, 1);
  173. }
  174. /* application controller exists? */
  175. if (is_file(APPPATH.'controllers/'.ucfirst($module).$ext))
  176. {
  177. return $segments;
  178. }
  179. $this->located = -1;
  180. }
  181. /* set module path */
  182. protected function _set_module_path(&$_route)
  183. {
  184. if ( ! empty($_route))
  185. {
  186. // Are module/directory/controller/method segments being specified?
  187. $sgs = sscanf($_route, '%[^/]/%[^/]/%[^/]/%s', $module, $directory, $class, $method);
  188. // set the module/controller directory location if found
  189. if ($this->locate(array($module, $directory, $class)))
  190. {
  191. //reset to class/method
  192. switch ($sgs)
  193. {
  194. case 1: $_route = $module.'/index';
  195. break;
  196. case 2: $_route = ($this->located < 2) ? $module.'/'.$directory : $directory.'/index';
  197. break;
  198. case 3: $_route = ($this->located == 2) ? $directory.'/'.$class : $class.'/index';
  199. break;
  200. case 4: $_route = ($this->located == 3) ? $class.'/'.$method : $method.'/index';
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. public function set_class($class)
  207. {
  208. $class = $class.$this->config->item('controller_suffix');
  209. parent::set_class($class);
  210. }
  211. }