refactoring.

This commit is contained in:
Taylor Otwell
2011-09-21 21:46:16 -05:00
parent b9b9711921
commit 0c4018ec88
42 changed files with 980 additions and 1330 deletions

View File

@@ -9,7 +9,25 @@ class URI {
*
* @var string
*/
protected static $uri;
protected $uri;
/**
* The $_SERVER array for the current request.
*
* @var array
*/
protected $server;
/**
* Create a new URI parser instance.
*
* @param array $server
* @return void
*/
public function __construct($server)
{
$this->server = $server;
}
/**
* Determine the request URI.
@@ -23,39 +41,28 @@ class URI {
*
* @return string
*/
public static function get()
public function get()
{
if ( ! is_null(static::$uri)) return static::$uri;
if ( ! is_null($this->uri)) return $this->uri;
if (($uri = static::from_server()) === false)
if (($uri = $this->from_server()) === false)
{
throw new \Exception('Malformed request URI. Request terminated.');
}
return static::$uri = static::format(static::clean($uri));
return $this->uri = $this->format($this->clean($uri));
}
/**
* Get a given URI segment from the URI for the current request.
*
* <code>
* // Get the first segment from the request URI
* $first = Request::uri()->segment(1);
*
* // Get the second segment or return a default value if it doesn't exist
* $second = Request::uri()->segment(2, 'Taylor');
*
* // Get all of the segments for the request URI
* $segments = Request::uri()->segment();
* </code>
*
* @param int $segment
* @param mixed $default
* @return string
*/
public static function segment($segment = null, $default = null)
public function segment($segment = null, $default = null)
{
$segments = Arr::without(explode('/', static::get()), array(''));
$segments = Arr::without(explode('/', $this->get()), array(''));
if ( ! is_null($segment)) $segment = $segment - 1;
@@ -67,20 +74,20 @@ class URI {
*
* @return string
*/
protected static function from_server()
protected function from_server()
{
// If the PATH_INFO $_SERVER element is set, we will use since it contains
// the request URI formatted perfectly for Laravel's routing engine.
if (isset($_SERVER['PATH_INFO']))
if (isset($this->server['PATH_INFO']))
{
return $_SERVER['PATH_INFO'];
return $this->server['PATH_INFO'];
}
// If the REQUEST_URI is set, we need to extract the URL path since this
// should return the URI formatted in a manner similar to PATH_INFO.
elseif (isset($_SERVER['REQUEST_URI']))
elseif (isset($this->server['REQUEST_URI']))
{
return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
return parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
}
throw new \Exception('Unable to determine the request URI.');
@@ -95,7 +102,7 @@ class URI {
* @param string $uri
* @return string
*/
protected static function clean($uri)
protected function clean($uri)
{
foreach (array(parse_url(Config::get('application.url'), PHP_URL_PATH), '/index.php') as $value)
{
@@ -113,7 +120,7 @@ class URI {
* @param string $uri
* @return string
*/
protected static function format($uri)
protected function format($uri)
{
return (($uri = trim($uri, '/')) == '') ? '/' : $uri;
}