ConfigCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\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@symfony.com>
  18. */
  19. class ConfigCache
  20. {
  21. private $debug;
  22. private $file;
  23. /**
  24. * Constructor.
  25. *
  26. * @param string $file The absolute cache path
  27. * @param Boolean $debug Whether debugging is enabled or not
  28. */
  29. public function __construct($file, $debug)
  30. {
  31. $this->file = $file;
  32. $this->debug = (Boolean) $debug;
  33. }
  34. /**
  35. * Gets the cache file path.
  36. *
  37. * @return string The cache file path
  38. */
  39. public function __toString()
  40. {
  41. return $this->file;
  42. }
  43. /**
  44. * Checks if the cache is still fresh.
  45. *
  46. * This method always returns true is debug is on and the cache file exists.
  47. *
  48. * @return Boolean true if the cache is fresh, false otherwise
  49. */
  50. public function isFresh()
  51. {
  52. if (!file_exists($this->file)) {
  53. return false;
  54. }
  55. if (!$this->debug) {
  56. return true;
  57. }
  58. $metadata = $this->file.'.meta';
  59. if (!file_exists($metadata)) {
  60. return false;
  61. }
  62. $time = filemtime($this->file);
  63. $meta = unserialize(file_get_contents($metadata));
  64. foreach ($meta as $resource) {
  65. if (!$resource->isFresh($time)) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * Writes cache.
  73. *
  74. * @param string $content The content to write in the cache
  75. * @param array $metadata An array of ResourceInterface instances
  76. *
  77. * @throws \RuntimeException When cache file can't be wrote
  78. */
  79. public function write($content, array $metadata = null)
  80. {
  81. $dir = dirname($this->file);
  82. if (!is_dir($dir)) {
  83. if (false === @mkdir($dir, 0777, true)) {
  84. throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
  85. }
  86. } elseif (!is_writable($dir)) {
  87. throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
  88. }
  89. $tmpFile = tempnam(dirname($this->file), basename($this->file));
  90. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
  91. chmod($this->file, 0666);
  92. } else {
  93. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
  94. }
  95. if (null !== $metadata && true === $this->debug) {
  96. $file = $this->file.'.meta';
  97. $tmpFile = tempnam(dirname($file), basename($file));
  98. if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
  99. chmod($file, 0666);
  100. }
  101. }
  102. }
  103. }