AbstractFilesystemStorage.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\File\CompiledJsonFile;
  11. use Grav\Common\File\CompiledMarkdownFile;
  12. use Grav\Common\File\CompiledYamlFile;
  13. use Grav\Common\Grav;
  14. use Grav\Framework\File\Formatter\JsonFormatter;
  15. use Grav\Framework\File\Formatter\MarkdownFormatter;
  16. use Grav\Framework\File\Formatter\YamlFormatter;
  17. use Grav\Framework\File\Interfaces\FileFormatterInterface;
  18. use Grav\Framework\Flex\Interfaces\FlexStorageInterface;
  19. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  20. use RuntimeException;
  21. use function is_array;
  22. /**
  23. * Class AbstractFilesystemStorage
  24. * @package Grav\Framework\Flex\Storage
  25. */
  26. abstract class AbstractFilesystemStorage implements FlexStorageInterface
  27. {
  28. /** @var FileFormatterInterface */
  29. protected $dataFormatter;
  30. /** @var string */
  31. protected $keyField = 'storage_key';
  32. /** @var int */
  33. protected $keyLen = 32;
  34. /** @var bool */
  35. protected $caseSensitive = true;
  36. /**
  37. * @return bool
  38. */
  39. public function isIndexed(): bool
  40. {
  41. return false;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. * @see FlexStorageInterface::hasKeys()
  46. */
  47. public function hasKeys(array $keys): array
  48. {
  49. $list = [];
  50. foreach ($keys as $key) {
  51. $list[$key] = $this->hasKey((string)$key);
  52. }
  53. return $list;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. * @see FlexStorageInterface::getKeyField()
  58. */
  59. public function getKeyField(): string
  60. {
  61. return $this->keyField;
  62. }
  63. /**
  64. * @param array $keys
  65. * @param bool $includeParams
  66. * @return string
  67. */
  68. public function buildStorageKey(array $keys, bool $includeParams = true): string
  69. {
  70. $key = $keys['key'] ?? '';
  71. $params = $includeParams ? $this->buildStorageKeyParams($keys) : '';
  72. return $params ? "{$key}|{$params}" : $key;
  73. }
  74. /**
  75. * @param array $keys
  76. * @return string
  77. */
  78. public function buildStorageKeyParams(array $keys): string
  79. {
  80. return '';
  81. }
  82. /**
  83. * @param array $row
  84. * @return array
  85. */
  86. public function extractKeysFromRow(array $row): array
  87. {
  88. return [
  89. 'key' => $this->normalizeKey($row[$this->keyField] ?? '')
  90. ];
  91. }
  92. /**
  93. * @param string $key
  94. * @return array
  95. */
  96. public function extractKeysFromStorageKey(string $key): array
  97. {
  98. return [
  99. 'key' => $key
  100. ];
  101. }
  102. /**
  103. * @param string|array $formatter
  104. * @return void
  105. */
  106. protected function initDataFormatter($formatter): void
  107. {
  108. // Initialize formatter.
  109. if (!is_array($formatter)) {
  110. $formatter = ['class' => $formatter];
  111. }
  112. $formatterClassName = $formatter['class'] ?? JsonFormatter::class;
  113. $formatterOptions = $formatter['options'] ?? [];
  114. $this->dataFormatter = new $formatterClassName($formatterOptions);
  115. }
  116. /**
  117. * @param string $filename
  118. * @return string|null
  119. */
  120. protected function detectDataFormatter(string $filename): ?string
  121. {
  122. if (preg_match('|(\.[a-z0-9]*)$|ui', $filename, $matches)) {
  123. switch ($matches[1]) {
  124. case '.json':
  125. return JsonFormatter::class;
  126. case '.yaml':
  127. return YamlFormatter::class;
  128. case '.md':
  129. return MarkdownFormatter::class;
  130. }
  131. }
  132. return null;
  133. }
  134. /**
  135. * @param string $filename
  136. * @return CompiledJsonFile|CompiledYamlFile|CompiledMarkdownFile
  137. */
  138. protected function getFile(string $filename)
  139. {
  140. $filename = $this->resolvePath($filename);
  141. // TODO: start using the new file classes.
  142. switch ($this->dataFormatter->getDefaultFileExtension()) {
  143. case '.json':
  144. $file = CompiledJsonFile::instance($filename);
  145. break;
  146. case '.yaml':
  147. $file = CompiledYamlFile::instance($filename);
  148. break;
  149. case '.md':
  150. $file = CompiledMarkdownFile::instance($filename);
  151. break;
  152. default:
  153. throw new RuntimeException('Unknown extension type ' . $this->dataFormatter->getDefaultFileExtension());
  154. }
  155. return $file;
  156. }
  157. /**
  158. * @param string $path
  159. * @return string
  160. */
  161. protected function resolvePath(string $path): string
  162. {
  163. /** @var UniformResourceLocator $locator */
  164. $locator = Grav::instance()['locator'];
  165. if (!$locator->isStream($path)) {
  166. return GRAV_ROOT . "/{$path}";
  167. }
  168. return $locator->getResource($path);
  169. }
  170. /**
  171. * Generates a random, unique key for the row.
  172. *
  173. * @return string
  174. */
  175. protected function generateKey(): string
  176. {
  177. return substr(hash('sha256', random_bytes($this->keyLen)), 0, $this->keyLen);
  178. }
  179. /**
  180. * @param string $key
  181. * @return string
  182. */
  183. public function normalizeKey(string $key): string
  184. {
  185. if ($this->caseSensitive === true) {
  186. return $key;
  187. }
  188. return mb_strtolower($key);
  189. }
  190. /**
  191. * Checks if a key is valid.
  192. *
  193. * @param string $key
  194. * @return bool
  195. */
  196. protected function validateKey(string $key): bool
  197. {
  198. return $key && (bool) preg_match('/^[^\\/?*:;{}\\\\\\n]+$/u', $key);
  199. }
  200. }