From 2b11d8956fb17e765463323ea088a99fac6c42c5 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Fri, 29 Nov 2019 10:10:47 +0000 Subject: [PATCH] Add cookie support to context --- src/Context.php | 22 ++++++++++++++++++++++ tests/ContextTest.php | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/Context.php b/src/Context.php index 5c06a31..d798be8 100644 --- a/src/Context.php +++ b/src/Context.php @@ -5,6 +5,7 @@ use Packaged\Config\Provider\ConfigProvider; use Packaged\Event\Channel\Channel; use Packaged\Helpers\System; +use Packaged\Http\Cookies\CookieJar; use Packaged\Http\Request; use Symfony\Component\HttpFoundation\ParameterBag; use function dirname; @@ -33,6 +34,7 @@ class Context private $_id; private $_events; private $_request; + private $_cookieJar; public final function __construct(Request $request = null) { @@ -197,6 +199,26 @@ public function events(): Channel return $this->_events; } + /** + * Cookies for the request + * + * @return CookieJar + */ + public function cookies(): CookieJar + { + if($this->_cookieJar === null) + { + $this->_cookieJar = new CookieJar(); + $this->_cookieJar->hydrate($this->request()); + } + return $this->_cookieJar; + } + + /** + * Retrieve the CONTEXT_ENV from the environment + * + * @return array|false|string + */ public function getSystemEnvironment() { //Calculate the environment diff --git a/tests/ContextTest.php b/tests/ContextTest.php index 953d13f..cb1c406 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -6,6 +6,7 @@ use Packaged\Context\Context; use Packaged\Event\Channel\Channel; use Packaged\Helpers\Arrays; +use Packaged\Http\Cookies\CookieJar; use Packaged\Http\Request; use Packaged\Tests\Context\Supporting\TestContextAwareObject; use PHPUnit\Framework\TestCase; @@ -23,6 +24,7 @@ public function testDefaults() $this->assertInstanceOf(ConfigProvider::class, $ctx->getConfig()); $this->assertInstanceOf(Request::class, $ctx->request()); $this->assertInstanceOf(Channel::class, $ctx->events()); + $this->assertInstanceOf(CookieJar::class, $ctx->cookies()); $this->assertEquals(Context::ENV_LOCAL, $ctx->getEnvironment()); $this->assertTrue($ctx->isCli()); $this->assertStringStartsWith('ctx-', $ctx->id());