diff options
author | Lukas Reschke <lukas@owncloud.com> | 2014-11-27 14:19:00 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2014-11-27 14:19:00 +0100 |
commit | 048139074df674d0ac82da12357d3934af3abc2e (patch) | |
tree | 04e66fdbdc9fbb255036e36fe29b2274b3661af6 /lib/public/appframework/http | |
parent | e306b588d29d71bf547b19d1725ea9ad1a5fbf42 (diff) | |
download | nextcloud-server-048139074df674d0ac82da12357d3934af3abc2e.tar.gz nextcloud-server-048139074df674d0ac82da12357d3934af3abc2e.zip |
Add functions to modify cookies to response class
Currently there is no AppFramework way to modify cookies, which makes it unusable for quite some use-cases or results in untestable code.
This PR adds some basic functionalities to add and invalidate cookies.
Usage:
```php
$response = new TemplateResponse(...);
$response->addCookie('foo', 'bar');
$response->invalidateCookie('foo');
$response->addCookie('bar', 'foo', new \DateTime('2015-01-01 00:00'));
```
Existing cookies can be accessed with the AppFramework using `$this->request->getCookie($name)`.
Diffstat (limited to 'lib/public/appframework/http')
-rw-r--r-- | lib/public/appframework/http/response.php | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/lib/public/appframework/http/response.php b/lib/public/appframework/http/response.php index 354911fee21..67e72cff6d9 100644 --- a/lib/public/appframework/http/response.php +++ b/lib/public/appframework/http/response.php @@ -46,8 +46,15 @@ class Response { /** + * Cookies that will be need to be constructed as header + * @var array + */ + private $cookies = array(); + + + /** * HTTP status code - defaults to STATUS OK - * @var string + * @var int */ private $status = Http::STATUS_OK; @@ -70,6 +77,7 @@ class Response { * Caches the response * @param int $cacheSeconds the amount of seconds that should be cached * if 0 then caching will be disabled + * @return $this */ public function cacheFor($cacheSeconds) { @@ -83,13 +91,68 @@ class Response { return $this; } + /** + * Adds a new cookie to the response + * @param string $name The name of the cookie + * @param string $value The value of the cookie + * @param \DateTime|null $expireDate Date on that the cookie should expire, if set + * to null cookie will be considered as session + * cookie. + * @return $this + */ + public function addCookie($name, $value, \DateTime $expireDate = null) { + $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate); + return $this; + } + + + /** + * Set the specified cookies + * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) + * @return $this + */ + public function setCookies(array $cookies) { + $this->cookies = $cookies; + return $this; + } + + + /** + * Invalidates the specified cookie + * @param string $name + * @return $this + */ + public function invalidateCookie($name) { + $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); + return $this; + } + + /** + * Invalidates the specified cookies + * @param array $cookieNames array('foo', 'bar') + * @return $this + */ + public function invalidateCookies(array $cookieNames) { + foreach($cookieNames as $cookieName) { + $this->invalidateCookie($cookieName); + } + return $this; + } + + /** + * Returns the cookies + * @return array + */ + public function getCookies() { + return $this->cookies; + } /** * Adds a new header to the response that will be called before the render * function * @param string $name The name of the HTTP header * @param string $value The value, null will delete it - * @return Response Reference to this object + * @return $this */ public function addHeader($name, $value) { $name = trim($name); // always remove leading and trailing whitespace @@ -108,10 +171,10 @@ class Response { /** * Set the headers - * @param array key value header pairs - * @return Response Reference to this object + * @param array $headers value header pairs + * @return $this */ - public function setHeaders($headers) { + public function setHeaders(array $headers) { $this->headers = $headers; return $this; |