1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99:
<?php
namespace bandwidthThrottle\tokenBucket\storage;
use malkusch\lock\mutex\NoMutex;
use bandwidthThrottle\tokenBucket\storage\scope\SessionScope;
/**
* Session based storage which is shared for one user accross requests.
*
* This storage is in the session scope.
*
* As PHP's session are thread safe this implementation doesn't provide a
* locking Mutex.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license WTFPL
*/
final class SessionStorage implements Storage, SessionScope
{
/**
* @var Mutex The mutex.
*/
private $mutex;
/**
* @var String The session key for this bucket.
*/
private $key;
/**
* @internal
*/
const SESSION_NAMESPACE = "TokenBucket_";
/**
* Sets the bucket's name.
*
* @param string $name The bucket's name.
*/
public function __construct($name)
{
$this->mutex = new NoMutex();
$this->key = self::SESSION_NAMESPACE . $name;
}
public function getMutex()
{
return $this->mutex;
}
public function bootstrap($microtime)
{
$this->setMicrotime($microtime);
}
/**
* @SuppressWarnings(PHPMD)
* @internal
*/
public function getMicrotime()
{
return $_SESSION[$this->key];
}
/**
* @SuppressWarnings(PHPMD)
* @internal
*/
public function isBootstrapped()
{
return isset($_SESSION[$this->key]);
}
/**
* @SuppressWarnings(PHPMD)
* @internal
*/
public function remove()
{
unset($_SESSION[$this->key]);
}
/**
* @SuppressWarnings(PHPMD)
* @internal
*/
public function setMicrotime($microtime)
{
$_SESSION[$this->key] = $microtime;
}
public function letMicrotimeUnchanged()
{
}
}