/halogy/application/libraries/MY_Router.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 131 lines · 59 code · 24 blank · 48 comment · 12 complexity · 998514bebee46904625e379981a6531b MD5 · raw file

  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. /* load the modules class */
  3. require_once 'Modules'.EXT;
  4. /* define the module locations and offset */
  5. Modules::$locations = array(
  6. APPPATH.'modules/' => '../modules/',
  7. );
  8. /**
  9. * Modular Extensions - PHP5
  10. *
  11. * Adapted from the CodeIgniter Core Classes
  12. * @link http://codeigniter.com
  13. *
  14. * Description:
  15. * This library extends the CodeIgniter router class.
  16. *
  17. * Install this file as application/libraries/MY_Router.php
  18. *
  19. * @copyright Copyright (c) Wiredesignz 2011-01-18
  20. * @version 5.2.31
  21. *
  22. * Permission is hereby granted, free of charge, to any person obtaining a copy
  23. * of this software and associated documentation files (the "Software"), to deal
  24. * in the Software without restriction, including without limitation the rights
  25. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26. * copies of the Software, and to permit persons to whom the Software is
  27. * furnished to do so, subject to the following conditions:
  28. *
  29. * The above copyright notice and this permission notice shall be included in
  30. * all copies or substantial portions of the Software.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  38. * THE SOFTWARE.
  39. **/
  40. class MY_Router extends CI_Router
  41. {
  42. private $module;
  43. public function fetch_module() {
  44. return $this->module;
  45. }
  46. public function _validate_request($segments) {
  47. /* locate module controller */
  48. if ($located = $this->locate($segments)) return $located;
  49. /* use a default 404 controller */
  50. if (isset($this->routes['404']) AND $segments = explode('/', $this->routes['404'])) {
  51. if ($located = $this->locate($segments)) return $located;
  52. }
  53. /* no controller found */
  54. show_404();
  55. }
  56. /** Locate the controller **/
  57. public function locate($segments) {
  58. $this->module = '';
  59. $this->directory = '';
  60. $ext = $this->config->item('controller_suffix').EXT;
  61. /* use module route if available */
  62. if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
  63. $segments = $routes;
  64. }
  65. /* get the segments array elements */
  66. list($module, $directory, $controller) = array_pad($segments, 3, NULL);
  67. foreach (Modules::$locations as $location => $offset) {
  68. /* module exists? */
  69. if (is_dir($source = $location.$module.'/controllers/')) {
  70. $this->module = $module;
  71. $this->directory = $offset.$module.'/controllers/';
  72. /* module sub-controller exists? */
  73. if($directory AND is_file($source.$directory.$ext)) {
  74. return array_slice($segments, 1);
  75. }
  76. /* module sub-directory exists? */
  77. if($directory AND is_dir($module_subdir = $source.$directory.'/')) {
  78. $this->directory .= $directory.'/';
  79. /* module sub-directory controller exists? */
  80. if(is_file($module_subdir.$directory.$ext)) {
  81. return array_slice($segments, 1);
  82. }
  83. /* module sub-directory sub-controller exists? */
  84. if($controller AND is_file($module_subdir.$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(is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
  100. $this->directory = $module.'/';
  101. return array_slice($segments, 1);
  102. }
  103. }
  104. public function set_class($class) {
  105. $this->class = $class.$this->config->item('controller_suffix');
  106. }
  107. }