Skip to content

Commit 6ba089b

Browse files
authored
Merge pull request #45 from franzliedke/fl/23-deprecate-pointless-expire-methods
Deprecate pointless cookie expiry facades
2 parents f06d771 + 168d37b commit 6ba089b

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ $request = FigRequestCookies::modify($request, 'theme', $modify);
150150

151151
#### Remove a Request Cookie
152152

153-
The `remove` method removes a cookie if it exists.
153+
The `remove` method removes a cookie from the request headers if it exists.
154154

155155
```php
156156
use Dflydev\FigCookies\FigRequestCookies;
@@ -159,7 +159,7 @@ $request = FigRequestCookies::remove($request, 'theme');
159159
```
160160

161161
Note that this does not cause the client to remove the cookie. Take a look at
162-
`FigResponseCookies::expire` to do that.
162+
the `SetCookie` class' `expire()` method to do that.
163163

164164
### Response Cookies
165165

@@ -261,10 +261,22 @@ $response = FigResponseCookies::remove($response, 'theme');
261261
The `expire` method sets a cookie with an expiry date in the far past. This
262262
causes the client to remove the cookie.
263263

264+
Note that in order to expire a cookie, you need to configure its `Set-Cookie`
265+
header just like when you initially wrote the cookie (i.e. same domain/path).
266+
The easiest way to do this is to re-use the same code for configuring the
267+
header when setting as well as expiring the cookie:
268+
264269
```php
265270
use Dflydev\FigCookies\FigResponseCookies;
271+
use Dflydev\FigCookies\SetCookie;
272+
273+
$setCookie = SetCookie::create('ba')
274+
->withValue('UQdfdafpJJ23k111m')
275+
->withPath('/')
276+
->withDomain('.example.com')
277+
;
266278

267-
$response = FigResponseCookies::expire($response, 'session_cookie');
279+
FigResponseCookies::set($response, $setCookie->expire());
268280
```
269281

270282

src/Dflydev/FigCookies/FigResponseCookies.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public static function set(ResponseInterface $response, SetCookie $setCookie): R
3030
->renderIntoSetCookieHeader($response);
3131
}
3232

33+
/**
34+
* @deprecated Do not use this method. Will be removed in v4.0.
35+
*
36+
* If you want to remove a cookie, create it normally and call ->expire()
37+
* on the SetCookie object.
38+
*/
3339
public static function expire(ResponseInterface $response, string $cookieName): ResponseInterface
3440
{
3541
return static::set($response, SetCookie::createExpired($cookieName));

src/Dflydev/FigCookies/SetCookie.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ public static function createRememberedForever(string $name, ?string $value = nu
239239
return static::create($name, $value)->rememberForever();
240240
}
241241

242+
/**
243+
* @deprecated Do not use this method. Will be removed in v4.0.
244+
*
245+
* If you want to remove a cookie, create it normally and call ->expire()
246+
* on the SetCookie object.
247+
*/
242248
public static function createExpired(string $name): self
243249
{
244250
return static::create($name)->expire();

tests/Dflydev/FigCookies/SetCookieTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public function provideParsesFromSetCookieStringData(): array
150150
*/
151151
public function it_expires_cookies(): void
152152
{
153-
$setCookie = SetCookie::createExpired('expire_immediately');
153+
$setCookie = SetCookie::create('HSID')
154+
->withValue('AYQEVn/.DKrdst')
155+
->withDomain('.foo.com')
156+
->withPath('/')
157+
->withHttpOnly(true)
158+
->expire();
154159

155160
self::assertLessThan(time(), $setCookie->getExpires());
156161
}

0 commit comments

Comments
 (0)