Skip to content

Commit

Permalink
Moved managing cookies of HeaderBag in ResponseHeaderBag
Browse files Browse the repository at this point in the history
By example, a cookie can't be set in a request
  • Loading branch information
francisbesset committed Jul 11, 2011
1 parent e8ea852 commit f08eeb4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 63 deletions.
63 changes: 0 additions & 63 deletions src/Symfony/Component/HttpFoundation/HeaderBag.php
Expand Up @@ -19,7 +19,6 @@
class HeaderBag
{
protected $headers;
protected $cookies;
protected $cacheControl;

/**
Expand All @@ -30,7 +29,6 @@ class HeaderBag
public function __construct(array $headers = array())
{
$this->cacheControl = array();
$this->cookies = array();
$this->headers = array();
foreach ($headers as $key => $values) {
$this->set($key, $values);
Expand Down Expand Up @@ -200,67 +198,6 @@ public function remove($key)
}
}

/**
* Sets a cookie.
*
* @param Cookie $cookie
* @return void
*/
public function setCookie(Cookie $cookie)
{
$this->cookies[$cookie->getName()] = $cookie;
}

/**
* Removes a cookie from the array, but does not unset it in the browser
*
* @param string $name
* @return void
*/
public function removeCookie($name)
{
unset($this->cookies[$name]);
}

/**
* Whether the array contains any cookie with this name
*
* @param string $name
* @return Boolean
*/
public function hasCookie($name)
{
return isset($this->cookies[$name]);
}

/**
* Returns a cookie
*
* @param string $name
*
* @throws \InvalidArgumentException When the cookie does not exist
*
* @return Cookie
*/
public function getCookie($name)
{
if (!$this->hasCookie($name)) {
throw new \InvalidArgumentException(sprintf('There is no cookie with name "%s".', $name));
}

return $this->cookies[$name];
}

/**
* Returns an array with all cookies
*
* @return array
*/
public function getCookies()
{
return $this->cookies;
}

/**
* Returns the HTTP header value converted to a date.
*
Expand Down
62 changes: 62 additions & 0 deletions src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php
Expand Up @@ -19,6 +19,7 @@
class ResponseHeaderBag extends HeaderBag
{
protected $computedCacheControl = array();
protected $cookies = array();

/**
* Constructor.
Expand Down Expand Up @@ -102,6 +103,67 @@ public function getCacheControlDirective($key)
return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
}

/**
* Sets a cookie.
*
* @param Cookie $cookie
* @return void
*/
public function setCookie(Cookie $cookie)
{
$this->cookies[$cookie->getName()] = $cookie;
}

/**
* Removes a cookie from the array, but does not unset it in the browser
*
* @param string $name
* @return void
*/
public function removeCookie($name)
{
unset($this->cookies[$name]);
}

/**
* Whether the array contains any cookie with this name
*
* @param string $name
* @return Boolean
*/
public function hasCookie($name)
{
return isset($this->cookies[$name]);
}

/**
* Returns a cookie
*
* @param string $name
*
* @throws \InvalidArgumentException When the cookie does not exist
*
* @return Cookie
*/
public function getCookie($name)
{
if (!$this->hasCookie($name)) {
throw new \InvalidArgumentException(sprintf('There is no cookie with name "%s".', $name));
}

return $this->cookies[$name];
}

/**
* Returns an array with all cookies
*
* @return array
*/
public function getCookies()
{
return $this->cookies;
}

/**
* Clears a cookie in the browser
*
Expand Down

0 comments on commit f08eeb4

Please sign in to comment.