Commit a3a93687 authored by Taylor Otwell's avatar Taylor Otwell

added dynamic Request::is_ method.. e.g. Request::is_login().

parent 7f06e0f4
......@@ -9,6 +9,13 @@ class Request {
*/
public static $uri;
/**
* The route handling the current request.
*
* @var Route
*/
public static $route;
/**
* Get the request URI.
*
......@@ -68,28 +75,14 @@ class Request {
}
/**
* Check the URI against a string or set of strings.
* Determine if the route handling the request is a given name.
*
* @param string $name
* @return bool
*/
public static function is()
public static function is($name)
{
$parameters = func_get_args();
// -------------------------------------------------------
// If any of the parameters match the URI, return true.
// -------------------------------------------------------
if (count($parameters) > 1)
{
return in_array(static::uri(), $parameters);
}
if (count($parameters) === 1)
{
return static::uri() == $parameters[0];
}
return false;
return (is_array(static::$route->callback) and isset(static::$route->callback['name']) and static::$route->callback['name'] === $name);
}
/**
......@@ -132,7 +125,7 @@ class Request {
*
* @return bool
*/
public static function is_secure()
public static function secure()
{
return (static::protocol() == 'https');
}
......@@ -152,9 +145,25 @@ class Request {
*
* @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');
}
/**
* 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 @@
class Route {
/**
* The route key, including request method and URI.
*
* @var string
*/
public $key;
/**
* The route callback or array.
*
* @var mixed
*/
public $route;
public $callback;
/**
* The parameters that will passed to the route function.
......@@ -19,13 +26,15 @@ class Route {
/**
* Create a new Route instance.
*
* @param mixed $route
* @param array $parameters
* @param string $key
* @param mixed $callback
* @param array $parameters
* @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;
}
......@@ -44,34 +53,34 @@ class Route {
// If the route value is just a function, all we have to do
// 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
// 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
// Before filters can override the request cycle to make things
// 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);
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;
......
......@@ -33,7 +33,7 @@ class Router {
// --------------------------------------------------------------
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 {
// --------------------------------------------------------------
// 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