Merge branch 'develop'
This commit is contained in:
commit
dead6c0a7b
|
@ -2,6 +2,20 @@
|
|||
|
||||
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.
|
||||
*
|
||||
|
@ -9,8 +23,13 @@ class Request {
|
|||
*/
|
||||
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']))
|
||||
{
|
||||
|
@ -28,9 +47,6 @@ public static function uri()
|
|||
throw new \Exception("Malformed request URI. Request terminated.");
|
||||
}
|
||||
}
|
||||
// -------------------------------------------------------
|
||||
// Neither PATH_INFO or REQUEST_URI are available.
|
||||
// -------------------------------------------------------
|
||||
else
|
||||
{
|
||||
throw new \Exception('Unable to determine the request URI.');
|
||||
|
@ -58,6 +74,17 @@ public static function 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.
|
||||
*
|
||||
|
@ -98,7 +125,7 @@ public static function ip()
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_secure()
|
||||
public static function secure()
|
||||
{
|
||||
return (static::protocol() == 'https');
|
||||
}
|
||||
|
@ -118,9 +145,25 @@ public static function protocol()
|
|||
*
|
||||
* @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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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 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 @@ public function call()
|
|||
// 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 @@ public static function route($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 @@ public static function route($method, $uri)
|
|||
// --------------------------------------------------------------
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue