Skip to content

Commit

Permalink
Merge pull request #13 from PhpGt/6-dot-notation
Browse files Browse the repository at this point in the history
Dot notation
  • Loading branch information
g105b authored Jul 26, 2018
2 parents 4e39e55 + 0fc6a65 commit 3c9462a
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 334 deletions.
2 changes: 1 addition & 1 deletion composer.lock

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

35 changes: 27 additions & 8 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use SessionHandlerInterface;

class Session {
use StoreContainer;

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

const DEFAULT_STORE = "_";

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

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

$this->sessionHandler->open($sessionPath, $sessionName);
$this->stores = $this->readSessionData();
$this->store = $this->readSessionData() ?: null;
if(is_null($this->store)) {
$this->store = new SessionStore(__NAMESPACE__, $this);
}
}

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

public function getStore(string $namespace) {
return $this->store->getStore($namespace);
}

public function get(string $key) {
return $this->store->get($key);
}

public function set(string $key, $value):void {
$this->store->set($key, $value);
}

public function contains(string $key):bool {
return $this->store->contains($key);
}

public function remove(string $key):void {
$this->store->remove($key);
}

public function getId():string {
$id = session_id();
if(empty($id)) {
Expand Down Expand Up @@ -106,7 +125,7 @@ protected function readSessionData() {
public function write() {
$this->sessionHandler->write(
$this->id,
serialize($this->stores)
serialize($this->store)
);
}
}
149 changes: 141 additions & 8 deletions src/SessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
namespace Gt\Session;

class SessionStore {
use StoreContainer;

/** @var string */
protected $name;
/** @var Session */
Expand All @@ -18,12 +16,6 @@ public function __construct(string $name, Session $session) {
$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;
}
Expand Down Expand Up @@ -60,4 +52,145 @@ public function removeDataOrStore(string $key):void {
public function write():void {
$this->session->write();
}

public function getStore(string $namespace):?SessionStore {
$namespaceParts = explode(".", $namespace);
$topLevelStoreName = array_shift($namespaceParts);

/** @var SessionStore $store */
$store = $this->stores[$topLevelStoreName] ?? null;
if (is_null($store)) {
return null;
}

if (empty($namespaceParts)) {
return $store;
}

$namespace = implode(".", $namespaceParts);
return $store->getStore($namespace);
}

public function setStore(
string $namespace,
SessionStore $newStore = null
):void {
$namespaceParts = explode(".", $namespace);
$store = $this;
$nextStore = $store;

while (!empty($namespaceParts)) {
$storeName = array_shift($namespaceParts);
$store = $nextStore;
$nextStore = $store->getStore($storeName);

if (is_null($nextStore)) {
$nextStore = new SessionStore(
$storeName,
$this->session
);
$store->stores[$storeName] = $nextStore;
}
}
}

public function createStore(string $namespace):SessionStore {
$this->setStore($namespace);
return $this->getStore($namespace);
}

public function get(string $key) {
$store = $this;
$lastDotPosition = strrpos($key, ".");

if ($lastDotPosition !== false) {
$namespace = $this->getNamespaceFromKey($key);
$store = $this->getStore($namespace);
}

if (is_null($store)) {
return null;
}

if ($lastDotPosition !== false) {
$key = substr($key, $lastDotPosition + 1);
}

return $store->getData($key);
}

public function set(string $key, $value):void {
$store = $this;
$lastDotPosition = strrpos($key, ".");

if ($lastDotPosition !== false) {
$namespace = $this->getNamespaceFromKey($key);
$store = $this->getStore($namespace);

if (is_null($store)) {
$store = $this->createStore($namespace);
}
}

if ($lastDotPosition !== false) {
$key = substr($key, $lastDotPosition + 1);
}

$store->setData($key, $value);
$store->write();
}

public function contains(string $key):bool {
$store = $this;
$lastDotPosition = strrpos($key, ".");

if ($lastDotPosition !== false) {
$namespace = $this->getNamespaceFromKey($key);
$store = $this->getStore($namespace);
}

if (is_null($store)) {
return false;
}

if ($lastDotPosition !== false) {
$key = substr($key, $lastDotPosition + 1);
}

return $store->containsData($key);
}

public function remove(string $key):void {
$store = $this;
$lastDotPosition = strrpos($key, ".");

if ($lastDotPosition !== false) {
$namespace = $this->getNamespaceFromKey($key);
$store = $this->getStore($namespace);
}

if (is_null($store)) {
return;
}

if ($lastDotPosition !== false) {
$key = substr($key, $lastDotPosition + 1);
}

$store->removeDataOrStore($key);
$store->write();
}

protected function getSession():Session {
return $this->session;
}

protected function getNamespaceFromKey(string $key):?string {
$lastDotPostition = strrpos($key, ".");
if ($lastDotPostition === false) {
return null;
}

return substr($key, 0, $lastDotPostition);
}
}
141 changes: 0 additions & 141 deletions src/StoreContainer.php

This file was deleted.

Loading

0 comments on commit 3c9462a

Please sign in to comment.