From 4cf7f0c627886189e648dc618b3e9e4c9b626189 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 31 Jan 2012 15:19:23 -0600 Subject: [PATCH] added cookie jar that holds cookies until end of request. --- laravel/cookie.php | 43 ++++++++++++++++++++++++++++++++++++------- laravel/laravel.php | 16 ++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/laravel/cookie.php b/laravel/cookie.php index c5e859e3..0a68b133 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -9,6 +9,13 @@ if (trim(Config::get('application.key')) === '') class Cookie { + /** + * The cookies that have been set. + * + * @var array + */ + public static $jar = array(); + /** * Determine if a cookie exists. * @@ -82,22 +89,44 @@ class Cookie { */ public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false) { - if (headers_sent()) return false; - $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0; - $_COOKIE[$name] = static::sign($name, $value); + $_COOKIE[$name] = $value = static::sign($name, $value); // A cookie payload can't exceed 4096 bytes, so if the payload // is greater than that, we'll raise an exception to warn the - // developer of the problem since it may cause problems with - // the application, especially if using cookie sessions. - if (strlen($_COOKIE[$name]) > 4000) + // developer of the problem since it may cause bad problems. + if (strlen($value) > 4000) { throw new \Exception("Payload too large for cookie."); } - return setcookie($name, $_COOKIE[$name], $time, $path, $domain, $secure); + static::$jar[$name] = compact( + + 'name', 'value', 'time', 'path', 'domain', 'secure' + + ); + } + + /** + * Send all of the cookies to the browser. + * + * @return void + */ + public static function send() + { + if (headers_sent()) return false; + + // All cookies are stored in the "jar" when set and not sent + // immediately to the browser. This just makes testing the + // cookie functionality of an application much easier, as + // the jar can be inspected by the developer. + foreach (static::$jar as $cookie) + { + extract($cookie); + + setcookie($name, $value, $time, $path, $domain, $secure); + } } /** diff --git a/laravel/laravel.php b/laravel/laravel.php index 5125a0de..93cd373c 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -181,6 +181,22 @@ if (Config::get('session.driver') !== '') Session::save(); } +/** + * Send all of the cookies to the browser. The cookies are + * stored in a "jar" until the end of a request, primarily + * to make testing the cookie functionality of the site + * much easier since the jar can be inspected. + */ +if (Config::get('application.key') !== '') +{ + Cookie::send(); +} + +/** + * Send the final response to the browser and fire the + * final event indicating that the processing for the + * current request is completed. + */ $response->send(); Event::fire('laravel: done'); \ No newline at end of file