loader.php 2.82 KB
Newer Older
Taylor Otwell committed
1 2 3
<?php namespace Laravel\Routing;

use Laravel\Arr;
4 5
use RecursiveIteratorIterator as Iterator;
use RecursiveDirectoryIterator as DirectoryIterator;
Taylor Otwell committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

class Loader {

	/**
	 * The location of the base routes file.
	 *
	 * @var string
	 */
	protected $base;

	/**
	 * The directory containing nested route files.
	 *
	 * @var string
	 */
	protected $nest;

	/**
	 * A cache for all of the routes defined for the entire application.
	 *
	 * @var array
	 */
	protected $everything;

	/**
	 * Create a new route loader instance.
	 *
	 * @param  string  $base
	 * @param  string  $nest
	 * @return void
	 */
	public function __construct($base, $nest)
	{
		$this->base = $base;
		$this->nest = $nest;
	}

	/**
	 * Load the applicable routes for a given URI.
	 *
	 * @param  string  $uri
	 * @return array
	 */
	public function load($uri)
	{
51 52
		$segments = Arr::without(explode('/', $uri), '');

Taylor Otwell committed
53
		return array_merge($this->nested($segments), require $this->base.'routes'.EXT);
Taylor Otwell committed
54 55 56 57 58
	}

	/**
	 * Get the appropriate routes from the routes directory for a given URI.
	 *
59 60 61 62
	 * This method works backwards through the URI segments until we find the
	 * deepest possible matching route directory. Once the deepest directory
	 * is found, all of the applicable routes will be returend.
	 *
Taylor Otwell committed
63 64 65 66 67 68 69
	 * @param  array  $segments
	 * @return array
	 */
	protected function nested($segments)
	{
		foreach (array_reverse($segments, true) as $key => $value)
		{
70 71 72
			$path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT;

			if (file_exists($path)) return require $path;
Taylor Otwell committed
73 74
		}

75
		return array();
Taylor Otwell committed
76 77 78 79 80
	}

	/**
	 * Get every route defined for the application.
	 *
81 82 83 84
	 * The entire routes directory will be searched recursively to gather
	 * every route for the application. Of course, the routes in the root
	 * routes file will be returned as well.
	 *
Taylor Otwell committed
85 86 87 88 89 90 91 92
	 * @return array
	 */
	public function everything()
	{
		if ( ! is_null($this->everything)) return $this->everything;

		$routes = array();

93 94 95
		// First, we'll grab the base routes from the application directory.
		// Once we have these, we'll merge all of the nested routes in the
		// routes directory into this array of routes.
96 97 98 99 100
		if (file_exists($path = $this->base.'routes'.EXT))
		{
			$routes = array_merge($routes, require $path);
		}

101 102
		if ( ! is_dir($this->nest)) return $routes;

103
		$iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST);
Taylor Otwell committed
104

105
		foreach ($iterator as $file)
Taylor Otwell committed
106
		{
107 108 109 110
			// Since some Laravel developers may place HTML files in the route
			// directories, we will check for the PHP extension before merging
			// the file. Typically, the HTML files are present in installations
			// that are not using mod_rewrite and the public directory.
111
			if (filetype($file) === 'file' and strpos($file, EXT) !== false)
Taylor Otwell committed
112 113 114 115 116 117 118 119 120
			{
				$routes = array_merge(require $file, $routes);
			}
		}

		return $this->everything = $routes;
	}

}