loader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $routes = (file_exists($path = $this->base.'routes'.EXT)) ? require $path : array();
  45. return array_merge($this->nested(Arr::without(explode('/', $uri), array(''))), $routes);
  46. }
  47. /**
  48. * Get the appropriate routes from the routes directory for a given URI.
  49. *
  50. * @param array $segments
  51. * @return array
  52. */
  53. protected function nested($segments)
  54. {
  55. // Work backwards through the URI segments until we find the deepest possible
  56. // matching route directory. Once we find it, we will return those routes.
  57. foreach (array_reverse($segments, true) as $key => $value)
  58. {
  59. if (file_exists($path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT))
  60. {
  61. return require $path;
  62. }
  63. }
  64. return array();
  65. }
  66. /**
  67. * Get every route defined for the application.
  68. *
  69. * @return array
  70. */
  71. public function everything()
  72. {
  73. if ( ! is_null($this->everything)) return $this->everything;
  74. $routes = array();
  75. if (file_exists($path = $this->base.'routes'.EXT))
  76. {
  77. $routes = array_merge($routes, require $path);
  78. }
  79. // Since route files can be nested deep within the route directory, we need to
  80. // recursively spin through each directory to find every file.
  81. $iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST);
  82. foreach ($iterator as $file)
  83. {
  84. // Since some Laravel developers may place HTML files in the route directories, we will
  85. // check for the PHP extension before merging the file. Typically, the HTML files are
  86. // present in installations that are not using mod_rewrite and the public directory.
  87. if (filetype($file) === 'file' and strpos($file, EXT) !== false)
  88. {
  89. $routes = array_merge(require $file, $routes);
  90. }
  91. }
  92. return $this->everything = $routes;
  93. }
  94. }