FileCache.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * @var string Cache directory.
  30. */
  31. protected $directory;
  32. /**
  33. * @var string Cache file extension.
  34. */
  35. protected $extension;
  36. /**
  37. * Constructor
  38. *
  39. * @param string $directory Cache directory.
  40. * @param string $directory Cache file extension.
  41. *
  42. * @throws \InvalidArgumentException
  43. */
  44. public function __construct($directory, $extension = null)
  45. {
  46. if ( ! is_dir($directory) && ! @mkdir($directory, 0777, true)) {
  47. throw new \InvalidArgumentException(sprintf(
  48. 'The directory "%s" does not exist and could not be created.',
  49. $directory
  50. ));
  51. }
  52. if ( ! is_writable($directory)) {
  53. throw new \InvalidArgumentException(sprintf(
  54. 'The directory "%s" is not writable.',
  55. $directory
  56. ));
  57. }
  58. $this->directory = realpath($directory);
  59. $this->extension = $extension ?: $this->extension;
  60. }
  61. /**
  62. * Gets the cache directory.
  63. *
  64. * @return string
  65. */
  66. public function getDirectory()
  67. {
  68. return $this->directory;
  69. }
  70. /**
  71. * Gets the cache file extension.
  72. *
  73. * @return string
  74. */
  75. public function getExtension()
  76. {
  77. return $this->extension;
  78. }
  79. /**
  80. * @return string
  81. */
  82. protected function getFilename($id)
  83. {
  84. $path = implode(str_split(md5($id), 12), DIRECTORY_SEPARATOR);
  85. $path = $this->directory . DIRECTORY_SEPARATOR . $path;
  86. return $path . DIRECTORY_SEPARATOR . $id . $this->extension;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. protected function doDelete($id)
  92. {
  93. return @unlink($this->getFilename($id));
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. protected function doFlush()
  99. {
  100. $pattern = '/^.+\\' . $this->extension . '$/i';
  101. $iterator = new \RecursiveDirectoryIterator($this->directory);
  102. $iterator = new \RecursiveIteratorIterator($iterator);
  103. $iterator = new \RegexIterator($iterator, $pattern);
  104. foreach ($iterator as $name => $file) {
  105. @unlink($name);
  106. }
  107. return true;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. protected function doGetStats()
  113. {
  114. return null;
  115. }
  116. }