/** * {@inheritdoc} */ public function set(ServerRequestInterface $request, ResponseInterface $response, $name, $value, array $settings = []) { // --------------------------------------------------- // Request cookies // --------------------------------------------------- $cookies = Cookies::fromRequest($request); if ($cookies->has($name)) { $cookie = $cookies->get($name)->withValue($value); } else { $cookie = Cookie::create($name, $value); } $request = $cookies->with($cookie)->renderIntoCookieHeader($request); // --------------------------------------------------- // Response cookies // --------------------------------------------------- $domain = isset($settings['domain']) ? (string) $settings['domain'] : ''; $path = isset($settings['path']) ? (string) $settings['path'] : '/'; $ttl = isset($settings['ttl']) ? $settings['ttl'] : 0; $expires = isset($settings['expires']) ? $settings['expires'] : time() + $ttl; $secure = isset($settings['secure']) && $settings['secure']; $http_only = isset($settings['http_only']) && $settings['http_only']; $set_cookies = SetCookies::fromResponse($response); if ($set_cookies->has($name)) { $set_cookie = $set_cookies->get($name)->withValue($value); } else { $set_cookie = SetCookie::create($name, $value); } $set_cookie = $set_cookie->withDomain($domain)->withPath($path)->withSecure($secure)->withExpires(date(DATE_COOKIE, $expires))->withHttpOnly($http_only); $response = $set_cookies->with($set_cookie)->renderIntoSetCookieHeader($response); return [$request, $response]; }
protected function main() { $this->attachToRequest(); $this->requestCookies = Cookies::fromRequest($this->request); $this->responseCookies = new SetCookies(); $this->response = $this->next(); $cookies = SetCookies::fromResponse($this->response); foreach ($this->responseCookies->getAll() as $setCookie) { $cookies = $cookies->with($setCookie); } return $cookies->renderIntoSetCookieHeader($this->response); }
public function persist(Session $session, ResponseInterface $response = null, $overwriteExistingCookie = false) { if (is_null($response)) { throw new InvalidArgumentException('You must pass an instance of ResponseInterface.'); } $setCookies = SetCookies::fromResponse($response); //Cookie already set in response by a preceding middleware if (!$overwriteExistingCookie && $setCookies->has($this->config['cookie_name'])) { return $response; } $setCookie = SetCookie::create($this->config['cookie_name'])->withPath($this->config['cookie_path'])->withDomain($this->config['cookie_domain'])->withSecure($this->config['cookie_secure'])->withHttpOnly($this->config['cookie_httponly']); if ($session->wasDestroyed()) { $setCookie = $setCookie->withValue('')->withExpires(1); } else { $setCookie = $setCookie->withValue($session->getSessionId())->withExpires($this->config['cookie_lifetime'] == 0 ? 0 : time() + $this->config['cookie_lifetime']); } return $setCookies->with($setCookie)->renderIntoSetCookieHeader($response); }
/** * @param ResponseInterface $response * @param string $name * * @return ResponseInterface */ public static function remove(ResponseInterface $response, $name) { return SetCookies::fromResponse($response)->without($name)->renderIntoSetCookieHeader($response); }