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
12namespace Symfony\Component\Config;
13
14use Symfony\Component\Config\Resource\ResourceInterface;
15use Symfony\Component\Filesystem\Exception\IOException;
16use Symfony\Component\Filesystem\Filesystem;
17
18/**
19 * ResourceCheckerConfigCache uses instances of ResourceCheckerInterface
20 * to check whether cached data is still fresh.
21 *
22 * @author Matthias Pigulla <mp@webfactory.de>
23 */
24class ResourceCheckerConfigCache implements ConfigCacheInterface
25{
26    /**
27     * @var string
28     */
29    private $file;
30
31    /**
32     * @var iterable|ResourceCheckerInterface[]
33     */
34    private $resourceCheckers;
35
36    /**
37     * @param string                              $file             The absolute cache path
38     * @param iterable|ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
39     */
40    public function __construct($file, $resourceCheckers = [])
41    {
42        $this->file = $file;
43        $this->resourceCheckers = $resourceCheckers;
44    }
45
46    /**
47     * {@inheritdoc}
48     */
49    public function getPath()
50    {
51        return $this->file;
52    }
53
54    /**
55     * Checks if the cache is still fresh.
56     *
57     * This implementation will make a decision solely based on the ResourceCheckers
58     * passed in the constructor.
59     *
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.
62     *
63     * @return bool true if the cache is fresh, false otherwise
64     */
65    public function isFresh()
66    {
67        if (!is_file($this->file)) {
68            return false;
69        }
70
71        if ($this->resourceCheckers instanceof \Traversable && !$this->resourceCheckers instanceof \Countable) {
72            $this->resourceCheckers = iterator_to_array($this->resourceCheckers);
73        }
74
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
77        }
78
79        $metadata = $this->getMetaFile();
80
81        if (!is_file($metadata)) {
82            return false;
83        }
84
85        $meta = $this->safelyUnserialize($metadata);
86
87        if (false === $meta) {
88            return false;
89        }
90
91        $time = filemtime($this->file);
92
93        foreach ($meta as $resource) {
94            /* @var ResourceInterface $resource */
95            foreach ($this->resourceCheckers as $checker) {
96                if (!$checker->supports($resource)) {
97                    continue; // next checker
98                }
99                if ($checker->isFresh($resource, $time)) {
100                    break; // no need to further check this resource
101                }
102
103                return false; // cache is stale
104            }
105            // no suitable checker found, ignore this resource
106        }
107
108        return true;
109    }
110
111    /**
112     * Writes cache.
113     *
114     * @param string              $content  The content to write in the cache
115     * @param ResourceInterface[] $metadata An array of metadata
116     *
117     * @throws \RuntimeException When cache file can't be written
118     */
119    public function write($content, array $metadata = null)
120    {
121        $mode = 0666;
122        $umask = umask();
123        $filesystem = new Filesystem();
124        $filesystem->dumpFile($this->file, $content);
125        try {
126            $filesystem->chmod($this->file, $mode, $umask);
127        } catch (IOException $e) {
128            // discard chmod failure (some filesystem may not support it)
129        }
130
131        if (null !== $metadata) {
132            $filesystem->dumpFile($this->getMetaFile(), serialize($metadata));
133            try {
134                $filesystem->chmod($this->getMetaFile(), $mode, $umask);
135            } catch (IOException $e) {
136                // discard chmod failure (some filesystem may not support it)
137            }
138        }
139
140        if (\function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
141            @opcache_invalidate($this->file, true);
142        }
143    }
144
145    /**
146     * Gets the meta file path.
147     *
148     * @return string The meta file path
149     */
150    private function getMetaFile()
151    {
152        return $this->file.'.meta';
153    }
154
155    private function safelyUnserialize($file)
156    {
157        $e = null;
158        $meta = false;
159        $content = file_get_contents($file);
160        $signalingException = new \UnexpectedValueException();
161        $prevUnserializeHandler = ini_set('unserialize_callback_func', '');
162        $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {
163            if (__FILE__ === $file) {
164                throw $signalingException;
165            }
166
167            return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
168        });
169
170        try {
171            $meta = unserialize($content);
172        } catch (\Error $e) {
173        } catch (\Exception $e) {
174        }
175        restore_error_handler();
176        ini_set('unserialize_callback_func', $prevUnserializeHandler);
177        if (null !== $e && $e !== $signalingException) {
178            throw $e;
179        }
180
181        return $meta;
182    }
183}
184