Files
spa-api/laravel/routing/loader.php

122 lines
2.9 KiB
PHP
Raw Normal View History

2011-09-19 21:33:25 -05:00
<?php namespace Laravel\Routing;
use Laravel\Arr;
2011-09-19 23:43:17 -05:00
use RecursiveIteratorIterator as Iterator;
use RecursiveDirectoryIterator as DirectoryIterator;
2011-09-19 21:33:25 -05:00
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)
{
$routes = (file_exists($path = $this->base.'routes'.EXT)) ? require $path : array();
2011-10-15 14:04:11 -05:00
$segments = Arr::without(explode('/', $uri), '');
return array_merge($this->nested($segments), $routes);
2011-09-19 21:33:25 -05:00
}
/**
* Get the appropriate routes from the routes directory for a given URI.
*
2011-10-15 14:04:11 -05:00
* 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.
*
2011-09-19 21:33:25 -05:00
* @param array $segments
* @return array
*/
protected function nested($segments)
{
foreach (array_reverse($segments, true) as $key => $value)
{
2011-10-15 14:04:11 -05:00
$path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT;
if (file_exists($path)) return require $path;
2011-09-19 21:33:25 -05:00
}
2011-09-20 00:07:16 -05:00
return array();
2011-09-19 21:33:25 -05:00
}
/**
* Get every route defined for the application.
*
2011-10-15 14:04:11 -05:00
* 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.
*
2011-09-19 21:33:25 -05:00
* @return array
*/
public function everything()
{
if ( ! is_null($this->everything)) return $this->everything;
$routes = array();
// 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.
2011-09-20 00:07:16 -05:00
if (file_exists($path = $this->base.'routes'.EXT))
{
$routes = array_merge($routes, require $path);
}
2011-10-15 14:04:11 -05:00
if ( ! is_dir($this->nest)) return $routes;
2011-09-20 00:07:16 -05:00
$iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST);
2011-09-19 21:33:25 -05:00
2011-09-19 23:43:17 -05:00
foreach ($iterator as $file)
2011-09-19 21:33:25 -05:00
{
// 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.
2011-09-19 23:43:17 -05:00
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
2011-09-19 21:33:25 -05:00
{
$routes = array_merge(require $file, $routes);
}
}
return $this->everything = $routes;
}
}