4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config
;
14 use Symfony\Component\Config\
Resource\ResourceInterface
;
15 use Symfony\Component\Filesystem\Exception\IOException
;
16 use Symfony\Component\Filesystem\Filesystem
;
19 * ResourceCheckerConfigCache uses instances of ResourceCheckerInterface
20 * to check whether cached data is still fresh.
22 * @author Matthias Pigulla <mp@webfactory.de>
24 class ResourceCheckerConfigCache
implements ConfigCacheInterface
32 * @var ResourceCheckerInterface[]
34 private $resourceCheckers;
37 * @param string $file The absolute cache path
38 * @param ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
40 public function __construct($file, array $resourceCheckers = array())
43 $this->resourceCheckers
= $resourceCheckers;
49 public function getPath()
55 * Checks if the cache is still fresh.
57 * This implementation will make a decision solely based on the ResourceCheckers
58 * passed in the constructor.
60 * The first ResourceChecker that supports a given resource is considered authoritative.
61 * Resources with no matching ResourceChecker will silently be ignored and considered fresh.
63 * @return bool true if the cache is fresh, false otherwise
65 public function isFresh()
67 if (!is_file($this->file
)) {
71 if (!$this->resourceCheckers
) {
72 return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all
75 $metadata = $this->getMetaFile();
76 if (!is_file($metadata)) {
82 $time = filemtime($this->file
);
83 $signalingException = new \
UnexpectedValueException();
84 $prevUnserializeHandler = ini_set('unserialize_callback_func', '');
85 $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context) use (&$prevErrorHandler, $signalingException) {
86 if (E_WARNING
=== $type && 'Class __PHP_Incomplete_Class has no unserializer' === $msg) {
87 throw $signalingException;
90 return $prevErrorHandler ?
$prevErrorHandler($type, $msg, $file, $line, $context) : false;
94 $meta = unserialize(file_get_contents($metadata));
96 } catch (\Exception
$e) {
98 restore_error_handler();
99 ini_set('unserialize_callback_func', $prevUnserializeHandler);
100 if (null !== $e && $e !== $signalingException) {
103 if (false === $meta) {
107 foreach ($meta as $resource) {
108 /* @var ResourceInterface $resource */
109 foreach ($this->resourceCheckers
as $checker) {
110 if (!$checker->supports($resource)) {
111 continue; // next checker
113 if ($checker->isFresh($resource, $time)) {
114 break; // no need to further check this resource
117 return false; // cache is stale
119 // no suitable checker found, ignore this resource
128 * @param string $content The content to write in the cache
129 * @param ResourceInterface[] $metadata An array of metadata
131 * @throws \RuntimeException When cache file can't be written
133 public function write($content, array $metadata = null)
137 $filesystem = new Filesystem();
138 $filesystem->dumpFile($this->file
, $content, null);
140 $filesystem->chmod($this->file
, $mode, $umask);
141 } catch (IOException
$e) {
142 // discard chmod failure (some filesystem may not support it)
145 if (null !== $metadata) {
146 $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), null);
148 $filesystem->chmod($this->getMetaFile(), $mode, $umask);
149 } catch (IOException
$e) {
150 // discard chmod failure (some filesystem may not support it)
154 if (\function_exists
('opcache_invalidate') && ini_get('opcache.enable')) {
155 @opcache_invalidate
($this->file
, true);
160 * Gets the meta file path.
162 * @return string The meta file path
164 private function getMetaFile()
166 return $this->file
.'.meta';