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