Skip to content

Commit

Permalink
Merge pull request #9 from PhpGt/8-session-store
Browse files Browse the repository at this point in the history
Implement session store
  • Loading branch information
g105b authored Jul 22, 2018
2 parents 4b7a4ba + ab78518 commit 4e39e55
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 342 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"autoload-dev": {
"psr-4": {
"Gt\\Session\\Test\\": "/test/unit"
"Gt\\Session\\Test\\": "./test/unit"
}
}
}
39 changes: 21 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 11 additions & 59 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use ArrayAccess;
use SessionHandlerInterface;

class Session implements ArrayAccess {
class Session {
use StoreContainer;

const DEFAULT_SESSION_NAME = "PHPSESSID";
const DEFAULT_SESSION_LIFETIME = 0;
const DEFAULT_SESSION_PATH = "/tmp";
Expand All @@ -13,10 +15,14 @@ class Session implements ArrayAccess {
const DEFAULT_SESSION_HTTPONLY = true;
const DEFAULT_COOKIE_PATH = "/";

protected $data;
const DEFAULT_STORE = "_";

/** @var string */
protected $id;
/** @var SessionHandlerInterface */
protected $sessionHandler;
/** @var SessionStore[] */
protected $stores;

public function __construct(
SessionHandlerInterface $sessionHandler,
Expand Down Expand Up @@ -46,28 +52,7 @@ public function __construct(
]);

$this->sessionHandler->open($sessionPath, $sessionName);
$this->data = $this->readSessionData();
}

public function get(string $key) {
return $this->data[$key] ?? null;
}

public function set(string $key, $value):void {
$this->data[$key] = $value;
$this->writeSessionData();
}

public function has(string $key):bool {
return isset($this->data[$key]);
}

public function delete($key):void {
if($this->has($key)) {
unset($this->data[$key]);
}

$this->writeSessionData();
$this->stores = $this->readSessionData();
}

public function kill():void {
Expand All @@ -84,39 +69,6 @@ public function kill():void {
);
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param string $offset
*/
public function offsetExists($offset):bool {
return $this->has($offset);
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param string $offset <p>
*/
public function offsetGet($offset) {
return $this->get($offset);
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param string $offset
* @param mixed $value
*/
public function offsetSet($offset, $value):void {
$this->set($offset, $value);
}

/**
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param string $offset
*/
public function offsetUnset($offset):void {
$this->delete($offset);
}

public function getId():string {
$id = session_id();
if(empty($id)) {
Expand Down Expand Up @@ -151,10 +103,10 @@ protected function readSessionData() {
return unserialize($this->sessionHandler->read($this->id));
}

protected function writeSessionData() {
public function write() {
$this->sessionHandler->write(
$this->id,
serialize($this->data)
serialize($this->stores)
);
}
}
63 changes: 63 additions & 0 deletions src/SessionStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Gt\Session;

class SessionStore {
use StoreContainer;

/** @var string */
protected $name;
/** @var Session */
protected $session;
/** @var SessionStore[] */
protected $stores;
/** @var string[] */
protected $data;

public function __construct(string $name, Session $session) {
$this->name = $name;
$this->session = $session;
}

public function createStore(string $name, Session $session):self {
$newStore = new self($name, $session);
$this->stores[$name] = $newStore;
return $newStore;
}

public function setData(string $key, $value):void {
$this->data[$key] = $value;
}

public function getData(string $key) {
return $this->data[$key] ?? null;
}

public function containsData(string $key):bool {
return isset($this->data[$key]);
}

public function containsStore(string $key):bool {
return isset($this->stores[$key]);
}

public function removeData(string $key):void {
unset($this->data[$key]);
}

public function removeStore(string $key):void {
unset($this->stores[$key]);
}

public function removeDataOrStore(string $key):void {
if($this->containsData($key)) {
$this->removeData($key);
}
if($this->containsStore($key)) {
$this->removeStore($key);
}
}

public function write():void {
$this->session->write();
}
}
16 changes: 16 additions & 0 deletions src/SessionStoreFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Gt\Session;

class SessionStoreFactory {
public static function create(string $namespace, Session $session):SessionStore {
$namespaceParts = explode(".", $namespace);
$store = new SessionStore(array_shift($namespaceParts), $session);

foreach($namespaceParts as $part) {
$innerStore = $store->createStore($part, $session);
$store = $innerStore;
}

return $store;
}
}
Loading

0 comments on commit 4e39e55

Please sign in to comment.