lib: Implement cookie handling in Response

Cookies set via Response::setCookie() or Response::getCookies()::add() will be automatically sent to client.
This commit is contained in:
Eric Lippmann 2015-11-27 15:51:38 +01:00
parent 2a0d3412d1
commit 59b540cc12
1 changed files with 65 additions and 0 deletions

View File

@ -12,6 +12,13 @@ use Icinga\Web\Response\JsonResponse;
*/
class Response extends Zend_Controller_Response_Http
{
/**
* Set of cookies which are to be sent to the client
*
* @var CookieSet
*/
protected $cookies;
/**
* Redirect URL
*
@ -40,6 +47,44 @@ class Response extends Zend_Controller_Response_Http
*/
protected $rerenderLayout = false;
/**
* Get the set of cookies which are to be sent to the client
*
* @return CookieSet
*/
public function getCookies()
{
if ($this->cookies === null) {
$this->cookies = new CookieSet();
}
return $this->cookies;
}
/**
* Get the cookie with the given name from the set of cookies which are to be sent to the client
*
* @param string $name The name of the cookie
*
* @return Cookie|null The cookie with the given name or null if the cookie does not exist
*/
public function getCookie($name)
{
return $this->getCookies()->get($name);
}
/**
* Set the given cookie for sending it to the client
*
* @param Cookie $cookie The cookie to send to the client
*
* @return $this
*/
public function setCookie(Cookie $cookie)
{
$this->getCookies()->add($cookie);
return $this;
}
/**
* Get the redirect URL
*
@ -184,12 +229,32 @@ class Response extends Zend_Controller_Response_Http
exit;
}
/**
* Send the cookies to the client
*/
public function sendCookies()
{
foreach ($this->getCookies() as $cookie) {
/** @var Cookie $cookie */
setcookie(
$cookie->getName(),
$cookie->getValue(),
$cookie->getExpire(),
$cookie->getPath(),
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttpOnly()
);
}
}
/**
* {@inheritdoc}
*/
public function sendHeaders()
{
$this->prepare();
$this->sendCookies();
return parent::sendHeaders();
}
}