FileCache.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Cache;
  20. /**
  21. * Base file cache driver.
  22. *
  23. * @since 2.3
  24. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  25. */
  26. abstract class FileCache extends CacheProvider
  27. {
  28. /**
  29. * The cache directory.
  30. *
  31. * @var string
  32. */
  33. protected $directory;
  34. /**
  35. * The cache file extension.
  36. *
  37. * @var string|null
  38. */
  39. protected $extension;
  40. /**
  41. * Constructor.
  42. *
  43. * @param string $directory The cache directory.
  44. * @param string|null $extension The cache file extension.
  45. *
  46. * @throws \InvalidArgumentException
  47. */
  48. public function __construct($directory, $extension = null)
  49. {
  50. if ( ! is_dir($directory) && ! @mkdir($directory, 0777, true)) {
  51. throw new \InvalidArgumentException(sprintf(
  52. 'The directory "%s" does not exist and could not be created.',
  53. $directory
  54. ));
  55. }
  56. if ( ! is_writable($directory)) {
  57. throw new \InvalidArgumentException(sprintf(
  58. 'The directory "%s" is not writable.',
  59. $directory
  60. ));
  61. }
  62. $this->directory = realpath($directory);
  63. $this->extension = $extension ?: $this->extension;
  64. }
  65. /**
  66. * Gets the cache directory.
  67. *
  68. * @return string
  69. */
  70. public function getDirectory()
  71. {
  72. return $this->directory;
  73. }
  74. /**
  75. * Gets the cache file extension.
  76. *
  77. * @return string|null
  78. */
  79. public function getExtension()
  80. {
  81. return $this->extension;
  82. }
  83. /**
  84. * @param string $id
  85. *
  86. * @return string
  87. */
  88. protected function getFilename($id)
  89. {
  90. $hash = hash('sha256', $id);
  91. $path = implode(str_split($hash, 16), DIRECTORY_SEPARATOR);
  92. $path = $this->directory . DIRECTORY_SEPARATOR . $path;
  93. $id = preg_replace('@[\\\/:"*?<>|]+@', '', $id);
  94. return $path . DIRECTORY_SEPARATOR . $id . $this->extension;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. protected function doDelete($id)
  100. {
  101. return @unlink($this->getFilename($id));
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. protected function doFlush()
  107. {
  108. foreach ($this->getIterator() as $name => $file) {
  109. @unlink($name);
  110. }
  111. return true;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. protected function doGetStats()
  117. {
  118. $usage = 0;
  119. foreach ($this->getIterator() as $name => $file) {
  120. $usage += $file->getSize();
  121. }
  122. $free = disk_free_space($this->directory);
  123. return array(
  124. Cache::STATS_HITS => null,
  125. Cache::STATS_MISSES => null,
  126. Cache::STATS_UPTIME => null,
  127. Cache::STATS_MEMORY_USAGE => $usage,
  128. Cache::STATS_MEMORY_AVAILABLE => $free,
  129. );
  130. }
  131. /**
  132. * @return \Iterator
  133. */
  134. private function getIterator()
  135. {
  136. $pattern = '/^.+\\' . $this->extension . '$/i';
  137. $iterator = new \RecursiveDirectoryIterator($this->directory);
  138. $iterator = new \RecursiveIteratorIterator($iterator);
  139. return new \RegexIterator($iterator, $pattern);
  140. }
  141. }