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 iterable|ResourceCheckerInterface[]
34 private $resourceCheckers;
37 * @param string $file The absolute cache path
38 * @param iterable|ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
40 public function __construct($file, $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 instanceof \Traversable && !$this->resourceCheckers instanceof \Countable) {
72 $this->resourceCheckers = iterator_to_array($this->resourceCheckers);
75 if (!count($this->resourceCheckers)) {
76 return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all
79 $metadata = $this->getMetaFile();
81 if (!is_file($metadata)) {
85 $meta = $this->safelyUnserialize($metadata);
87 if (false === $meta) {
91 $time = filemtime($this->file);
93 foreach ($meta as $resource) {
94 /* @var ResourceInterface $resource */
95 foreach ($this->resourceCheckers as $checker) {
96 if (!$checker->supports($resource)) {
97 continue; // next checker
99 if ($checker->isFresh($resource, $time)) {
100 break; // no need to further check this resource
103 return false; // cache is stale
105 // no suitable checker found, ignore this resource
114 * @param string $content The content to write in the cache
115 * @param ResourceInterface[] $metadata An array of metadata
117 * @throws \RuntimeException When cache file can't be written
119 public function write($content, array $metadata = null)
123 $filesystem = new Filesystem();
124 $filesystem->dumpFile($this->file, $content);
126 $filesystem->chmod($this->file, $mode, $umask);
127 } catch (IOException $e) {
128 // discard chmod failure (some filesystem may not support it)
131 if (null !== $metadata) {
132 $filesystem->dumpFile($this->getMetaFile(), serialize($metadata));
134 $filesystem->chmod($this->getMetaFile(), $mode, $umask);
135 } catch (IOException $e) {
136 // discard chmod failure (some filesystem may not support it)
140 if (\function_exists('opcache_invalidate') && ini_get('opcache.enable')) {
141 @opcache_invalidate($this->file, true);
146 * Gets the meta file path.
148 * @return string The meta file path
150 private function getMetaFile()
152 return $this->file.'.meta';
155 private function safelyUnserialize($file)
159 $signalingException = new \UnexpectedValueException();
160 $prevUnserializeHandler = ini_set('unserialize_callback_func', '');
161 $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$prevErrorHandler, $signalingException) {
162 if (E_WARNING === $type && 'Class __PHP_Incomplete_Class has no unserializer' === $msg) {
163 throw $signalingException;
166 return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
170 $meta = unserialize(file_get_contents($file));
171 } catch (\Error $e) {
172 } catch (\Exception $e) {
174 restore_error_handler();
175 ini_set('unserialize_callback_func', $prevUnserializeHandler);
176 if (null !== $e && $e !== $signalingException) {