123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php namespace Laravel;
- class URI {
-
- public static $uri;
-
- public static $segments = array();
-
- protected static $attempt = array(
- 'PATH_INFO', 'REQUEST_URI',
- 'PHP_SELF', 'REDIRECT_URL'
- );
-
- public static function full()
- {
- return static::current().static::query();
- }
-
- public static function current()
- {
- if ( ! is_null(static::$uri)) return static::$uri;
-
-
-
- static::$uri = static::detect();
-
-
-
- if (is_null(static::$uri))
- {
- throw new \Exception("Could not detect request URI.");
- }
- static::segments(static::$uri);
- return static::$uri;
- }
-
- protected static function detect()
- {
- foreach (static::$attempt as $variable)
- {
-
-
-
- $method = "parse_{$variable}";
- if (isset($_SERVER[$variable]))
- {
- $uri = static::$method($_SERVER[$variable]);
- return static::format($uri);
- }
- }
- }
-
- protected static function format($uri)
- {
-
-
-
- $uri = static::remove_base($uri);
- $index = '/'.Config::get('application.index');
-
-
-
- if ($index !== '/')
- {
- $uri = static::remove($uri, $index);
- }
- return trim($uri, '/') ?: '/';
- }
-
- public static function is($pattern)
- {
-
-
-
- if ($pattern !== '/')
- {
- $pattern = str_replace('*', '(.*)', $pattern).'\z';
- }
- else
- {
- $pattern = '^/$';
- }
- return preg_match('#'.$pattern.'#', static::current());
- }
-
- protected static function parse_path_info($value)
- {
- return $value;
- }
-
- protected static function parse_request_uri($value)
- {
- return parse_url($value, PHP_URL_PATH);
- }
-
- protected static function parse_php_self($value)
- {
- return $value;
- }
-
- protected static function parse_redirect_url($value)
- {
- return $value;
- }
-
- protected static function remove_base($uri)
- {
- return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
- }
-
- public static function segment($index, $default = null)
- {
- static::current();
- return array_get(static::$segments, $index - 1, $default);
- }
-
- protected static function segments($uri)
- {
- $segments = explode('/', trim($uri, '/'));
- static::$segments = array_diff($segments, array(''));
- }
-
- protected static function remove($uri, $value)
- {
- return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
- }
-
- protected static function query()
- {
- return (count((array) $_GET) > 0) ? '?'.http_build_query($_GET) : '';
- }
- }
|