ConfigCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Config;
  11. /**
  12. * ConfigCache manages PHP cache files.
  13. *
  14. * When debug is enabled, it knows when to flush the cache
  15. * thanks to an array of ResourceInterface instances.
  16. *
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class ConfigCache
  20. {
  21. protected $debug;
  22. protected $cacheDir;
  23. protected $file;
  24. /**
  25. * Constructor.
  26. *
  27. * @param string $cacheDir The cache directory
  28. * @param string $file The cache file name (without the .php extension)
  29. * @param Boolean $debug Whether debugging is enabled or not
  30. */
  31. public function __construct($cacheDir, $file, $debug)
  32. {
  33. $this->cacheDir = $cacheDir;
  34. $this->file = $file;
  35. $this->debug = (Boolean) $debug;
  36. }
  37. /**
  38. * Gets the cache file path.
  39. *
  40. * @return string The cache file path
  41. */
  42. public function __toString()
  43. {
  44. return $this->getCacheFile();
  45. }
  46. /**
  47. * Checks if the cache is still fresh.
  48. *
  49. * This method always returns true is debug is on and the cache file exists.
  50. *
  51. * @return Boolean true if the cache is fresh, false otherwise
  52. */
  53. public function isFresh()
  54. {
  55. $file = $this->getCacheFile();
  56. if (!file_exists($file)) {
  57. return false;
  58. }
  59. if (!$this->debug) {
  60. return true;
  61. }
  62. $metadata = $this->getCacheFile('meta');
  63. if (!file_exists($metadata)) {
  64. return false;
  65. }
  66. $time = filemtime($file);
  67. $meta = unserialize(file_get_contents($metadata));
  68. foreach ($meta as $resource) {
  69. if (!$resource->isFresh($time)) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. /**
  76. * Writes cache.
  77. *
  78. * @param string $content The content to write in the cache
  79. * @param array $metadata An array of ResourceInterface instances
  80. *
  81. * @throws \RuntimeException When cache file can't be wrote
  82. */
  83. public function write($content, array $metadata = null)
  84. {
  85. $file = $this->getCacheFile();
  86. $dir = dirname($file);
  87. if (!is_dir($dir)) {
  88. if (false === @mkdir($dir, 0777, true)) {
  89. throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
  90. }
  91. } elseif (!is_writable($dir)) {
  92. throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
  93. }
  94. $tmpFile = tempnam(dirname($file), basename($file));
  95. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
  96. chmod($file, 0666);
  97. } else {
  98. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
  99. }
  100. if (null !== $metadata && true === $this->debug) {
  101. $file = $this->getCacheFile('meta');
  102. $tmpFile = tempnam(dirname($file), basename($file));
  103. if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
  104. chmod($file, 0666);
  105. }
  106. }
  107. }
  108. protected function getCacheFile($extension = 'php')
  109. {
  110. return $this->cacheDir.'/'.$this->file.'.'.$extension;
  111. }
  112. }