From 3408bb8492ef49f2666074a35d33fddb4348f476 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 17 Jun 2011 00:02:24 -0500 Subject: [PATCH] overall code refactoring, comment improvement. --- system/form.php | 2 +- system/lang.php | 24 +++++----- system/redirect.php | 26 +++++------ system/request.php | 25 ++--------- system/response.php | 24 +--------- system/route.php | 28 ++++++------ system/router.php | 11 ++--- system/session.php | 104 +++++++++++++++++++++++--------------------- system/url.php | 9 ++-- system/view.php | 5 +-- 10 files changed, 108 insertions(+), 150 deletions(-) diff --git a/system/form.php b/system/form.php index 3b327a76..2e3ffb42 100644 --- a/system/form.php +++ b/system/form.php @@ -44,7 +44,7 @@ class Form { // ------------------------------------------------------- if ($method == 'PUT' or $method == 'DELETE') { - $html .= PHP_EOL.static::hidden('request_method', $method); + $html .= PHP_EOL.static::hidden('REQUEST_METHOD', $method); } return $html.PHP_EOL; diff --git a/system/lang.php b/system/lang.php index 2f5e8fcc..6778c5eb 100644 --- a/system/lang.php +++ b/system/lang.php @@ -12,6 +12,8 @@ class Lang { /** * All of the loaded language lines. * + * The array is keyed by [$language.$file]. + * * @var array */ private static $lines = array(); @@ -65,18 +67,12 @@ class Lang { $language = Config::get('application.language'); } - // ----------------------------------------------------- - // Parse the key to separate the file and key name. - // ----------------------------------------------------- list($file, $line) = $this->parse($this->key); - // ----------------------------------------------------- - // Load the appropriate language file. - // ----------------------------------------------------- $this->load($file, $language); // -------------------------------------------------------------- - // Get the language line. + // Get the language line from the appropriate file array. // -------------------------------------------------------------- if (array_key_exists($line, static::$lines[$language.$file])) { @@ -88,7 +84,8 @@ class Lang { } // -------------------------------------------------------------- - // Make all place-holder replacements. + // Make all place-holder replacements. Place-holders are prefixed + // with a colon for convenient location. // -------------------------------------------------------------- foreach ($this->replacements as $key => $value) { @@ -106,6 +103,10 @@ class Lang { */ private function parse($key) { + // -------------------------------------------------------------- + // The left side of the dot is the file name, while the right + // side of the dot is the item within that file being requested. + // -------------------------------------------------------------- $segments = explode('.', $key); if (count($segments) < 2) @@ -113,10 +114,6 @@ class Lang { throw new \Exception("Invalid language key [$key]."); } - // -------------------------------------------------------------- - // The left side of the dot is the file name, while the right - // side of the dot is the item within that file being requested. - // -------------------------------------------------------------- return array($segments[0], implode('.', array_slice($segments, 1))); } @@ -138,7 +135,8 @@ class Lang { } // -------------------------------------------------------------- - // Load the language file into the array of lines. + // Load the language file into the array of lines. The array + // is keyed by the language and file name. // -------------------------------------------------------------- if (file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT)) { diff --git a/system/redirect.php b/system/redirect.php index 3790844f..51662493 100644 --- a/system/redirect.php +++ b/system/redirect.php @@ -38,6 +38,19 @@ class Redirect { : new static(Response::make('', $status)->header('Location', $url)); } + /** + * Create a redirect response to a HTTPS URL. + * + * @param string $url + * @param string $method + * @param int $status + * @return Response + */ + public static function to_secure($url, $method = 'location', $status = 302) + { + return static::to($url, $method, $status, true); + } + /** * Add an item to the session flash data. * @@ -59,19 +72,6 @@ class Redirect { return $this; } - /** - * Create a redirect response to a HTTPS URL. - * - * @param string $url - * @param string $method - * @param int $status - * @return Response - */ - public static function to_secure($url, $method = 'location', $status = 302) - { - return static::to($url, $method, $status, true); - } - /** * Magic Method to handle redirecting to routes. */ diff --git a/system/request.php b/system/request.php index bf439796..e56e77e8 100644 --- a/system/request.php +++ b/system/request.php @@ -2,13 +2,6 @@ class Request { - /** - * The request URI. - * - * @var string - */ - public static $uri; - /** * Get the request URI. * @@ -16,14 +9,6 @@ class Request { */ public static function uri() { - // ------------------------------------------------------- - // If we have already determined the URI, return it. - // ------------------------------------------------------- - if ( ! is_null(static::$uri)) - { - return static::$uri; - } - // ------------------------------------------------------- // If the PATH_INFO is available, use it. // ------------------------------------------------------- @@ -62,17 +47,15 @@ class Request { } // ------------------------------------------------------- - // Remove the application index. + // Remove the application index and any extra slashes. // ------------------------------------------------------- - $uri = str_replace('/index.php', '', $uri); - - $uri = trim($uri, '/'); + $uri = trim(str_replace('/index.php', '', $uri), '/'); // ------------------------------------------------------- // If the requests is to the root of the application, we // always return a single forward slash. // ------------------------------------------------------- - return static::$uri = ($uri == '') ? '/' : Str::lower($uri); + return ($uri == '') ? '/' : Str::lower($uri); } /** @@ -86,7 +69,7 @@ class Request { // The method can be spoofed using a POST variable, allowing HTML // forms to simulate PUT and DELETE requests. // -------------------------------------------------------------- - return (isset($_POST['request_method'])) ? $_POST['request_method'] : $_SERVER['REQUEST_METHOD']; + return Arr::get($_POST, 'REQUEST_METHOD', $_SERVER['REQUEST_METHOD']); } /** diff --git a/system/response.php b/system/response.php index c4468e44..a3d1b5cc 100644 --- a/system/response.php +++ b/system/response.php @@ -10,7 +10,7 @@ class Response { public $content; /** - * The HTTP status code. + * The HTTP status code of the response. * * @var int */ @@ -111,15 +111,13 @@ class Response { { // -------------------------------------------------------------- // If the response is a Redirect instance, grab the Response. + // The Redirect class manages a Response instance internally. // -------------------------------------------------------------- if ($response instanceof Redirect) { $response = $response->response; } - // -------------------------------------------------------------- - // Make sure the response is an instance of the Response class. - // -------------------------------------------------------------- return ( ! $response instanceof Response) ? new static($response) : $response; } @@ -130,25 +128,16 @@ class Response { */ public function send() { - // ------------------------------------------------- - // If a Content-Type header has not been set, do it. - // ------------------------------------------------- if ( ! array_key_exists('Content-Type', $this->headers)) { $this->header('Content-Type', 'text/html; charset=utf-8'); } - // ------------------------------------------------- - // Send the headers to the browser. - // ------------------------------------------------- if ( ! headers_sent()) { $this->send_headers(); } - // ------------------------------------------------- - // Send the content of the response to the browser. - // ------------------------------------------------- echo (string) $this->content; } @@ -159,19 +148,10 @@ class Response { */ public function send_headers() { - // ------------------------------------------------- - // Get the proper protocol. - // ------------------------------------------------- $protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; - // ------------------------------------------------- - // Send the protocol and status header. - // ------------------------------------------------- header($protocol.' '.$this->status.' '.$this->statuses[$this->status]); - // ------------------------------------------------- - // Send the rest of the response headers. - // ------------------------------------------------- foreach ($this->headers as $name => $value) { header($name.': '.$value, true); diff --git a/system/route.php b/system/route.php index f778d90f..60800371 100644 --- a/system/route.php +++ b/system/route.php @@ -40,26 +40,27 @@ class Route { { $response = null; - // -------------------------------------------------------------- - // If the route just has a callback, call it. - // -------------------------------------------------------------- + // ------------------------------------------------------------ + // 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)) { $response = call_user_func_array($this->route, $this->parameters); } - // -------------------------------------------------------------- - // The route value is an array. We'll need to evaluate it. - // -------------------------------------------------------------- + // ------------------------------------------------------------ + // 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)) { - // -------------------------------------------------------------- - // Call the "before" route filters. - // -------------------------------------------------------------- $response = isset($this->route['before']) ? Filter::call($this->route['before'], array(), true) : null; - // -------------------------------------------------------------- - // Call the route callback. - // -------------------------------------------------------------- + // ------------------------------------------------------------ + // 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'])) { $response = call_user_func_array($this->route['do'], $this->parameters); @@ -68,9 +69,6 @@ class Route { $response = Response::prepare($response); - // -------------------------------------------------------------- - // Call the "after" route filters. - // -------------------------------------------------------------- if (is_array($this->route) and isset($this->route['after'])) { Filter::call($this->route['after'], array($response)); diff --git a/system/router.php b/system/router.php index 8a2a6a30..120d15ca 100644 --- a/system/router.php +++ b/system/router.php @@ -19,13 +19,10 @@ class Router { public static function route($method, $uri) { // -------------------------------------------------------------- - // Force the URI to have a forward slash. + // Prepend a forward slash since all routes begin with one. // -------------------------------------------------------------- $uri = ($uri != '/') ? '/'.$uri : $uri; - // -------------------------------------------------------------- - // Load the application routes. - // -------------------------------------------------------------- if (is_null(static::$routes)) { static::$routes = Route\Loader::load($uri); @@ -50,11 +47,11 @@ class Router { // -------------------------------------------------------------- if (strpos($keys, '(') !== false or strpos($keys, ',') !== false ) { + // -------------------------------------------------------------- + // Routes can be comma-delimited, so spin through each one. + // -------------------------------------------------------------- foreach (explode(', ', $keys) as $route) { - // -------------------------------------------------------------- - // Convert the route wild-cards to regular expressions. - // -------------------------------------------------------------- $route = str_replace(':num', '[0-9]+', str_replace(':any', '[a-zA-Z0-9\-_]+', $route)); if (preg_match('#^'.$route.'$#', $method.' '.$uri)) diff --git a/system/session.php b/system/session.php index 6c958390..1a6dc681 100644 --- a/system/session.php +++ b/system/session.php @@ -17,8 +17,7 @@ class Session { private static $session = array(); /** - * Get the session driver. If the driver has already been instantiated, that - * instance will be returned. + * Get the session driver. * * @return Session\Driver */ @@ -39,26 +38,25 @@ class Session { */ public static function load() { - // ----------------------------------------------------- - // If a valid ID is present, load the session. - // ----------------------------------------------------- if ( ! is_null($id = Cookie::get('laravel_session'))) { static::$session = static::driver()->load($id); } - // ----------------------------------------------------- - // If the session is invalid, start a new one. - // ----------------------------------------------------- - if (is_null($id) or is_null(static::$session) or (time() - static::$session['last_activity']) > (Config::get('session.lifetime') * 60)) + // --------------------------------------------------------- + // If the session is invalid or expired, start a new one. + // --------------------------------------------------------- + if (is_null($id) or is_null(static::$session) or static::expired(static::$session['last_activity'])) { static::$session['id'] = Str::random(40); static::$session['data'] = array(); } - // ----------------------------------------------------- - // Create a CSRF token for the session if necessary. - // ----------------------------------------------------- + // --------------------------------------------------------- + // Create a CSRF token for the session if necessary. This + // token is used by the Form class and filters to protect + // against cross-site request forgeries. + // --------------------------------------------------------- if ( ! static::has('csrf_token')) { static::put('csrf_token', Str::random(16)); @@ -67,6 +65,17 @@ class Session { static::$session['last_activity'] = time(); } + /** + * Determine if a session has expired based on the last activity. + * + * @param int $last_activity + * @return bool + */ + private static function expired($last_activity) + { + return (time() - $last_activity) > (Config::get('session.lifetime') * 60); + } + /** * Determine if the session or flash data contains an item. * @@ -88,20 +97,17 @@ class Session { */ public static function get($key, $default = null) { - if (static::has($key)) + if (array_key_exists($key, static::$session['data'])) { - if (array_key_exists($key, static::$session['data'])) - { - return static::$session['data'][$key]; - } - elseif (array_key_exists(':old:'.$key, static::$session['data'])) - { - return static::$session['data'][':old:'.$key]; - } - elseif (array_key_exists(':new:'.$key, static::$session['data'])) - { - return static::$session['data'][':new:'.$key]; - } + return static::$session['data'][$key]; + } + elseif (array_key_exists(':old:'.$key, static::$session['data'])) + { + return static::$session['data'][':old:'.$key]; + } + elseif (array_key_exists(':new:'.$key, static::$session['data'])) + { + return static::$session['data'][':new:'.$key]; } return $default; @@ -159,7 +165,15 @@ class Session { */ public static function regenerate() { + // --------------------------------------------------------- + // When regenerating the session ID, we go ahead and delete + // the session data from storage. Then, we assign a new ID. + // + // The session will be re-written to storage at the end + // of the request to the application. + // --------------------------------------------------------- static::driver()->delete(static::$session['id']); + static::$session['id'] = Str::random(40); } @@ -170,30 +184,26 @@ class Session { */ public static function close() { - // ----------------------------------------------------- - // Flash the old input to the session and age the flash. - // ----------------------------------------------------- + // --------------------------------------------------------- + // Flash the old input data to the session. This allows + // the Input::old method to retrieve input from the + // previous request made by the user. + // --------------------------------------------------------- static::flash('laravel_old_input', Input::get()); static::age_flash(); - // ----------------------------------------------------- - // Write the session data to storage. - // ----------------------------------------------------- static::driver()->save(static::$session); - // ----------------------------------------------------- - // Set the session cookie. - // ----------------------------------------------------- + // --------------------------------------------------------- + // Send the session cookie the browser so we can remember + // who the session belongs to on subsequent requests. + // --------------------------------------------------------- if ( ! headers_sent()) { $cookie = new Cookie('laravel_session', static::$session['id']); - if ( ! Config::get('session.expire_on_close')) - { - $cookie->lifetime = Config::get('session.lifetime'); - } - + $cookie->lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime'); $cookie->path = Config::get('session.path'); $cookie->domain = Config::get('session.domain'); $cookie->secure = Config::get('session.https'); @@ -201,9 +211,10 @@ class Session { $cookie->send(); } - // ----------------------------------------------------- + // --------------------------------------------------------- // Perform session garbage collection (2% chance). - // ----------------------------------------------------- + // Session garbage collection removes all expired sessions. + // --------------------------------------------------------- if (mt_rand(1, 100) <= 2) { static::driver()->sweep(time() - (Config::get('session.lifetime') * 60)); @@ -218,7 +229,7 @@ class Session { private static function age_flash() { // ----------------------------------------------------- - // Expire all of the old flash data. + // Remove all of the :old: items from the session. // ----------------------------------------------------- foreach (static::$session['data'] as $key => $value) { @@ -229,20 +240,15 @@ class Session { } // ----------------------------------------------------- - // Age all of the new flash data. + // Copy all of the :new: items to :old: items and then + // remove the :new: items from the session. // ----------------------------------------------------- foreach (static::$session['data'] as $key => $value) { if (strpos($key, ':new:') === 0) { - // ----------------------------------------------------- - // Create an :old: item for the :new: item. - // ----------------------------------------------------- static::put(':old:'.substr($key, 5), $value); - // ----------------------------------------------------- - // Forget the :new: item. - // ----------------------------------------------------- static::forget($key); } } diff --git a/system/url.php b/system/url.php index a3643a44..fc383be1 100644 --- a/system/url.php +++ b/system/url.php @@ -20,9 +20,6 @@ class URL { return $url; } - // ---------------------------------------------------- - // Get the base URL and index page. - // ---------------------------------------------------- $base = Config::get('application.url'); // ---------------------------------------------------- @@ -89,11 +86,13 @@ class URL { $uri = substr($uris[0], strpos($uris[0], '/')); // ---------------------------------------------------- - // Replace any parameters in the URI. + // Replace any parameters in the URI. This allows + // the dynamic creation of URLs that contain parameter + // wildcards. // ---------------------------------------------------- foreach ($parameters as $parameter) { - $uri = preg_replace('/\(\:any\)|\(\:num\)|\(.+\)/', $parameter, $uri, 1); + $uri = preg_replace('/\(.+\)/', $parameter, $uri, 1); } return static::to($uri, $https); diff --git a/system/view.php b/system/view.php index 1cc2eae0..7a314211 100644 --- a/system/view.php +++ b/system/view.php @@ -55,13 +55,10 @@ class View { */ public function get() { - // ----------------------------------------------------- - // Set the last rendered view name to the current view. - // ----------------------------------------------------- static::$last = $this->view; // ----------------------------------------------------- - // Get the content of all of the sub-views. + // Get the evaluated content of all of the sub-views. // ----------------------------------------------------- foreach ($this->data as &$data) {