ConfigCache.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. use Symfony\Component\Config\Resource\ResourceInterface;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. /**
  14. * ConfigCache manages PHP cache files.
  15. *
  16. * When debug is enabled, it knows when to flush the cache
  17. * thanks to an array of ResourceInterface instances.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class ConfigCache
  22. {
  23. private $debug;
  24. private $file;
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $file The absolute cache path
  29. * @param Boolean $debug Whether debugging is enabled or not
  30. */
  31. public function __construct($file, $debug)
  32. {
  33. $this->file = $file;
  34. $this->debug = (Boolean) $debug;
  35. }
  36. /**
  37. * Gets the cache file path.
  38. *
  39. * @return string The cache file path
  40. */
  41. public function __toString()
  42. {
  43. return $this->file;
  44. }
  45. /**
  46. * Checks if the cache is still fresh.
  47. *
  48. * This method always returns true when debug is off and the
  49. * cache file exists.
  50. *
  51. * @return Boolean true if the cache is fresh, false otherwise
  52. */
  53. public function isFresh()
  54. {
  55. if (!is_file($this->file)) {
  56. return false;
  57. }
  58. if (!$this->debug) {
  59. return true;
  60. }
  61. $metadata = $this->getMetaFile();
  62. if (!is_file($metadata)) {
  63. return false;
  64. }
  65. $time = filemtime($this->file);
  66. $meta = unserialize(file_get_contents($metadata));
  67. foreach ($meta as $resource) {
  68. if (!$resource->isFresh($time)) {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. /**
  75. * Writes cache.
  76. *
  77. * @param string $content The content to write in the cache
  78. * @param ResourceInterface[] $metadata An array of ResourceInterface instances
  79. *
  80. * @throws \RuntimeException When cache file can't be wrote
  81. */
  82. public function write($content, array $metadata = null)
  83. {
  84. $mode = 0666 & ~umask();
  85. $filesystem = new Filesystem();
  86. $filesystem->dumpFile($this->file, $content, $mode);
  87. if (null !== $metadata && true === $this->debug) {
  88. $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), $mode);
  89. }
  90. }
  91. /**
  92. * Gets the meta file path.
  93. *
  94. * @return string The meta file path
  95. */
  96. private function getMetaFile()
  97. {
  98. return $this->file.'.meta';
  99. }
  100. }