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: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375:
<?php
namespace Webmozart\KeyValueStore;
use stdClass;
use Webmozart\Assert\Assert;
use Webmozart\Json\DecodingFailedException;
use Webmozart\Json\EncodingFailedException;
use Webmozart\Json\FileNotFoundException;
use Webmozart\Json\IOException;
use Webmozart\Json\JsonDecoder;
use Webmozart\Json\JsonEncoder;
use Webmozart\KeyValueStore\Api\CountableStore;
use Webmozart\KeyValueStore\Api\NoSuchKeyException;
use Webmozart\KeyValueStore\Api\ReadException;
use Webmozart\KeyValueStore\Api\SortableStore;
use Webmozart\KeyValueStore\Api\UnsupportedValueException;
use Webmozart\KeyValueStore\Api\WriteException;
use Webmozart\KeyValueStore\Util\KeyUtil;
use Webmozart\KeyValueStore\Util\Serializer;
class JsonFileStore implements SortableStore, CountableStore
{
const NO_SERIALIZE_STRINGS = 1;
const NO_SERIALIZE_ARRAYS = 2;
const ESCAPE_GT_LT = 4;
const ESCAPE_AMPERSAND = 8;
const ESCAPE_SINGLE_QUOTE = 16;
const ESCAPE_DOUBLE_QUOTE = 32;
const NO_ESCAPE_SLASH = 64;
const NO_ESCAPE_UNICODE = 128;
const PRETTY_PRINT = 256;
const TERMINATE_WITH_LINE_FEED = 512;
const MAX_FLOAT = 1.0E+14;
private $path;
private $flags;
private $encoder;
private $decoder;
public function __construct($path, $flags = 0)
{
Assert::string($path, 'The path must be a string. Got: %s');
Assert::notEmpty($path, 'The path must not be empty.');
Assert::integer($flags, 'The flags must be an integer. Got: %s');
$this->path = $path;
$this->flags = $flags;
$this->encoder = new JsonEncoder();
$this->encoder->setEscapeGtLt($this->flags & self::ESCAPE_GT_LT);
$this->encoder->setEscapeAmpersand($this->flags & self::ESCAPE_AMPERSAND);
$this->encoder->setEscapeSingleQuote($this->flags & self::ESCAPE_SINGLE_QUOTE);
$this->encoder->setEscapeDoubleQuote($this->flags & self::ESCAPE_DOUBLE_QUOTE);
$this->encoder->setEscapeSlash(!($this->flags & self::NO_ESCAPE_SLASH));
$this->encoder->setEscapeUnicode(!($this->flags & self::NO_ESCAPE_UNICODE));
$this->encoder->setPrettyPrinting($this->flags & self::PRETTY_PRINT);
$this->encoder->setTerminateWithLineFeed($this->flags & self::TERMINATE_WITH_LINE_FEED);
$this->decoder = new JsonDecoder();
$this->decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);
}
public function set($key, $value)
{
KeyUtil::validate($key);
if (is_float($value) && $value > self::MAX_FLOAT) {
throw new UnsupportedValueException('The JSON file store cannot handle floats larger than 1.0E+14.');
}
$data = $this->load();
$data[$key] = $this->serializeValue($value);
$this->save($data);
}
public function get($key, $default = null)
{
KeyUtil::validate($key);
$data = $this->load();
if (!array_key_exists($key, $data)) {
return $default;
}
return $this->unserializeValue($data[$key]);
}
public function getOrFail($key)
{
KeyUtil::validate($key);
$data = $this->load();
if (!array_key_exists($key, $data)) {
throw NoSuchKeyException::forKey($key);
}
return $this->unserializeValue($data[$key]);
}
public function getMultiple(array $keys, $default = null)
{
$values = array();
$data = $this->load();
foreach ($keys as $key) {
KeyUtil::validate($key);
if (array_key_exists($key, $data)) {
$value = $this->unserializeValue($data[$key]);
} else {
$value = $default;
}
$values[$key] = $value;
}
return $values;
}
public function getMultipleOrFail(array $keys)
{
$values = array();
$data = $this->load();
foreach ($keys as $key) {
KeyUtil::validate($key);
if (!array_key_exists($key, $data)) {
throw NoSuchKeyException::forKey($key);
}
$values[$key] = $this->unserializeValue($data[$key]);
}
return $values;
}
public function remove($key)
{
KeyUtil::validate($key);
$data = $this->load();
if (!array_key_exists($key, $data)) {
return false;
}
unset($data[$key]);
$this->save($data);
return true;
}
public function exists($key)
{
KeyUtil::validate($key);
$data = $this->load();
return array_key_exists($key, $data);
}
public function clear()
{
$this->save(new stdClass());
}
public function keys()
{
return array_keys($this->load());
}
public function sort($flags = SORT_REGULAR)
{
$data = $this->load();
ksort($data, $flags);
$this->save($data);
}
public function count()
{
$data = $this->load();
return count($data);
}
private function load()
{
try {
return $this->decoder->decodeFile($this->path);
} catch (FileNotFoundException $e) {
return array();
} catch (DecodingFailedException $e) {
throw new ReadException($e->getMessage(), 0, $e);
} catch (IOException $e) {
throw new ReadException($e->getMessage(), 0, $e);
}
}
private function save($data)
{
try {
$this->encoder->encodeFile($data, $this->path);
} catch (EncodingFailedException $e) {
if (JSON_ERROR_UTF8 === $e->getCode()) {
throw UnsupportedValueException::forType('binary', $this);
}
throw new WriteException($e->getMessage(), 0, $e);
} catch (IOException $e) {
throw new WriteException($e->getMessage(), 0, $e);
}
}
private function serializeValue($value)
{
$serializeValue = (is_string($value) && !($this->flags & self::NO_SERIALIZE_STRINGS))
|| (is_array($value) && !($this->flags & self::NO_SERIALIZE_ARRAYS))
|| (null !== $value && !is_scalar($value) && !is_array($value));
if ($serializeValue) {
return Serializer::serialize($value);
}
if (is_array($value)) {
return array_map(array($this, 'serializeValue'), $value);
}
return $value;
}
private function unserializeValue($value)
{
$unserializeValue = is_string($value) && (
!($this->flags & self::NO_SERIALIZE_STRINGS)
|| 'O:' === ($prefix = substr($value, 0, 2))
|| ('a:' === $prefix && !($this->flags & self::NO_SERIALIZE_ARRAYS))
);
if ($unserializeValue) {
return Serializer::unserialize($value);
}
if (is_array($value)) {
return array_map(array($this, 'unserializeValue'), $value);
}
return $value;
}
}