Skip to content

Commit

Permalink
The missing simple session manager
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Jun 28, 2017
1 parent 54e37b1 commit ab3bfb2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,39 @@ public function cookie($key = '', $value = '', $options = null)
return null;
}

/**
* Set or Get session
*
* @param mixed $key Index for item for session
* @param string $value session value
* @return mixed
*/
public function session($key = '', $value = '')
{
if (is_null($key)) {
if (empty($_SESSION)) {
return null;
}

unset($_SESSION);
} elseif ('' === $key) {
// Get All Session
return $_SESSION;
}

if ('' === $value) {
return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
} else {
if (is_null($value)) {
unset($_SESSION[$key]);
} else {
$_SESSION[$key] = $value;
}
}

return null;
}

/**
* Is HTTPS?
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ public function testDeleteCookie()
$this->assertNull($request->cookie('name'));
}

public function testSetAndGetSession()
{
$request = new Request();
// @codingStandardsIgnoreStart
@$request->session('name', 'honoka');
// @codingStandardsIgnoreEnd
$this->assertEquals('honoka', $request->session('name'));
}

public function testDeleteSession()
{
$request = new Request();
// @codingStandardsIgnoreStart
@$request->session('name', 'honoka');
@$request->session('name', null);
// @codingStandardsIgnoreEnd
$this->assertNull($request->session('name'));
}

public function testIsSecure()
{
$request = new Request();
Expand Down

0 comments on commit ab3bfb2

Please sign in to comment.