FilesystemCommonTrait.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 (!file_exists($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($namespace)
  50. {
  51. $ok = true;
  52. foreach ($this->scanHashDir($this->directory) as $file) {
  53. if ('' !== $namespace && 0 !== strpos($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 = (!file_exists($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
  69. }
  70. return $ok;
  71. }
  72. protected function doUnlink($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.uniqid('', true);
  82. }
  83. file_put_contents($this->tmp, $data);
  84. if (null !== $expiresAt) {
  85. touch($this->tmp, $expiresAt);
  86. }
  87. return rename($this->tmp, $file);
  88. } finally {
  89. restore_error_handler();
  90. }
  91. }
  92. private function getFile(string $id, bool $mkdir = false, string $directory = null)
  93. {
  94. // Use MD5 to favor speed over security, which is not an issue here
  95. $hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
  96. $dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
  97. if ($mkdir && !file_exists($dir)) {
  98. @mkdir($dir, 0777, true);
  99. }
  100. return $dir.substr($hash, 2, 20);
  101. }
  102. private function getFileKey(string $file): string
  103. {
  104. return '';
  105. }
  106. private function scanHashDir(string $directory): \Generator
  107. {
  108. if (!file_exists($directory)) {
  109. return;
  110. }
  111. $chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  112. for ($i = 0; $i < 38; ++$i) {
  113. if (!file_exists($directory.$chars[$i])) {
  114. continue;
  115. }
  116. for ($j = 0; $j < 38; ++$j) {
  117. if (!file_exists($dir = $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
  118. continue;
  119. }
  120. foreach (@scandir($dir, \SCANDIR_SORT_NONE) ?: [] as $file) {
  121. if ('.' !== $file && '..' !== $file) {
  122. yield $dir.\DIRECTORY_SEPARATOR.$file;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * @internal
  130. */
  131. public static function throwError($type, $message, $file, $line)
  132. {
  133. throw new \ErrorException($message, 0, $type, $file, $line);
  134. }
  135. /**
  136. * @return array
  137. */
  138. public function __sleep()
  139. {
  140. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  141. }
  142. public function __wakeup()
  143. {
  144. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  145. }
  146. public function __destruct()
  147. {
  148. if (method_exists(parent::class, '__destruct')) {
  149. parent::__destruct();
  150. }
  151. if (null !== $this->tmp && file_exists($this->tmp)) {
  152. unlink($this->tmp);
  153. }
  154. }
  155. }