From d446ccc82fe63481a08d0f4f5041f423db52e06d Mon Sep 17 00:00:00 2001 From: Pedro Borges Date: Sat, 18 Jun 2011 11:54:59 -0300 Subject: [PATCH 1/5] Added the method 'is' to request.php --- system/request.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/system/request.php b/system/request.php index e56e77e8..d8b69625 100644 --- a/system/request.php +++ b/system/request.php @@ -58,6 +58,24 @@ class Request { return ($uri == '') ? '/' : Str::lower($uri); } + /** + * Check the request URI. + * + * @param mixed $uri + * @return bool + */ + public static function is($uri) + { + if (is_array($uri)) + { + return (in_array(static::uri(), $uri)) ? true : false; + } + else + { + return (static::uri() == $uri) ? true : false; + } + } + /** * Get the request method. * From 396d1487209a8d5bf7d7f6ac24b68bb4cdf7cdf7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 18 Jun 2011 10:26:04 -0500 Subject: [PATCH 2/5] added is method, improved comments. --- system/request.php | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/system/request.php b/system/request.php index d8b69625..05f59f2b 100644 --- a/system/request.php +++ b/system/request.php @@ -2,6 +2,13 @@ class Request { + /** + * The request URI. + * + * @var string + */ + private static $uri; + /** * Get the request URI. * @@ -9,8 +16,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 +40,6 @@ class Request { 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.'); @@ -59,21 +68,28 @@ class Request { } /** - * Check the request URI. + * Check the URI against a string or set of strings. * - * @param mixed $uri * @return bool */ - public static function is($uri) + public static function is() { - if (is_array($uri)) + $parameters = func_get_args(); + + // ------------------------------------------------------- + // If any of the parameters match the URI, return true. + // ------------------------------------------------------- + if (count($parameters) > 1) { - return (in_array(static::uri(), $uri)) ? true : false; + return in_array(static::uri(), $parameters); } - else + + if (count($parameters) === 1) { - return (static::uri() == $uri) ? true : false; + return static::uri() == $parameters[0]; } + + return false; } /** From 7f06e0f4cd3bc12c20d3ecd5ab2b82c9999ce228 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 18 Jun 2011 10:38:56 -0500 Subject: [PATCH 3/5] added request::is. --- system/request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/request.php b/system/request.php index 05f59f2b..a6a9a13d 100644 --- a/system/request.php +++ b/system/request.php @@ -7,7 +7,7 @@ class Request { * * @var string */ - private static $uri; + public static $uri; /** * Get the request URI. From a3a936870799ceac09c412117689db1a4e8533a3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 18 Jun 2011 12:00:52 -0500 Subject: [PATCH 4/5] added dynamic Request::is_ method.. e.g. Request::is_login(). --- system/request.php | 49 +++++++++++++++++++++++++++------------------- system/route.php | 35 +++++++++++++++++++++------------ system/router.php | 10 +++++----- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/system/request.php b/system/request.php index a6a9a13d..5e933bee 100644 --- a/system/request.php +++ b/system/request.php @@ -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 diff --git a/system/route.php b/system/route.php index 60800371..8210a7f3 100644 --- a/system/route.php +++ b/system/route.php @@ -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; diff --git a/system/router.php b/system/router.php index 120d15ca..3bb18981 100644 --- a/system/router.php +++ b/system/router.php @@ -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)); } } } From fcd6cd77efe26f94d4a9c42173d60adcc524c0ed Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 18 Jun 2011 16:44:34 -0500 Subject: [PATCH 5/5] fixed messed up commit of file cache driver. --- system/cache/driver/file.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/system/cache/driver/file.php b/system/cache/driver/file.php index dfe4eb55..852bdb15 100644 --- a/system/cache/driver/file.php +++ b/system/cache/driver/file.php @@ -1,6 +1,6 @@ items)) @@ -34,14 +34,25 @@ class Memcached implements \System\Cache\Driver { return $this->items[$key]; } - $cache = \System\Memcached::instance()->get(\System\Config::get('cache.key').$key); - - if ($cache === false) + if ( ! file_exists(APP_PATH.'cache/'.$key)) { return $default; } - return $this->items[$key] = $cache; + $cache = file_get_contents(APP_PATH.'cache/'.$key); + + // -------------------------------------------------- + // Has the cache expired? The UNIX expiration time + // is stored at the beginning of the file. + // -------------------------------------------------- + if (time() >= substr($cache, 0, 10)) + { + $this->forget($key); + + return $default; + } + + return $this->items[$key] = unserialize(substr($cache, 10)); } /** @@ -54,7 +65,7 @@ class Memcached implements \System\Cache\Driver { */ public function put($key, $value, $minutes) { - \System\Memcached::instance()->set(\System\Config::get('cache.key').$key, $value, 0, $minutes * 60); + file_put_contents(APP_PATH.'cache/'.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX); } /** @@ -65,7 +76,7 @@ class Memcached implements \System\Cache\Driver { */ public function forget($key) { - \System\Memcached::instance()->delete(\System\Config::get('cache.key').$key); + @unlink(APP_PATH.'cache/'.$key); } } \ No newline at end of file