Code: Select all
<?php
namespace GlobalVariables;
require_once 'SharedMemoryStore.php';
/**
* Description of GlobalVariablesBoard
*
* @author vladsun
*/
class Board implements \IteratorAggregate
{
const DEFAULT_KEY = 0x1024;
private $_memoryStore;
private $_persistanceStore;
private $_key;
public function __construct($persistanceStore, $key = null)
{
$this->_persistanceStore = $persistanceStore;
$this->_key = $key ? $key : self::DEFAULT_KEY;
$this->_memoryStore = $this->createMemoryStore();
if (!$this->_memoryStore->isKeyValid($this->_key))
{
// $data = $this->_persistanceStore->load($this->_key);
//
// if (!$data)
// {
// $data = $this->_persistanceStore->create($this->_key);
// }
//
// $this->_memoryStore->create($this->_key, $data);
$this->_memoryStore->create($this->_key, array());
}
else
{
$this->_memoryStore->load($this->_key);
// $this->_persistanceStore->store($this->_key, $this->_memoryStore->getData());
}
}
public function getIterator()
{
return new \ArrayIterator($this->_memoryStore->getData());
}
protected function createMemoryStore()
{
return new \GlobalVariables\SharedMemoryStore();
}
public function __get($variable)
{
return $this->_memoryStore->getVariable($variable);
}
public function __set($variable, $value)
{
$this->_memoryStore->setVariable($variable, $value);
}
public function __unset($variable)
{
$this->_memoryStore->unsetVariable($variable);
}
public function __isset($variable)
{
return $this->_memoryStore->isSetVariable($variable);
}
}Code: Select all
<?php
namespace GlobalVariables;
abstract class AbstractMemoryStore
{
abstract public function create($key, $data);
abstract public function store($key, $data);
abstract public function load($key);
abstract public function isKeyValid($key);
abstract public function getData();
abstract public function getVariable($variable);
abstract public function setVariable($variable, $value);
abstract public function isSetVariable($variable);
abstract public function unsetVariable($variable);
}Code: Select all
<?php
namespace GlobalVariables;
require_once 'AbstractMemoryStore.php';
class SharedMemoryStore extends AbstractMemoryStore
{
protected $initialMemorySize;
protected $incrementMemorySize;
protected $permitions;
protected $key;
protected $data;
protected $shmID = null;
public function __construct($initialMemorySize = 10240, $incrementMemorySize = 10240, $permitions = 0x666)
{
$this->initialMemorySize = $initialMemorySize;
$this->incrementMemorySize = $incrementMemorySize;
$this->permitions = $permitions;
}
public function getData()
{
$this->attach();
$header = $this->getHeader();
return $header['data'];
}
public function getVariable($variable)
{
$this->attach();
$header = $this->getHeader();
if (isset($header['data'][$variable]))
{
return $header['data'][$variable];
}
else
{
trigger_error('Variable '.$variable.' is undefined.');
return null;
}
}
public function setVariable($variable, $value)
{
$this->attach();
$header = $this->getHeader();
$header['data'][$variable] = $value;
$this->writeHeader($header);
}
public function isSetVariable($variable)
{
$this->attach();
$header = $this->getHeader();
return isset($header['data'][$variable]);
}
public function unsetVariable($variable)
{
$this->attach();
$header = $this->getHeader();
unset($header['data'][$variable]);
$this->writeHeader($header);
}
public function isKeyValid($key)
{
$this->attach($key);
if (shm_has_var($this->shmID, 1))
{
$this->detach();
return true;
}
$this->detach();
return false;
}
public function create($key, $data)
{
$this->data = $data;
$this->key = $key;
$this->attach();
$header = array
(
'initialMemorySize' => $this->initialMemorySize,
'incrementMemorySize' => $this->incrementMemorySize,
'permitions' => $this->permitions,
'data' => $this->data,
);
$this->writeHeader($header);
}
public function store($key, $data)
{
$this->attach();
$header = $this->getHeader();
unset($header->data[$variable]);
$this->writeHeader($header);
}
public function load($key)
{
$this->attach($key);
}
protected function getHeader()
{
$this->attach();
if (shm_has_var($this->shmID, 1))
return shm_get_var($this->shmID, 1);
throw new HeaderNotFound();
}
protected function writeHeader($header)
{
$this->attach();
shm_put_var($this->shmID, 1, $header);
}
protected function detach()
{
if ($this->shmID)
{
shm_detach($this->shmID);
$this->shmID = null;
}
}
protected function attach($key = null)
{
if (!$key)
{
$key = $this->key;
}
else
{
$this->key = $key;
}
if (!$this->shmID)
{
$this->shmID = shm_attach($this->key, $this->initialMemorySize, $this->permitions);
if (!$this->shmID)
{
throw new SharedMemoryNotFound();
}
}
}
}
class HeaderNotFound extends \Exception {}
class SharedMemoryNotFound extends \Exception {}Code: Select all
<?php
require_once 'Board.php';
define('APP_STORE_KEY', 0x4096);
$global = new \GlobalVariables\Board(null, APP_STORE_KEY);
$global->v1 = 10;
$global->v2 = 20;
sleep(10);
$global->v3 = array(30);Code: Select all
<?php
require_once 'Board.php';
define('APP_STORE_KEY', 0x4096);
$global = new \GlobalVariables\Board(null, APP_STORE_KEY);
foreach ($global as $key => $value)
echo "$key = ". var_export($value, true)." <br/>";
unset($global->v3);
echo $global->v3;
if (isset($global->v2))
echo 'Variable "v2" is SET';There are obvious TODOs