1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Config;
13:
14: use Symfony\Component\Config\Resource\ResourceInterface;
15:
16: 17: 18: 19: 20: 21: 22: 23:
24: class ConfigCache
25: {
26: private $debug;
27: private $file;
28:
29: 30: 31: 32: 33: 34:
35: public function __construct($file, $debug)
36: {
37: $this->file = $file;
38: $this->debug = (Boolean) $debug;
39: }
40:
41: 42: 43: 44: 45:
46: public function __toString()
47: {
48: return $this->file;
49: }
50:
51: 52: 53: 54: 55: 56: 57: 58:
59: public function isFresh()
60: {
61: if (!is_file($this->file)) {
62: return false;
63: }
64:
65: if (!$this->debug) {
66: return true;
67: }
68:
69: $metadata = $this->file.'.meta';
70: if (!is_file($metadata)) {
71: return false;
72: }
73:
74: $time = filemtime($this->file);
75: $meta = unserialize(file_get_contents($metadata));
76: foreach ($meta as $resource) {
77: if (!$resource->isFresh($time)) {
78: return false;
79: }
80: }
81:
82: return true;
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92:
93: public function write($content, array $metadata = null)
94: {
95: $dir = dirname($this->file);
96: if (!is_dir($dir)) {
97: if (false === @mkdir($dir, 0777, true)) {
98: throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
99: }
100: } elseif (!is_writable($dir)) {
101: throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
102: }
103:
104: $tmpFile = tempnam($dir, basename($this->file));
105: if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
106: @chmod($this->file, 0666 & ~umask());
107: } else {
108: throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
109: }
110:
111: if (null !== $metadata && true === $this->debug) {
112: $file = $this->file.'.meta';
113: $tmpFile = tempnam($dir, basename($file));
114: if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
115: @chmod($file, 0666 & ~umask());
116: }
117: }
118: }
119: }
120: