-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I implemented a straightforward cookie collection mechanism #66
Changes from all commits
8f3fbee
b1bebe6
2bef751
0a92769
3c47fb1
2b670f6
9a9ccc6
f4205c0
8eb1f9b
adb94ea
71b9b6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -146,6 +146,46 @@ $cookie = (new \Yiisoft\Cookies\Cookie('cookieName')) | |||
->withRawValue('ebaKUq90PhiHck_MR7st-E1SxhbYWiTsLo82mCTbNuAh7rgflx5LVsYfJJseyQCrODuVcJkTSYhm1WKte-l5lQ==') | ||||
``` | ||||
|
||||
Work with cookie request collection from any place in you project. Add in config you app in middleware block | ||||
```php | ||||
'middlewares' => [ | ||||
\Yiisoft\Cookies\RequestCookies\RequestCookiesCollectorMiddleware::class, | ||||
... | ||||
], | ||||
``` | ||||
|
||||
Initial provider in di-web config | ||||
```php | ||||
return [ | ||||
..., | ||||
\Yiisoft\Cookies\RequestCookies\RequestCookiesProviderInterface::class => [ | ||||
'class' => Yiisoft\Cookies\RequestCookies\RequestCookiesProvider::class, | ||||
], | ||||
|
||||
``` | ||||
|
||||
Use as dependency in any part of ur code | ||||
|
||||
```php | ||||
|
||||
use Yiisoft\Cookies\RequestCookies\RequestCookiesProviderInterface; | ||||
|
||||
final class MyService | ||||
{ | ||||
public function __construct( | ||||
private RequestCookiesProviderInterface $requestCookieProvider | ||||
) | ||||
{ | ||||
} | ||||
|
||||
public function doIt() | ||||
{ | ||||
$cookieCollection = $this->requestCookieProvider->get(); | ||||
// ... | ||||
} | ||||
} | ||||
``` | ||||
``` | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
## Documentation | ||||
|
||||
- [Internals](docs/internals.md) | ||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||||||
<?php | ||||||||||
|
||||||||||
declare(strict_types=1); | ||||||||||
|
||||||||||
/** @var array $params */ | ||||||||||
|
||||||||||
use Yiisoft\Cookies\RequestCookies\RequestCookiesProvider; | ||||||||||
use Yiisoft\Cookies\RequestCookies\RequestCookiesProviderInterface; | ||||||||||
|
||||||||||
return [ | ||||||||||
RequestCookiesProviderInterface::class => [ | ||||||||||
'class' => RequestCookiesProvider::class, | ||||||||||
], | ||||||||||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
]; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Yiisoft\Cookies\RequestCookies\Exception; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
use LogicException; | ||||||
use Throwable; | ||||||
|
||||||
/** | ||||||
* Thrown when Request cookie collect isn't set before. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
final class RequestCookieCollectionNotSetException extends LogicException | ||||||
{ | ||||||
public function __construct(int $code = 0, ?Throwable $previous = null) | ||||||
{ | ||||||
parent::__construct('Request cookie collect is not set.', $code, $previous); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Yiisoft\Cookies\RequestCookies; | ||||||
|
||||||
/** | ||||||
* A RequestCookies helps to work with many cookies at once and to read request cookies. | ||||||
*/ | ||||||
final class RequestCookies | ||||||
{ | ||||||
/** | ||||||
* @var array The cookies in this collection (indexed by the cookie name). | ||||||
* | ||||||
* @psalm-var array<string, string> | ||||||
*/ | ||||||
private array $cookies = []; | ||||||
|
||||||
/** | ||||||
* RequestCookies constructor. | ||||||
* | ||||||
* @param array<string, string> $cookies The cookies that this collection initially contains. | ||||||
*/ | ||||||
public function __construct(array $cookies = []) | ||||||
{ | ||||||
foreach ($cookies as $name => $value) { | ||||||
$this->cookies[$name] = $value; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns the cookie with the specified name. | ||||||
* | ||||||
* @param string $name The cookie name. | ||||||
* | ||||||
* @return ?string The cookie with the specified name. Null if the named cookie does not exist. | ||||||
* | ||||||
* @see getValue() | ||||||
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public function get(string $name): ?string | ||||||
{ | ||||||
return $this->cookies[$name] ?? null; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns whether there is a cookie with the specified name. | ||||||
* | ||||||
* @param string $name The cookie name. | ||||||
* | ||||||
* @return bool Whether the named cookie exists. | ||||||
* | ||||||
* @see remove() | ||||||
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public function has(string $name): bool | ||||||
{ | ||||||
return isset($this->cookies[$name]); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||||
<?php | ||||||||
|
||||||||
declare(strict_types=1); | ||||||||
|
||||||||
namespace Yiisoft\Cookies\RequestCookies; | ||||||||
|
||||||||
use Psr\Http\Message\ResponseInterface; | ||||||||
use Psr\Http\Message\ServerRequestInterface; | ||||||||
use Psr\Http\Server\MiddlewareInterface; | ||||||||
use Psr\Http\Server\RequestHandlerInterface; | ||||||||
|
||||||||
/** | ||||||||
* Stores cookies request {@see RequestCookiesProviderInterface}. | ||||||||
* You need to add this into your application middleware stack. | ||||||||
*/ | ||||||||
final class RequestCookiesCollectorMiddleware implements MiddlewareInterface | ||||||||
{ | ||||||||
private RequestCookiesProviderInterface $cookieProvider; | ||||||||
|
||||||||
public function __construct(RequestCookiesProviderInterface $cookieProvider) | ||||||||
{ | ||||||||
$this->cookieProvider = $cookieProvider; | ||||||||
} | ||||||||
|
||||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||||||||
{ | ||||||||
$cookies = $this->collectCookies($request); | ||||||||
$this->cookieProvider->set(new RequestCookies($cookies)); | ||||||||
Comment on lines
+27
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems, |
||||||||
return $handler->handle($request); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* @param ServerRequestInterface $request | ||||||||
* @return array<string, string> | ||||||||
*/ | ||||||||
private function collectCookies(ServerRequestInterface $request): array | ||||||||
{ | ||||||||
$collection = []; | ||||||||
foreach ($request->getCookieParams() as $name => $value) { | ||||||||
if (!is_string($name) || !is_string($value)) { | ||||||||
continue; | ||||||||
} | ||||||||
$collection[$name] = $value; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can I access expires, domain and other parameters later? |
||||||||
} | ||||||||
return $collection; | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Yiisoft\Cookies\RequestCookies; | ||||||
|
||||||
use Yiisoft\Cookies\RequestCookies\Exception\RequestCookieCollectionNotSetException; | ||||||
|
||||||
/** | ||||||
* Stores request for further consumption by attribute handlers. | ||||||
*/ | ||||||
final class RequestCookiesProvider implements RequestCookiesProviderInterface | ||||||
{ | ||||||
/** | ||||||
* @var RequestCookies|null The collection. | ||||||
*/ | ||||||
private ?RequestCookies $cookieCollection = null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
public function set(RequestCookies $cookieCollection): void | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have an empty collection in this class and merge two collections instead. |
||||||
{ | ||||||
$this->cookieCollection = $cookieCollection; | ||||||
} | ||||||
|
||||||
public function get(): RequestCookies | ||||||
{ | ||||||
if ($this->cookieCollection === null) { | ||||||
throw new RequestCookieCollectionNotSetException(); | ||||||
} | ||||||
|
||||||
return $this->cookieCollection; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Yiisoft\Cookies\RequestCookies; | ||||||
|
||||||
use Yiisoft\Cookies\RequestCookies\Exception\RequestCookieCollectionNotSetException; | ||||||
|
||||||
/** | ||||||
* Provides a way to set the current cookie collection and then get it when needed. | ||||||
*/ | ||||||
interface RequestCookiesProviderInterface | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CookiesProvider sounds better than RequestCookiesProvider |
||||||
{ | ||||||
/** | ||||||
* Set the current cookie request collection. | ||||||
* | ||||||
* @param RequestCookies $cookieCollection The collection to set. | ||||||
*/ | ||||||
public function set(RequestCookies $cookieCollection): void; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
/** | ||||||
* Get the current request cookie collection. | ||||||
* | ||||||
* @throws RequestCookieCollectionNotSetException | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd return an empty collection instead |
||||||
*/ | ||||||
public function get(): RequestCookies; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Cookies\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Cookies\RequestCookies\RequestCookiesProvider; | ||
use Yiisoft\Cookies\RequestCookies\RequestCookiesProviderInterface; | ||
use Yiisoft\Di\Container; | ||
use Yiisoft\Di\ContainerConfig; | ||
|
||
use function dirname; | ||
|
||
final class ConfigTest extends TestCase | ||
{ | ||
public function testDi(): void | ||
{ | ||
$container = $this->createContainer(); | ||
|
||
$this->assertInstanceOf( | ||
RequestCookiesProvider::class, | ||
$container->get(RequestCookiesProviderInterface::class) | ||
); | ||
} | ||
|
||
private function createContainer(): Container | ||
{ | ||
return new Container( | ||
ContainerConfig::create()->withDefinitions($this->getContainerDefinitions()) | ||
); | ||
} | ||
|
||
private function getContainerDefinitions(): array | ||
{ | ||
return require dirname(__DIR__) . '/config/di-web.php'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.