/system/cms/libraries/MX/Router.php

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