forked from hhamon/lpdim2016
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Framework] added a new Session component.
- Loading branch information
Hugo Hamon
committed
Jan 18, 2016
1 parent
af0824e
commit 3a130df
Showing
10 changed files
with
582 additions
and
0 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 |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
/coverage | ||
!/app/cache/.gitkeep | ||
/app/cache | ||
!/app/sessions/.gitkeep | ||
/app/sessions | ||
/app/config/settings.yml |
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
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,50 @@ | ||
<?php | ||
|
||
namespace Framework\Session\Driver; | ||
|
||
/** | ||
* This class is only used for unit tests. | ||
* | ||
* @internal | ||
*/ | ||
class ArrayDriver implements DriverInterface | ||
{ | ||
private $data; | ||
|
||
public function __construct() | ||
{ | ||
$this->data = []; | ||
} | ||
|
||
public function clear($id) | ||
{ | ||
if (isset($this->data[$id])) { | ||
unset($this->data[$id]); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function store($id, $key, $value) | ||
{ | ||
$this->data[$id][$key] = $value; | ||
|
||
return true; | ||
} | ||
|
||
public function fetch($id, $key, $default = null) | ||
{ | ||
if (isset($this->data[$id][$key])) { | ||
return $this->data[$id][$key]; | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
public function save($id) | ||
{ | ||
return true; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Framework\Session\Driver; | ||
|
||
class DriverException extends \RuntimeException | ||
{ | ||
|
||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Framework\Session\Driver; | ||
|
||
interface DriverInterface | ||
{ | ||
/** | ||
* Clear session data. | ||
* | ||
* @param string $id The unique session ID | ||
* | ||
* @return bool True if successful, false otherwise | ||
* | ||
* @throws DriverException | ||
*/ | ||
public function clear($id); | ||
|
||
/** | ||
* Stores new data into the session. | ||
* | ||
* @param string $id The unique session ID | ||
* @param string $key The session variable name | ||
* @param mixed $value The session variable value (must be serializable at some point) | ||
* | ||
* @return bool True if succeeded, false otherwise. | ||
* | ||
* @throws DriverException | ||
*/ | ||
public function store($id, $key, $value); | ||
|
||
/** | ||
* Fetches data from the session. | ||
* | ||
* @param string $id The unique session ID | ||
* @param string $key The session variable name | ||
* @param mixed $default The default value to return | ||
* | ||
* @return mixed|null | ||
* | ||
* @throws DriverException | ||
*/ | ||
public function fetch($id, $key, $default = null); | ||
|
||
/** | ||
* Saves the session data to the persistent storage. | ||
* | ||
* @param string $id The unique session ID | ||
* | ||
* @return bool True if succeeded, false otherwise. | ||
* | ||
* @throws DriverException | ||
*/ | ||
public function save($id); | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace Framework\Session\Driver; | ||
|
||
class NativeDriver implements DriverInterface | ||
{ | ||
const DEFAULT_NS = 'FKNS'; | ||
|
||
private $namespace; | ||
private $allowOverride; | ||
|
||
public function __construct($allowOverride = true, $namespace = self::DEFAULT_NS) | ||
{ | ||
$this->allowOverride = (bool) $allowOverride; | ||
$this->namespace = $namespace; | ||
$_SESSION = []; | ||
} | ||
|
||
public function clear($id) | ||
{ | ||
$_SESSION[$this->namespace] = []; | ||
|
||
return true; | ||
} | ||
|
||
public function store($id, $key, $value) | ||
{ | ||
$fetched = $this->doFetch($id, $key); | ||
if (null !== $fetched && !$this->allowOverride) { | ||
throw new DriverException(sprintf('Overriding session variable "%s" is not permitted.', $key)); | ||
} | ||
|
||
$_SESSION[$this->namespace][$key] = $value; | ||
|
||
return true; | ||
} | ||
|
||
public function fetch($id, $key, $default = null) | ||
{ | ||
if (null !== $value = $this->doFetch($id, $key)) { | ||
return $value; | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
private function doFetch($id, $key) | ||
{ | ||
if (array_key_exists($key, $_SESSION[$this->namespace])) { | ||
return $_SESSION[$this->namespace][$key]; | ||
} | ||
} | ||
|
||
public function save($id) | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.