FileStorage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 FilesystemIterator;
  11. use Grav\Framework\Flex\Interfaces\FlexStorageInterface;
  12. use RuntimeException;
  13. use SplFileInfo;
  14. /**
  15. * Class FileStorage
  16. * @package Grav\Framework\Flex\Storage
  17. */
  18. class FileStorage extends FolderStorage
  19. {
  20. /**
  21. * {@inheritdoc}
  22. * @see FlexStorageInterface::__construct()
  23. */
  24. public function __construct(array $options)
  25. {
  26. $this->dataPattern = '{FOLDER}/{KEY}{EXT}';
  27. if (!isset($options['formatter']) && isset($options['pattern'])) {
  28. $options['formatter'] = $this->detectDataFormatter($options['pattern']);
  29. }
  30. parent::__construct($options);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. * @see FlexStorageInterface::getMediaPath()
  35. */
  36. public function getMediaPath(string $key = null): ?string
  37. {
  38. $path = $this->getStoragePath();
  39. if (!$path) {
  40. return null;
  41. }
  42. return $key ? "{$path}/{$key}" : $path;
  43. }
  44. /**
  45. * @param string $src
  46. * @param string $dst
  47. * @return bool
  48. */
  49. public function copyRow(string $src, string $dst): bool
  50. {
  51. if ($this->hasKey($dst)) {
  52. throw new RuntimeException("Cannot copy object: key '{$dst}' is already taken");
  53. }
  54. if (!$this->hasKey($src)) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. * @see FlexStorageInterface::renameRow()
  62. */
  63. public function renameRow(string $src, string $dst): bool
  64. {
  65. if (!$this->hasKey($src)) {
  66. return false;
  67. }
  68. // Remove old file.
  69. $path = $this->getPathFromKey($src);
  70. $file = $this->getFile($path);
  71. $file->delete();
  72. $file->free();
  73. unset($file);
  74. return true;
  75. }
  76. /**
  77. * @param string $src
  78. * @param string $dst
  79. * @return bool
  80. */
  81. protected function copyFolder(string $src, string $dst): bool
  82. {
  83. // Nothing to copy.
  84. return true;
  85. }
  86. /**
  87. * @param string $src
  88. * @param string $dst
  89. * @return bool
  90. */
  91. protected function moveFolder(string $src, string $dst): bool
  92. {
  93. // Nothing to move.
  94. return true;
  95. }
  96. /**
  97. * @param string $key
  98. * @return bool
  99. */
  100. protected function canDeleteFolder(string $key): bool
  101. {
  102. return false;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function getKeyFromPath(string $path): string
  108. {
  109. return basename($path, $this->dataFormatter->getDefaultFileExtension());
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function buildIndex(): array
  115. {
  116. $this->clearCache();
  117. $path = $this->getStoragePath();
  118. if (!$path || !file_exists($path)) {
  119. return [];
  120. }
  121. $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS;
  122. $iterator = new FilesystemIterator($path, $flags);
  123. $list = [];
  124. /** @var SplFileInfo $info */
  125. foreach ($iterator as $filename => $info) {
  126. if (!$info->isFile() || !($key = $this->getKeyFromPath($filename)) || strpos($info->getFilename(), '.') === 0) {
  127. continue;
  128. }
  129. $list[$key] = $this->getObjectMeta($key);
  130. }
  131. ksort($list, SORT_NATURAL | SORT_FLAG_CASE);
  132. return $list;
  133. }
  134. }