Skip to content

Commit

Permalink
Add cookie support to context
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Nov 29, 2019
1 parent dd899a5 commit 2b11d89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +34,7 @@ class Context
private $_id;
private $_events;
private $_request;
private $_cookieJar;

public final function __construct(Request $request = null)
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down

0 comments on commit 2b11d89

Please sign in to comment.