-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from PhpGt/8-session-store
Implement session store
- Loading branch information
Showing
10 changed files
with
444 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Gt\\Session\\Test\\": "/test/unit" | ||
"Gt\\Session\\Test\\": "./test/unit" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.