Maslosoft Embedi API
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/embedi
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
*/
namespace Maslosoft\EmbeDi;
use ArrayAccess;
use Countable;
use Iterator;
use Maslosoft\EmbeDi\Interfaces\MassAssignedInterface;
use ReflectionObject;
use ReflectionProperty;
use Serializable;
/**
* Static Storage class
* This stores variables in static var, depending on owner and instance id
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class StaticStorage implements Countable, Iterator, Serializable, ArrayAccess, MassAssignedInterface
{
/**
* Namespace, current container class name
* @var string
*/
private $ns = '';
/**
* Owner Id
* @var string
*/
private $ownerId = '';
/**
* Instance id
* @var string
*/
private $instanceId = '';
/**
* Preset ID
* @var string
*/
private $presetId = '';
/**
* Key for storage
* @var string
*/
private $key = '';
/**
* Stored values
* @var mixed[][]
*/
public static $values = [];
/**
*
* @param object|string $owner
* @param string $instanceId
*/
public function __construct($owner, $instanceId, $presetId = null)
{
$this->ns = get_class($this);
$this->ownerId = is_object($owner) ? get_class($owner) : $owner;
$this->instanceId = $instanceId;
$this->presetId = $presetId;
if (!empty($presetId))
{
$this->key = $this->instanceId . '.' . $this->presetId;
}
else
{
$this->key = $this->instanceId;
}
// Gracefully init - this is required for subsequent constructor calls
if (!array_key_exists($this->ns, self::$values))
{
self::$values[$this->ns] = [];
}
if (!array_key_exists($this->ownerId, self::$values[$this->ns]))
{
self::$values[$this->ns][$this->ownerId] = [];
}
if (!array_key_exists($this->key, self::$values[$this->ns][$this->ownerId]))
{
self::$values[$this->ns][$this->ownerId][$this->key] = [];
}
$this->_propertize();
}
public function getAll()
{
return self::$values[$this->ns][$this->ownerId][$this->key];
}
public function setAll($values)
{
return self::$values[$this->ns][$this->ownerId][$this->key] = $values;
}
public function removeAll()
{
self::$values[$this->ns][$this->ownerId][$this->key] = [];
}
/**
* Destroy all data in all containers
*/
public function destroy()
{
self::$values = [];
}
public function &__get($name)
{
return self::$values[$this->ns][$this->ownerId][$this->key][$name];
}
public function __set($name, $value)
{
self::$values[$this->ns][$this->ownerId][$this->key][$name] = $value;
}
public function __unset($name)
{
unset(self::$values[$this->ns][$this->ownerId][$this->key][$name]);
}
public function __isset($name)
{
return isset(self::$values[$this->ns][$this->ownerId][$this->key][$name]);
}
// <editor-fold defaultstate="collapsed" desc="Interfaces implementation">
public function count($mode = 'COUNT_NORMAL')
{
return count(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function current()
{
return current(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function key()
{
return key(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function next()
{
return next(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function offsetExists($offset)
{
return array_key_exists($offset, self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function offsetGet($offset)
{
return self::$values[$this->ns][$this->ownerId][$this->instanceId][$offset];
}
public function offsetSet($offset, $value)
{
self::$values[$this->ns][$this->ownerId][$this->instanceId][$offset] = $value;
}
public function offsetUnset($offset)
{
unset(self::$values[$this->ns][$this->ownerId][$this->instanceId][$offset]);
}
public function rewind()
{
reset(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function serialize()
{
return serialize(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
}
public function unserialize($serialized)
{
return self::$values[$this->ns][$this->ownerId][$this->instanceId] = unserialize($serialized);
}
public function valid()
{
return $this->offsetExists($this->key());
}
// </editor-fold>
/**
* This unsets class fields and turns them into storage-aware properties
* @return void
*/
private function _propertize()
{
foreach ((new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property)
{
// http://stackoverflow.com/a/15784768/133408
if (!$property->isStatic())
{
$name = $property->name;
if (!array_key_exists($name, self::$values[$this->ns][$this->ownerId][$this->key]))
{
$this->__set($name, $this->$name);
}
unset($this->$name);
}
}
}
}
API documentation generated by ApiGen