SimpleStorage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Framework\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Framework\Flex\Storage;
  10. use Grav\Common\Data\Data;
  11. use Grav\Common\Filesystem\Folder;
  12. use Grav\Framework\Filesystem\Filesystem;
  13. use InvalidArgumentException;
  14. use LogicException;
  15. use RuntimeException;
  16. use function is_scalar;
  17. use function is_string;
  18. /**
  19. * Class SimpleStorage
  20. * @package Grav\Framework\Flex\Storage
  21. */
  22. class SimpleStorage extends AbstractFilesystemStorage
  23. {
  24. /** @var string */
  25. protected $dataFolder;
  26. /** @var string */
  27. protected $dataPattern;
  28. /** @var string */
  29. protected $prefix;
  30. /** @var array|null */
  31. protected $data;
  32. /** @var int */
  33. protected $modified = 0;
  34. /**
  35. * {@inheritdoc}
  36. * @see FlexStorageInterface::__construct()
  37. */
  38. public function __construct(array $options)
  39. {
  40. if (!isset($options['folder'])) {
  41. throw new InvalidArgumentException("Argument \$options is missing 'folder'");
  42. }
  43. $formatter = $options['formatter'] ?? $this->detectDataFormatter($options['folder']);
  44. $this->initDataFormatter($formatter);
  45. $filesystem = Filesystem::getInstance(true);
  46. $extension = $this->dataFormatter->getDefaultFileExtension();
  47. $pattern = basename($options['folder']);
  48. $this->dataPattern = basename($pattern, $extension) . $extension;
  49. $this->dataFolder = $filesystem->dirname($options['folder']);
  50. $this->keyField = $options['key'] ?? 'storage_key';
  51. $this->keyLen = (int)($options['key_len'] ?? 32);
  52. $this->prefix = $options['prefix'] ?? null;
  53. // Make sure that the data folder exists.
  54. if (!file_exists($this->dataFolder)) {
  55. try {
  56. Folder::create($this->dataFolder);
  57. } catch (RuntimeException $e) {
  58. throw new RuntimeException(sprintf('Flex: %s', $e->getMessage()));
  59. }
  60. }
  61. }
  62. /**
  63. * @return void
  64. */
  65. public function clearCache(): void
  66. {
  67. $this->data = null;
  68. $this->modified = 0;
  69. }
  70. /**
  71. * @param string[] $keys
  72. * @param bool $reload
  73. * @return array
  74. */
  75. public function getMetaData(array $keys, bool $reload = false): array
  76. {
  77. if (null === $this->data || $reload) {
  78. $this->buildIndex();
  79. }
  80. $list = [];
  81. foreach ($keys as $key) {
  82. $list[$key] = $this->getObjectMeta((string)$key);
  83. }
  84. return $list;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. * @see FlexStorageInterface::getExistingKeys()
  89. */
  90. public function getExistingKeys(): array
  91. {
  92. return $this->buildIndex();
  93. }
  94. /**
  95. * {@inheritdoc}
  96. * @see FlexStorageInterface::hasKey()
  97. */
  98. public function hasKey(string $key): bool
  99. {
  100. if (null === $this->data) {
  101. $this->buildIndex();
  102. }
  103. return $key && strpos($key, '@@') === false && isset($this->data[$key]);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. * @see FlexStorageInterface::createRows()
  108. */
  109. public function createRows(array $rows): array
  110. {
  111. if (null === $this->data) {
  112. $this->buildIndex();
  113. }
  114. $list = [];
  115. foreach ($rows as $key => $row) {
  116. $list[$key] = $this->saveRow('@@', $rows);
  117. }
  118. if ($list) {
  119. $this->save();
  120. }
  121. return $list;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. * @see FlexStorageInterface::readRows()
  126. */
  127. public function readRows(array $rows, array &$fetched = null): array
  128. {
  129. if (null === $this->data) {
  130. $this->buildIndex();
  131. }
  132. $list = [];
  133. foreach ($rows as $key => $row) {
  134. if (null === $row || is_scalar($row)) {
  135. // Only load rows which haven't been loaded before.
  136. $key = (string)$key;
  137. $list[$key] = $this->hasKey($key) ? $this->loadRow($key) : null;
  138. if (null !== $fetched) {
  139. $fetched[$key] = $list[$key];
  140. }
  141. } else {
  142. // Keep the row if it has been loaded.
  143. $list[$key] = $row;
  144. }
  145. }
  146. return $list;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. * @see FlexStorageInterface::updateRows()
  151. */
  152. public function updateRows(array $rows): array
  153. {
  154. if (null === $this->data) {
  155. $this->buildIndex();
  156. }
  157. $save = false;
  158. $list = [];
  159. foreach ($rows as $key => $row) {
  160. $key = (string)$key;
  161. if ($this->hasKey($key)) {
  162. $list[$key] = $this->saveRow($key, $row);
  163. $save = true;
  164. } else {
  165. $list[$key] = null;
  166. }
  167. }
  168. if ($save) {
  169. $this->save();
  170. }
  171. return $list;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. * @see FlexStorageInterface::deleteRows()
  176. */
  177. public function deleteRows(array $rows): array
  178. {
  179. if (null === $this->data) {
  180. $this->buildIndex();
  181. }
  182. $list = [];
  183. foreach ($rows as $key => $row) {
  184. $key = (string)$key;
  185. if ($this->hasKey($key)) {
  186. unset($this->data[$key]);
  187. $list[$key] = $row;
  188. }
  189. }
  190. if ($list) {
  191. $this->save();
  192. }
  193. return $list;
  194. }
  195. /**
  196. * {@inheritdoc}
  197. * @see FlexStorageInterface::replaceRows()
  198. */
  199. public function replaceRows(array $rows): array
  200. {
  201. if (null === $this->data) {
  202. $this->buildIndex();
  203. }
  204. $list = [];
  205. foreach ($rows as $key => $row) {
  206. $list[$key] = $this->saveRow((string)$key, $row);
  207. }
  208. if ($list) {
  209. $this->save();
  210. }
  211. return $list;
  212. }
  213. /**
  214. * @param string $src
  215. * @param string $dst
  216. * @return bool
  217. */
  218. public function copyRow(string $src, string $dst): bool
  219. {
  220. if ($this->hasKey($dst)) {
  221. throw new RuntimeException("Cannot copy object: key '{$dst}' is already taken");
  222. }
  223. if (!$this->hasKey($src)) {
  224. return false;
  225. }
  226. $this->data[$dst] = $this->data[$src];
  227. return true;
  228. }
  229. /**
  230. * {@inheritdoc}
  231. * @see FlexStorageInterface::renameRow()
  232. */
  233. public function renameRow(string $src, string $dst): bool
  234. {
  235. if (null === $this->data) {
  236. $this->buildIndex();
  237. }
  238. if ($this->hasKey($dst)) {
  239. throw new RuntimeException("Cannot rename object: key '{$dst}' is already taken");
  240. }
  241. if (!$this->hasKey($src)) {
  242. return false;
  243. }
  244. // Change single key in the array without changing the order or value.
  245. $keys = array_keys($this->data);
  246. $keys[array_search($src, $keys, true)] = $dst;
  247. $data = array_combine($keys, $this->data);
  248. if (false === $data) {
  249. throw new LogicException('Bad data');
  250. }
  251. $this->data = $data;
  252. return true;
  253. }
  254. /**
  255. * {@inheritdoc}
  256. * @see FlexStorageInterface::getStoragePath()
  257. */
  258. public function getStoragePath(string $key = null): ?string
  259. {
  260. return $this->dataFolder . '/' . $this->dataPattern;
  261. }
  262. /**
  263. * {@inheritdoc}
  264. * @see FlexStorageInterface::getMediaPath()
  265. */
  266. public function getMediaPath(string $key = null): ?string
  267. {
  268. return null;
  269. }
  270. /**
  271. * Prepares the row for saving and returns the storage key for the record.
  272. *
  273. * @param array $row
  274. */
  275. protected function prepareRow(array &$row): void
  276. {
  277. unset($row[$this->keyField]);
  278. }
  279. /**
  280. * @param string $key
  281. * @return array
  282. */
  283. protected function loadRow(string $key): ?array
  284. {
  285. $data = $this->data[$key] ?? [];
  286. if ($this->keyField !== 'storage_key') {
  287. $data[$this->keyField] = $key;
  288. }
  289. $data['__META'] = $this->getObjectMeta($key);
  290. return $data;
  291. }
  292. /**
  293. * @param string $key
  294. * @param array $row
  295. * @return array
  296. */
  297. protected function saveRow(string $key, array $row): array
  298. {
  299. try {
  300. if (isset($row[$this->keyField])) {
  301. $key = $row[$this->keyField];
  302. }
  303. if (strpos($key, '@@') !== false) {
  304. $key = $this->getNewKey();
  305. }
  306. // Check if the row already exists and if the key has been changed.
  307. $oldKey = $row['__META']['storage_key'] ?? null;
  308. if (is_string($oldKey) && $oldKey !== $key) {
  309. $isCopy = $row['__META']['copy'] ?? false;
  310. if ($isCopy) {
  311. $this->copyRow($oldKey, $key);
  312. } else {
  313. $this->renameRow($oldKey, $key);
  314. }
  315. }
  316. $this->prepareRow($row);
  317. unset($row['__META'], $row['__ERROR']);
  318. $this->data[$key] = $row;
  319. } catch (RuntimeException $e) {
  320. throw new RuntimeException(sprintf('Flex saveRow(%s): %s', $key, $e->getMessage()));
  321. }
  322. $row['__META'] = $this->getObjectMeta($key, true);
  323. return $row;
  324. }
  325. /**
  326. * @param string $key
  327. * @param bool $variations
  328. * @return array
  329. */
  330. public function parseKey(string $key, bool $variations = true): array
  331. {
  332. return [
  333. 'key' => $key,
  334. ];
  335. }
  336. protected function save(): void
  337. {
  338. if (null === $this->data) {
  339. $this->buildIndex();
  340. }
  341. try {
  342. $path = $this->getStoragePath();
  343. if (!$path) {
  344. throw new RuntimeException('Storage path is not defined');
  345. }
  346. $file = $this->getFile($path);
  347. if ($this->prefix) {
  348. $data = new Data((array)$file->content());
  349. $content = $data->set($this->prefix, $this->data)->toArray();
  350. } else {
  351. $content = $this->data;
  352. }
  353. $file->save($content);
  354. $this->modified = (int)$file->modified(); // cast false to 0
  355. } catch (RuntimeException $e) {
  356. throw new RuntimeException(sprintf('Flex save(): %s', $e->getMessage()));
  357. } finally {
  358. if (isset($file)) {
  359. $file->free();
  360. unset($file);
  361. }
  362. }
  363. }
  364. /**
  365. * Get key from the filesystem path.
  366. *
  367. * @param string $path
  368. * @return string
  369. */
  370. protected function getKeyFromPath(string $path): string
  371. {
  372. return basename($path);
  373. }
  374. /**
  375. * Returns list of all stored keys in [key => timestamp] pairs.
  376. *
  377. * @return array
  378. */
  379. protected function buildIndex(): array
  380. {
  381. $path = $this->getStoragePath();
  382. if (!$path) {
  383. $this->data = [];
  384. return [];
  385. }
  386. $file = $this->getFile($path);
  387. $this->modified = (int)$file->modified(); // cast false to 0
  388. $content = (array) $file->content();
  389. if ($this->prefix) {
  390. $data = new Data($content);
  391. $content = $data->get($this->prefix);
  392. }
  393. $file->free();
  394. unset($file);
  395. $this->data = $content;
  396. $list = [];
  397. foreach ($this->data as $key => $info) {
  398. $list[$key] = $this->getObjectMeta((string)$key);
  399. }
  400. return $list;
  401. }
  402. /**
  403. * @param string $key
  404. * @param bool $reload
  405. * @return array
  406. */
  407. protected function getObjectMeta(string $key, bool $reload = false): array
  408. {
  409. $modified = isset($this->data[$key]) ? $this->modified : 0;
  410. return [
  411. 'storage_key' => $key,
  412. 'key' => $key,
  413. 'storage_timestamp' => $modified
  414. ];
  415. }
  416. /**
  417. * @return string
  418. */
  419. protected function getNewKey(): string
  420. {
  421. if (null === $this->data) {
  422. $this->buildIndex();
  423. }
  424. // Make sure that the key doesn't exist.
  425. do {
  426. $key = $this->generateKey();
  427. } while (isset($this->data[$key]));
  428. return $key;
  429. }
  430. }