/www/application/third_party/MX/Router.php

https://github.com/kelios/imshop · PHP · 136 lines · 62 code · 25 blank · 49 comment · 15 complexity · 84d679d46165e9bcdd0b592a94ab37ac 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) 2011 Wiredesignz
  16. * @version 5.4
  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. private $module;
  39. public function fetch_module() {
  40. return $this->module;
  41. }
  42. public function _validate_request($segments) {
  43. if (count($segments) == 0) return $segments;
  44. /* locate module controller */
  45. if ($located = $this->locate($segments)) return $located;
  46. /* use a default 404_override controller */
  47. if (isset($this->routes['404_override']) AND $segments = explode('/', $this->routes['404_override'])) {
  48. if ($located = $this->locate($segments)) return $located;
  49. }
  50. /* no controller found */
  51. show_404();
  52. }
  53. /** Locate the controller **/
  54. public function locate($segments) {
  55. $this->module = '';
  56. $this->directory = '';
  57. $ext = $this->config->item('controller_suffix').EXT;
  58. /* use module route if available */
  59. if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
  60. $segments = $routes;
  61. }
  62. /* get the segments array elements */
  63. list($module, $directory, $controller) = array_pad($segments, 3, NULL);
  64. /* check modules */
  65. foreach (Modules::$locations as $location => $offset) {
  66. /* module exists? */
  67. if (is_dir($source = $location.$module.'/')) {
  68. $this->module = $module;
  69. $this->directory = $offset.$module.'/';
  70. /* module sub-controller exists? */
  71. if($directory AND is_file($source.$directory.$ext)) {
  72. return array_slice($segments, 1);
  73. }
  74. /* module sub-directory exists? */
  75. if($directory AND is_dir($source.$directory.'/')) {
  76. $source = $source.$directory.'/';
  77. $this->directory .= $directory.'/';
  78. /* module sub-directory controller exists? */
  79. if(is_file($source.$directory.$ext)) {
  80. return array_slice($segments, 1);
  81. }
  82. /* module sub-directory sub-controller exists? */
  83. if($controller AND is_file($source.$controller.$ext)) {
  84. return array_slice($segments, 2);
  85. }
  86. }
  87. /* module controller exists? */
  88. if(is_file($source.$module.$ext)) {
  89. return $segments;
  90. }
  91. }
  92. }
  93. /* application controller exists? */
  94. if (is_file(APPPATH.'controllers/'.$module.$ext)) {
  95. return $segments;
  96. }
  97. /* application sub-directory controller exists? */
  98. if($directory AND is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
  99. $this->directory = $module.'/';
  100. return array_slice($segments, 1);
  101. }
  102. /* application sub-directory default controller exists? */
  103. if (is_file(APPPATH.'controllers/'.$module.'/'.$this->default_controller.$ext)) {
  104. $this->directory = $module.'/';
  105. return array($this->default_controller);
  106. }
  107. }
  108. public function set_class($class) {
  109. $this->class = $class.$this->config->item('controller_suffix');
  110. }
  111. }