loader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php namespace Laravel\Routing;
  2. use Laravel\Arr;
  3. use RecursiveIteratorIterator as Iterator;
  4. use RecursiveDirectoryIterator as DirectoryIterator;
  5. class Loader {
  6. /**
  7. * The location of the base routes file.
  8. *
  9. * @var string
  10. */
  11. protected $base;
  12. /**
  13. * The directory containing nested route files.
  14. *
  15. * @var string
  16. */
  17. protected $nest;
  18. /**
  19. * A cache for all of the routes defined for the entire application.
  20. *
  21. * @var array
  22. */
  23. protected $everything;
  24. /**
  25. * Create a new route loader instance.
  26. *
  27. * @param string $base
  28. * @param string $nest
  29. * @return void
  30. */
  31. public function __construct($base, $nest)
  32. {
  33. $this->base = $base;
  34. $this->nest = $nest;
  35. }
  36. /**
  37. * Load the applicable routes for a given URI.
  38. *
  39. * @param string $uri
  40. * @return array
  41. */
  42. public function load($uri)
  43. {
  44. $segments = Arr::without(explode('/', $uri), '');
  45. return array_merge($this->nested($segments), require $this->base.'routes'.EXT);
  46. }
  47. /**
  48. * Get the appropriate routes from the routes directory for a given URI.
  49. *
  50. * This method works backwards through the URI segments until we find the
  51. * deepest possible matching route directory. Once the deepest directory
  52. * is found, all of the applicable routes will be returend.
  53. *
  54. * @param array $segments
  55. * @return array
  56. */
  57. protected function nested($segments)
  58. {
  59. foreach (array_reverse($segments, true) as $key => $value)
  60. {
  61. $path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT;
  62. if (file_exists($path)) return require $path;
  63. }
  64. return array();
  65. }
  66. /**
  67. * Get every route defined for the application.
  68. *
  69. * The entire routes directory will be searched recursively to gather
  70. * every route for the application. Of course, the routes in the root
  71. * routes file will be returned as well.
  72. *
  73. * @return array
  74. */
  75. public function everything()
  76. {
  77. if ( ! is_null($this->everything)) return $this->everything;
  78. $routes = array();
  79. // First, we'll grab the base routes from the application directory.
  80. // Once we have these, we'll merge all of the nested routes in the
  81. // routes directory into this array of routes.
  82. if (file_exists($path = $this->base.'routes'.EXT))
  83. {
  84. $routes = array_merge($routes, require $path);
  85. }
  86. if ( ! is_dir($this->nest)) return $routes;
  87. $iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST);
  88. foreach ($iterator as $file)
  89. {
  90. // Since some Laravel developers may place HTML files in the route
  91. // directories, we will check for the PHP extension before merging
  92. // the file. Typically, the HTML files are present in installations
  93. // that are not using mod_rewrite and the public directory.
  94. if (filetype($file) === 'file' and strpos($file, EXT) !== false)
  95. {
  96. $routes = array_merge(require $file, $routes);
  97. }
  98. }
  99. return $this->everything = $routes;
  100. }
  101. }