ConfigCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 when debug is off and the
  47. * cache file exists.
  48. *
  49. * @return Boolean true if the cache is fresh, false otherwise
  50. */
  51. public function isFresh()
  52. {
  53. if (!file_exists($this->file)) {
  54. return false;
  55. }
  56. if (!$this->debug) {
  57. return true;
  58. }
  59. $metadata = $this->file.'.meta';
  60. if (!file_exists($metadata)) {
  61. return false;
  62. }
  63. $time = filemtime($this->file);
  64. $meta = unserialize(file_get_contents($metadata));
  65. foreach ($meta as $resource) {
  66. if (!$resource->isFresh($time)) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. /**
  73. * Writes cache.
  74. *
  75. * @param string $content The content to write in the cache
  76. * @param array $metadata An array of ResourceInterface instances
  77. *
  78. * @throws \RuntimeException When cache file can't be wrote
  79. */
  80. public function write($content, array $metadata = null)
  81. {
  82. $dir = dirname($this->file);
  83. if (!is_dir($dir)) {
  84. if (false === @mkdir($dir, 0777, true)) {
  85. throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
  86. }
  87. } elseif (!is_writable($dir)) {
  88. throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
  89. }
  90. $tmpFile = tempnam(dirname($this->file), basename($this->file));
  91. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
  92. chmod($this->file, 0666);
  93. } else {
  94. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
  95. }
  96. if (null !== $metadata && true === $this->debug) {
  97. $file = $this->file.'.meta';
  98. $tmpFile = tempnam(dirname($file), basename($file));
  99. if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
  100. chmod($file, 0666);
  101. }
  102. }
  103. }
  104. }