added cookie jar that holds cookies until end of request.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
Reference in New Issue
Block a user