Commit dead6c0a authored by Taylor Otwell's avatar Taylor Otwell

Merge branch 'develop'

parents 4f361a1b fcd6cd77
...@@ -2,6 +2,20 @@ ...@@ -2,6 +2,20 @@
class Request { class Request {
/**
* The request URI.
*
* @var string
*/
public static $uri;
/**
* The route handling the current request.
*
* @var Route
*/
public static $route;
/** /**
* Get the request URI. * Get the request URI.
* *
...@@ -9,8 +23,13 @@ class Request { ...@@ -9,8 +23,13 @@ class Request {
*/ */
public static function uri() public static function uri()
{ {
if ( ! is_null(static::$uri))
{
return static::$uri;
}
// ------------------------------------------------------- // -------------------------------------------------------
// If the PATH_INFO is available, use it. // Use the PATH_INFO variable if it is available.
// ------------------------------------------------------- // -------------------------------------------------------
if (isset($_SERVER['PATH_INFO'])) if (isset($_SERVER['PATH_INFO']))
{ {
...@@ -28,9 +47,6 @@ class Request { ...@@ -28,9 +47,6 @@ class Request {
throw new \Exception("Malformed request URI. Request terminated."); throw new \Exception("Malformed request URI. Request terminated.");
} }
} }
// -------------------------------------------------------
// Neither PATH_INFO or REQUEST_URI are available.
// -------------------------------------------------------
else else
{ {
throw new \Exception('Unable to determine the request URI.'); throw new \Exception('Unable to determine the request URI.');
...@@ -58,6 +74,17 @@ class Request { ...@@ -58,6 +74,17 @@ class Request {
return ($uri == '') ? '/' : Str::lower($uri); return ($uri == '') ? '/' : Str::lower($uri);
} }
/**
* Determine if the route handling the request is a given name.
*
* @param string $name
* @return bool
*/
public static function is($name)
{
return (is_array(static::$route->callback) and isset(static::$route->callback['name']) and static::$route->callback['name'] === $name);
}
/** /**
* Get the request method. * Get the request method.
* *
...@@ -98,7 +125,7 @@ class Request { ...@@ -98,7 +125,7 @@ class Request {
* *
* @return bool * @return bool
*/ */
public static function is_secure() public static function secure()
{ {
return (static::protocol() == 'https'); return (static::protocol() == 'https');
} }
...@@ -118,9 +145,25 @@ class Request { ...@@ -118,9 +145,25 @@ class Request {
* *
* @return bool * @return bool
*/ */
public static function is_ajax() public static function ajax()
{ {
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and Str::lower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and Str::lower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
} }
/**
* Magic Method to handle dynamic static methods.
*/
public static function __callStatic($method, $parameters)
{
// --------------------------------------------------------------
// Dynamically call the "is" method using the given name.
//
// Example: Request::is_login()
// --------------------------------------------------------------
if (strpos($method, 'is_') === 0)
{
return static::is(substr($method, 3));
}
}
} }
\ No newline at end of file
...@@ -2,12 +2,19 @@ ...@@ -2,12 +2,19 @@
class Route { class Route {
/**
* The route key, including request method and URI.
*
* @var string
*/
public $key;
/** /**
* The route callback or array. * The route callback or array.
* *
* @var mixed * @var mixed
*/ */
public $route; public $callback;
/** /**
* The parameters that will passed to the route function. * The parameters that will passed to the route function.
...@@ -19,13 +26,15 @@ class Route { ...@@ -19,13 +26,15 @@ class Route {
/** /**
* Create a new Route instance. * Create a new Route instance.
* *
* @param mixed $route * @param string $key
* @param array $parameters * @param mixed $callback
* @param array $parameters
* @return void * @return void
*/ */
public function __construct($route, $parameters = array()) public function __construct($key, $callback, $parameters = array())
{ {
$this->route = $route; $this->key = $key;
$this->callback = $callback;
$this->parameters = $parameters; $this->parameters = $parameters;
} }
...@@ -44,34 +53,34 @@ class Route { ...@@ -44,34 +53,34 @@ class Route {
// If the route value is just a function, all we have to do // If the route value is just a function, all we have to do
// is execute the function! There are no filters to call. // is execute the function! There are no filters to call.
// ------------------------------------------------------------ // ------------------------------------------------------------
if (is_callable($this->route)) if (is_callable($this->callback))
{ {
$response = call_user_func_array($this->route, $this->parameters); $response = call_user_func_array($this->callback, $this->parameters);
} }
// ------------------------------------------------------------ // ------------------------------------------------------------
// If the route value is an array, we'll need to check it for // If the route value is an array, we'll need to check it for
// any filters that may be attached. // any filters that may be attached.
// ------------------------------------------------------------ // ------------------------------------------------------------
elseif (is_array($this->route)) elseif (is_array($this->callback))
{ {
$response = isset($this->route['before']) ? Filter::call($this->route['before'], array(), true) : null; $response = isset($this->callback['before']) ? Filter::call($this->callback['before'], array(), true) : null;
// ------------------------------------------------------------ // ------------------------------------------------------------
// We verify that the before filters did not return a response // We verify that the before filters did not return a response
// Before filters can override the request cycle to make things // Before filters can override the request cycle to make things
// like authentication convenient to implement. // like authentication convenient to implement.
// ------------------------------------------------------------ // ------------------------------------------------------------
if (is_null($response) and isset($this->route['do'])) if (is_null($response) and isset($this->callback['do']))
{ {
$response = call_user_func_array($this->route['do'], $this->parameters); $response = call_user_func_array($this->callback['do'], $this->parameters);
} }
} }
$response = Response::prepare($response); $response = Response::prepare($response);
if (is_array($this->route) and isset($this->route['after'])) if (is_array($this->callback) and isset($this->callback['after']))
{ {
Filter::call($this->route['after'], array($response)); Filter::call($this->callback['after'], array($response));
} }
return $response; return $response;
......
...@@ -33,7 +33,7 @@ class Router { ...@@ -33,7 +33,7 @@ class Router {
// -------------------------------------------------------------- // --------------------------------------------------------------
if (isset(static::$routes[$method.' '.$uri])) if (isset(static::$routes[$method.' '.$uri]))
{ {
return new Route(static::$routes[$method.' '.$uri]); return Request::$route = new Route($method.' '.$uri, static::$routes[$method.' '.$uri]);
} }
// -------------------------------------------------------------- // --------------------------------------------------------------
...@@ -50,13 +50,13 @@ class Router { ...@@ -50,13 +50,13 @@ class Router {
// -------------------------------------------------------------- // --------------------------------------------------------------
// Routes can be comma-delimited, so spin through each one. // Routes can be comma-delimited, so spin through each one.
// -------------------------------------------------------------- // --------------------------------------------------------------
foreach (explode(', ', $keys) as $route) foreach (explode(', ', $keys) as $key)
{ {
$route = str_replace(':num', '[0-9]+', str_replace(':any', '[a-zA-Z0-9\-_]+', $route)); $key = str_replace(':num', '[0-9]+', str_replace(':any', '[a-zA-Z0-9\-_]+', $key));
if (preg_match('#^'.$route.'$#', $method.' '.$uri)) if (preg_match('#^'.$key.'$#', $method.' '.$uri))
{ {
return new Route($callback, Route\Parser::parameters($uri, $route)); return Request::$route = new Route($key, $callback, Route\Parser::parameters($uri, $key));
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment