From 2b12c0c140663b157ccc9affc74ea71cffe2e86b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 31 Jan 2012 15:58:00 -0600 Subject: [PATCH] modified cookie class. set application key on first request if not set. --- application/config/application.php | 2 +- laravel/cookie.php | 83 +++++++++++++----------------- laravel/crypter.php | 5 -- laravel/laravel.php | 24 +++++++-- laravel/session.php | 5 -- laravel/session/payload.php | 5 -- 6 files changed, 58 insertions(+), 66 deletions(-) diff --git a/application/config/application.php b/application/config/application.php index 45382e5d..b2ddacc8 100644 --- a/application/config/application.php +++ b/application/config/application.php @@ -153,4 +153,4 @@ 'View' => 'Laravel\\View', ), -); +); \ No newline at end of file diff --git a/laravel/cookie.php b/laravel/cookie.php index 0a68b133..c9ab92f9 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -2,11 +2,6 @@ use Closure; -if (trim(Config::get('application.key')) === '') -{ - throw new \Exception('The cookie class may not be used without an application key.'); -} - class Cookie { /** @@ -27,6 +22,39 @@ public static function has($name) return ! is_null(static::get($name)); } + /** + * 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 + // directly to the browser. This simply makes testing all of + // the cookie functionality easier since the cooke jar can + // be inspected by the developer in tests. + foreach (static::$jar as $cookie) + { + extract($cookie); + + $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0; + + // A cookie payload can't exceed 4096 bytes, so if the + // payload is greater than that, we'll raise an error + // to warn the developer. + $value = static::sign($name, $value); + + if (strlen($value) > 4000) + { + throw new \Exception("Payload too large for cookie."); + } + + setcookie($name, $value, $time, $path, $domain, $secure); + } + } + /** * Get the value of a cookie. * @@ -34,7 +62,7 @@ public static function has($name) * // Get the value of the "favorite" cookie * $favorite = Cookie::get('favorite'); * - * // Get the value of a cookie or return a default value if it doesn't exist + * // Get the value of a cookie or return a default value * $favorite = Cookie::get('framework', 'Laravel'); * * @@ -44,6 +72,8 @@ public static function has($name) */ public static function get($name, $default = null) { + if (isset(static::$jar[$name])) return static::$jar[$name]; + $value = array_get($_COOKIE, $name); if ( ! is_null($value) and isset($value[40]) and $value[40] == '~') @@ -69,8 +99,6 @@ public static function get($name, $default = null) /** * Set the value of a cookie. * - * If the response headers have already been sent, the cookie will not be set. - * * * // Set the value of the "favorite" cookie * Cookie::put('favorite', 'Laravel'); @@ -89,44 +117,7 @@ public static function get($name, $default = null) */ public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false) { - $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0; - - $_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 bad problems. - if (strlen($value) > 4000) - { - throw new \Exception("Payload too large for cookie."); - } - - 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); - } + static::$jar[$name] = compact('name', 'value', 'minutes', 'path', 'domain', 'secure'); } /** diff --git a/laravel/crypter.php b/laravel/crypter.php index 6ddea304..ad01e175 100644 --- a/laravel/crypter.php +++ b/laravel/crypter.php @@ -1,10 +1,5 @@ ''", "'key' => '{$key}'", $config); + + File::put(path('app').'config/application'.EXT, $config); +} + /** * Register the default timezone for the application. This will be the * default timezone used by all date / timezone functions throughout @@ -187,10 +206,7 @@ * to make testing the cookie functionality of the site * much easier since the jar can be inspected. */ -if (Config::get('application.key') !== '') -{ - Cookie::send(); -} +Cookie::send(); /** * Send the final response to the browser and fire the diff --git a/laravel/session.php b/laravel/session.php index b108772d..817e32a5 100644 --- a/laravel/session.php +++ b/laravel/session.php @@ -24,11 +24,6 @@ class Session { */ public static function start($driver) { - if (Config::get('application.key') === '') - { - throw new \Exception("An application key is required to use sessions."); - } - static::$instance = new Session\Payload(static::factory($driver)); } diff --git a/laravel/session/payload.php b/laravel/session/payload.php index e6f55713..15b341c5 100644 --- a/laravel/session/payload.php +++ b/laravel/session/payload.php @@ -8,11 +8,6 @@ use Laravel\Session\Drivers\Driver; use Laravel\Session\Drivers\Sweeper; -if (Config::get('application.key') === '') -{ - throw new \Exception("An application key is required to use sessions."); -} - class Payload { /**