PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php

https://gitlab.com/pr0055/symfonypizza
PHP | 158 lines | 140 code | 6 blank | 12 comment | 7 complexity | 2635f0d4963d7e05a6094a14dc678e69 MD5 | raw file
  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\Adapter;
  11. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. */
  15. class FilesystemAdapter extends AbstractAdapter
  16. {
  17. private $directory;
  18. public function __construct($namespace = '', $defaultLifetime = 0, $directory = null)
  19. {
  20. parent::__construct('', $defaultLifetime);
  21. if (!isset($directory[0])) {
  22. $directory = sys_get_temp_dir().'/symfony-cache';
  23. }
  24. if (isset($namespace[0])) {
  25. if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
  26. throw new InvalidArgumentException(sprintf('FilesystemAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
  27. }
  28. $directory .= '/'.$namespace;
  29. }
  30. if (!file_exists($dir = $directory.'/.')) {
  31. @mkdir($directory, 0777, true);
  32. }
  33. if (false === $dir = realpath($dir)) {
  34. throw new InvalidArgumentException(sprintf('Cache directory does not exist (%s)', $directory));
  35. }
  36. if (!is_writable($dir .= DIRECTORY_SEPARATOR)) {
  37. throw new InvalidArgumentException(sprintf('Cache directory is not writable (%s)', $directory));
  38. }
  39. // On Windows the whole path is limited to 258 chars
  40. if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 234) {
  41. throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory));
  42. }
  43. $this->directory = $dir;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function doFetch(array $ids)
  49. {
  50. $values = array();
  51. $now = time();
  52. foreach ($ids as $id) {
  53. $file = $this->getFile($id);
  54. if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
  55. continue;
  56. }
  57. if ($now >= (int) $expiresAt = fgets($h)) {
  58. fclose($h);
  59. if (isset($expiresAt[0])) {
  60. @unlink($file);
  61. }
  62. } else {
  63. $i = rawurldecode(rtrim(fgets($h)));
  64. $value = stream_get_contents($h);
  65. fclose($h);
  66. if ($i === $id) {
  67. $values[$id] = parent::unserialize($value);
  68. }
  69. }
  70. }
  71. return $values;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function doHave($id)
  77. {
  78. $file = $this->getFile($id);
  79. return file_exists($file) && (@filemtime($file) > time() || $this->doFetch(array($id)));
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function doClear($namespace)
  85. {
  86. $ok = true;
  87. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
  88. $ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok;
  89. }
  90. return $ok;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function doDelete(array $ids)
  96. {
  97. $ok = true;
  98. foreach ($ids as $id) {
  99. $file = $this->getFile($id);
  100. $ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
  101. }
  102. return $ok;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function doSave(array $values, $lifetime)
  108. {
  109. $ok = true;
  110. $expiresAt = time() + ($lifetime ?: 31557600); // 31557600s = 1 year
  111. $tmp = $this->directory.uniqid('', true);
  112. foreach ($values as $id => $value) {
  113. $file = $this->getFile($id, true);
  114. $value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
  115. if (false !== @file_put_contents($tmp, $value)) {
  116. @touch($tmp, $expiresAt);
  117. $ok = @rename($tmp, $file) && $ok;
  118. } else {
  119. $ok = false;
  120. }
  121. }
  122. return $ok;
  123. }
  124. private function getFile($id, $mkdir = false)
  125. {
  126. $hash = str_replace('/', '-', base64_encode(md5($id, true)));
  127. $dir = $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR;
  128. if ($mkdir && !file_exists($dir)) {
  129. @mkdir($dir, 0777, true);
  130. }
  131. return $dir.substr($hash, 2, -2);
  132. }
  133. }