FilesystemCommonTrait.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. *
  15. * @internal
  16. */
  17. trait FilesystemCommonTrait
  18. {
  19. private $directory;
  20. private $tmp;
  21. private function init(string $namespace, ?string $directory)
  22. {
  23. if (!isset($directory[0])) {
  24. $directory = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'symfony-cache';
  25. } else {
  26. $directory = realpath($directory) ?: $directory;
  27. }
  28. if (isset($namespace[0])) {
  29. if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
  30. throw new InvalidArgumentException(sprintf('Namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
  31. }
  32. $directory .= \DIRECTORY_SEPARATOR.$namespace;
  33. } else {
  34. $directory .= \DIRECTORY_SEPARATOR.'@';
  35. }
  36. if (!is_dir($directory)) {
  37. @mkdir($directory, 0777, true);
  38. }
  39. $directory .= \DIRECTORY_SEPARATOR;
  40. // On Windows the whole path is limited to 258 chars
  41. if ('\\' === \DIRECTORY_SEPARATOR && \strlen($directory) > 234) {
  42. throw new InvalidArgumentException(sprintf('Cache directory too long (%s).', $directory));
  43. }
  44. $this->directory = $directory;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function doClear(string $namespace)
  50. {
  51. $ok = true;
  52. foreach ($this->scanHashDir($this->directory) as $file) {
  53. if ('' !== $namespace && !str_starts_with($this->getFileKey($file), $namespace)) {
  54. continue;
  55. }
  56. $ok = ($this->doUnlink($file) || !file_exists($file)) && $ok;
  57. }
  58. return $ok;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doDelete(array $ids)
  64. {
  65. $ok = true;
  66. foreach ($ids as $id) {
  67. $file = $this->getFile($id);
  68. $ok = (!is_file($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
  69. }
  70. return $ok;
  71. }
  72. protected function doUnlink(string $file)
  73. {
  74. return @unlink($file);
  75. }
  76. private function write(string $file, string $data, int $expiresAt = null)
  77. {
  78. set_error_handler(__CLASS__.'::throwError');
  79. try {
  80. if (null === $this->tmp) {
  81. $this->tmp = $this->directory.bin2hex(random_bytes(6));
  82. }
  83. try {
  84. $h = fopen($this->tmp, 'x');
  85. } catch (\ErrorException $e) {
  86. if (!str_contains($e->getMessage(), 'File exists')) {
  87. throw $e;
  88. }
  89. $this->tmp = $this->directory.bin2hex(random_bytes(6));
  90. $h = fopen($this->tmp, 'x');
  91. }
  92. fwrite($h, $data);
  93. fclose($h);
  94. if (null !== $expiresAt) {
  95. touch($this->tmp, $expiresAt);
  96. }
  97. return rename($this->tmp, $file);
  98. } finally {
  99. restore_error_handler();
  100. }
  101. }
  102. private function getFile(string $id, bool $mkdir = false, string $directory = null)
  103. {
  104. // Use MD5 to favor speed over security, which is not an issue here
  105. $hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
  106. $dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
  107. if ($mkdir && !is_dir($dir)) {
  108. @mkdir($dir, 0777, true);
  109. }
  110. return $dir.substr($hash, 2, 20);
  111. }
  112. private function getFileKey(string $file): string
  113. {
  114. return '';
  115. }
  116. private function scanHashDir(string $directory): \Generator
  117. {
  118. if (!is_dir($directory)) {
  119. return;
  120. }
  121. $chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  122. for ($i = 0; $i < 38; ++$i) {
  123. if (!is_dir($directory.$chars[$i])) {
  124. continue;
  125. }
  126. for ($j = 0; $j < 38; ++$j) {
  127. if (!is_dir($dir = $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
  128. continue;
  129. }
  130. foreach (@scandir($dir, \SCANDIR_SORT_NONE) ?: [] as $file) {
  131. if ('.' !== $file && '..' !== $file) {
  132. yield $dir.\DIRECTORY_SEPARATOR.$file;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * @internal
  140. */
  141. public static function throwError(int $type, string $message, string $file, int $line)
  142. {
  143. throw new \ErrorException($message, 0, $type, $file, $line);
  144. }
  145. /**
  146. * @return array
  147. */
  148. public function __sleep()
  149. {
  150. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  151. }
  152. public function __wakeup()
  153. {
  154. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  155. }
  156. public function __destruct()
  157. {
  158. if (method_exists(parent::class, '__destruct')) {
  159. parent::__destruct();
  160. }
  161. if (null !== $this->tmp && is_file($this->tmp)) {
  162. unlink($this->tmp);
  163. }
  164. }
  165. }