From 730808fb02ace6fb154abb3472ef6cf5d454e4aa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Mar 2012 22:43:58 -0500 Subject: [PATCH] converting cookies to use http foundation. --- laravel/cookie.php | 107 +++++-------------------------------------- laravel/input.php | 32 ++----------- laravel/laravel.php | 13 ------ laravel/request.php | 20 ++++++-- laravel/response.php | 42 +++++++++++++++-- 5 files changed, 67 insertions(+), 147 deletions(-) diff --git a/laravel/cookie.php b/laravel/cookie.php index fc48212e..bafe6422 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -1,6 +1,4 @@ - 4000) - { - throw new \Exception("Payload too large for cookie."); - } - else - { - setcookie($name, $value, $time, $path, $domain, $secure); - } - } - - /** * Get the value of a cookie. * @@ -85,27 +37,9 @@ protected static function set($cookie) */ public static function get($name, $default = null) { - if (isset(static::$jar[$name])) return static::$jar[$name]['value']; + 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] == '~') - { - // The hash signature and the cookie value are separated by a tilde - // character for convenience. To separate the hash and the payload - // we can simply expode on that character. - list($hash, $value) = explode('~', $value, 2); - - // By re-feeding the cookie value into the "hash" method we should - // be able to generate a hash that matches the one taken from the - // cookie. If they don't, we return null. - if (static::hash($name, $value) === $hash) - { - return $value; - } - } - - return value($default); + return array_get(Request::foundation()->cookies->all(), $name, $default); } /** @@ -121,15 +55,20 @@ public static function get($name, $default = null) * * @param string $name * @param string $value - * @param int $minutes + * @param int $expiration * @param string $path * @param string $domain * @param bool $secure * @return void */ - public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false) + public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false) { - static::$jar[$name] = compact('name', 'value', 'minutes', 'path', 'domain', 'secure'); + if ($expiration !== 0) + { + $expiration = time() + ($expiration * 60); + } + + static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure'); } /** @@ -152,30 +91,6 @@ public static function forever($name, $value, $path = '/', $domain = null, $secu return static::put($name, $value, 525600, $path, $domain, $secure); } - /** - * Generate a cookie signature based on the contents. - * - * @param string $name - * @param string $value - * @return string - */ - public static function sign($name, $value) - { - return static::hash($name, $value).'~'.$value; - } - - /** - * Generate a cookie hash based on the contents. - * - * @param string $name - * @param string $value - * @return string - */ - protected static function hash($name, $value) - { - return sha1($name.$value.Config::get('application.key')); - } - /** * Delete a cookie. * diff --git a/laravel/input.php b/laravel/input.php index 8524957e..e1239e72 100644 --- a/laravel/input.php +++ b/laravel/input.php @@ -136,39 +136,15 @@ public static function old($key = null, $default = null) * * // Get the array of information for the "picture" upload * $picture = Input::file('picture'); - * - * // Get a specific element from within the file's data array - * $size = Input::file('picture.size'); * * - * @param string $key - * @param mixed $default - * @return array + * @param string $key + * @param mixed $default + * @return UploadedFile */ public static function file($key = null, $default = null) { - return array_get($_FILES, $key, $default); - } - - /** - * Move an uploaded file to permanent storage. - * - * This method is simply a convenient wrapper around move_uploaded_file. - * - * - * // Move the "picture" file to a permanent location on disk - * Input::upload('picture', 'path/to/photos/picture.jpg'); - * - * - * @param string $key - * @param string $path - * @return bool - */ - public static function upload($key, $path) - { - if (is_null(static::file($key))) return false; - - return move_uploaded_file(static::file("{$key}.tmp_name"), $path); + return array_get(Request::foundation()->files->all(), $key, $default); } /** diff --git a/laravel/laravel.php b/laravel/laravel.php index 3a90fe0d..4cd4ea00 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -208,19 +208,6 @@ Session::save(); } -/* -|-------------------------------------------------------------------------- -| Let's Eat Cookies -|-------------------------------------------------------------------------- -| -| All cookies set during the request are actually stored in a cookie jar -| until the end of the request so they can be expected by unit tests or -| the developer. Here, we'll push them out to the browser. -| -*/ - -Cookie::send(); - /* |-------------------------------------------------------------------------- | Send The Response To The Browser diff --git a/laravel/request.php b/laravel/request.php index c7344ecc..7efeadf7 100644 --- a/laravel/request.php +++ b/laravel/request.php @@ -78,7 +78,7 @@ public static function spoofed() */ public static function ip($default = '0.0.0.0') { - return value(static::$foundation->getClientIp(), $default); + return value(static::foundation()->getClientIp(), $default); } /** @@ -98,7 +98,7 @@ public static function protocol() */ public static function accept() { - return static::$foundation->getAcceptableContentTypes(); + return static::foundation()->getAcceptableContentTypes(); } /** @@ -118,7 +118,7 @@ public static function accepts($type) */ public static function secure() { - return static::$foundation->isSecure(); + return static::foundation()->isSecure(); } /** @@ -140,7 +140,7 @@ public static function forged() */ public static function ajax() { - return static::$foundation->isXmlHttpRequest(); + return static::foundation()->isXmlHttpRequest(); } /** @@ -194,6 +194,16 @@ public static function route() return static::$route; } + /** + * Get the Symfony HttpFoundation Request instance. + * + * @return HttpFoundation\Request + */ + public static function foundation() + { + return static::$foundation; + } + /** * Pass any other methods to the Symfony request. * @@ -203,7 +213,7 @@ public static function route() */ public static function __callStatic($method, $parameters) { - return call_user_func_array(array(static::$foundation, $method), $parameters); + return call_user_func_array(array(static::foundation(), $method), $parameters); } } \ No newline at end of file diff --git a/laravel/response.php b/laravel/response.php index a531938d..991931c5 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -175,9 +175,9 @@ public function render() $this->content = (string) $this->content; } - // Once we have the string content, we can set the content on - // the HttpFoundation Response instance in preparation for - // sending it back to client browser when all is done. + // Once we obtain the string content, we can set the content on + // the HttpFoundation's Response instance in preparation for + // sending it back to client browser when all is finished. $this->foundation->setContent($this->content); return $this->content; @@ -190,7 +190,9 @@ public function render() */ public function send() { - $this->foundation->prepare(Request::$foundation); + $this->cookies(); + + $this->foundation->prepare(Request::foundation()); $this->foundation->send(); } @@ -202,11 +204,31 @@ public function send() */ public function send_headers() { - $this->foundation->prepare(Request::$foundation); + $this->foundation->prepare(Request::foundation()); $this->foundation->sendHeaders(); } + /** + * Set the cookies on the HttpFoundation Response. + * + * @return void + */ + protected function cookies() + { + $ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie'); + + // All of the cookies for the response are actually stored on the + // Cookie class until we're ready to send the response back to + // the browser. This allows a cookies to be set easily. + foreach (Cookie::$jar as $name => $cookie) + { + $config = array_values($cookie); + + $this->headers()->setCookie($ref->newInstanceArgs($config)); + } + } + /** * Add a header to the array of response headers. * @@ -221,6 +243,16 @@ public function header($name, $value) return $this; } + /** + * Get the HttpFoundation Response headers. + * + * @return ResponseParameterBag + */ + public function headers() + { + return $this->foundation->headers; + } + /** * Set the response status code. *