Files
spa-api/laravel/uri.php

253 lines
5.3 KiB
PHP
Raw Normal View History

2011-11-14 21:18:18 -06:00
<?php namespace Laravel;
class URI {
/**
* The URI for the current request.
*
* @var string
*/
2011-11-15 19:15:31 -06:00
public static $uri;
2011-11-14 21:18:18 -06:00
/**
* The URI segments for the current request.
*
* @var array
*/
2012-01-16 13:59:24 -06:00
public static $segments = array();
2011-11-14 21:18:18 -06:00
2012-02-08 14:27:22 -06:00
/**
* The server variables to check for the URI.
*
* @var array
*/
2012-02-12 14:48:36 -06:00
protected static $attempt = array(
'PATH_INFO', 'REQUEST_URI',
'PHP_SELF', 'REDIRECT_URL'
);
/**
* Get the full URI including the query string.
*
* @return string
*/
public static function full()
{
return static::current().static::query();
}
2012-02-08 14:27:22 -06:00
2011-11-14 21:18:18 -06:00
/**
* Get the URI for the current request.
*
* @return string
*/
public static function current()
{
if ( ! is_null(static::$uri)) return static::$uri;
2012-02-08 14:27:22 -06:00
// To get the URI, we'll first call the detect method which will spin
// through each of the server variables that we check for the URI in
// and use the first one we encounter for the URI.
static::$uri = static::detect();
2012-02-08 14:37:58 -06:00
// If you ever encounter this error, please inform the nerdy Laravel
2012-02-08 14:27:22 -06:00
// dev team with information about your server. We want to support
2012-02-12 14:48:36 -06:00
// Laravel an as many servers as we possibly can!
2012-02-08 14:27:22 -06:00
if (is_null(static::$uri))
{
throw new \Exception("Could not detect request URI.");
}
static::segments(static::$uri);
return static::$uri;
}
/**
* Detect the URI from the server variables.
*
* @return string|null
*/
protected static function detect()
{
foreach (static::$attempt as $variable)
{
// Each variable we search for the URI has its own parser function
// which is responsible for doing any formatting before the value
// is fed into the main formatting function.
$method = "parse_{$variable}";
if (isset($_SERVER[$variable]))
{
$uri = static::$method($_SERVER[$variable]);
return static::format($uri);
}
}
}
/**
* Format a given URI.
*
* @param string $uri
* @return string
*/
protected static function format($uri)
{
2012-02-12 14:48:36 -06:00
// First we want to remove the application's base URL from the URI if it is
// in the string. It is possible for some of the parsed server variables to
// include the entire document root in the string.
2012-02-08 14:27:22 -06:00
$uri = static::remove_base($uri);
2011-11-14 21:18:18 -06:00
2012-02-08 14:27:22 -06:00
$index = '/'.Config::get('application.index');
2011-11-14 21:18:18 -06:00
2012-02-12 14:48:36 -06:00
// Next we'll remove the index file from the URI if it is there and then
// finally trim down the URI. If the URI is left with spaces, we'll use
// a single slash for the root URI.
2012-02-08 14:27:22 -06:00
if ($index !== '/')
2011-11-14 21:18:18 -06:00
{
$uri = static::remove($uri, $index);
}
2012-02-08 14:35:30 -06:00
return trim($uri, '/') ?: '/';
2012-02-08 14:27:22 -06:00
}
2012-02-12 14:48:36 -06:00
/**
* Determine if the current URI matches a given pattern.
*
* @param string $pattern
* @param string $uri
2012-02-12 14:48:36 -06:00
* @return bool
*/
public static function is($pattern, $uri = null)
2012-02-12 14:48:36 -06:00
{
$uri = $uri ?: static::current();
2012-02-12 14:48:36 -06:00
// Asterisks are translated into zero-or-more regular expression wildcards
// to make it convenient to check if the URI starts with a given pattern
// such as "library/*". This is only done when not root.
if ($pattern !== '/')
{
$pattern = str_replace('*', '(.*)', $pattern).'\z';
}
else
{
$pattern = '^/$';
}
return preg_match('#'.$pattern.'#', $uri);
2012-02-12 14:48:36 -06:00
}
2012-02-08 14:27:22 -06:00
/**
* Parse the PATH_INFO server variable.
*
* @param string $value
* @return string
*/
protected static function parse_path_info($value)
{
return $value;
}
2011-11-14 21:18:18 -06:00
2012-02-08 14:27:22 -06:00
/**
* Parse the REQUEST_URI server variable.
*
* @param string $value
* @return string
*/
protected static function parse_request_uri($value)
{
return parse_url($value, PHP_URL_PATH);
}
/**
* Parse the PHP_SELF server variable.
*
* @param string $value
* @return string
*/
protected static function parse_php_self($value)
{
return $value;
}
/**
* Parse the REDIRECT_URL server variable.
*
* @param string $value
* @return string
*/
protected static function parse_redirect_url($value)
{
return $value;
}
/**
* Remove the base URL off of the request URI.
*
* @param string $uri
* @return string
*/
protected static function remove_base($uri)
{
return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
2011-11-14 21:18:18 -06:00
}
/**
* Get a specific segment of the request URI via an one-based index.
*
* <code>
* // Get the first segment of the request URI
* $segment = URI::segment(1);
*
* // Get the second segment of the URI, or return a default value
* $segment = URI::segment(2, 'Taylor');
* </code>
*
* @param int $index
* @param mixed $default
* @return string
*/
public static function segment($index, $default = null)
{
static::current();
2012-01-16 13:59:24 -06:00
return array_get(static::$segments, $index - 1, $default);
2011-11-14 21:18:18 -06:00
}
2012-02-04 20:13:10 -06:00
/**
* Set the URI segments for the request.
*
* @param string $uri
* @return void
*/
2012-02-04 20:13:30 -06:00
protected static function segments($uri)
2012-02-04 20:13:10 -06:00
{
$segments = explode('/', trim($uri, '/'));
static::$segments = array_diff($segments, array(''));
}
2011-11-14 21:18:18 -06:00
/**
* Remove a given value from the URI.
*
* @param string $uri
* @param string $value
* @return string
*/
protected static function remove($uri, $value)
{
2012-01-16 13:59:24 -06:00
return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
2011-11-14 21:18:18 -06:00
}
2012-02-12 14:48:36 -06:00
/**
* Get the query string for the current request.
*
* @return string
*/
protected static function query()
{
return (count((array) $_GET) > 0) ? '?'.http_build_query($_GET) : '';
}
2011-11-14 21:18:18 -06:00
}