3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Cache;
23 * Php file cache driver.
26 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
28 class PhpFileCache extends FileCache
30 const EXTENSION = '.doctrinecache.php';
35 * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
37 private static $emptyErrorHandler;
42 public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
44 parent::__construct($directory, $extension, $umask);
46 self::$emptyErrorHandler = function () {
53 protected function doFetch($id)
55 $value = $this->includeFileForId($id);
57 if ($value === null) {
61 if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
65 return $value['data'];
71 protected function doContains($id)
73 $value = $this->includeFileForId($id);
75 if ($value === null) {
79 return $value['lifetime'] === 0 || $value['lifetime'] > time();
85 protected function doSave($id, $data, $lifeTime = 0)
88 $lifeTime = time() + $lifeTime;
91 $filename = $this->getFilename($id);
94 'lifetime' => $lifeTime,
98 if (is_object($data) && method_exists($data, '__set_state')) {
99 $value = var_export($value, true);
100 $code = sprintf('<?php return %s;', $value);
102 $value = var_export(serialize($value), true);
103 $code = sprintf('<?php return unserialize(%s);', $value);
106 return $this->writeFile($filename, $code);
114 private function includeFileForId(string $id) : ?array
116 $fileName = $this->getFilename($id);
118 // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
119 set_error_handler(self::$emptyErrorHandler);
121 $value = include $fileName;
123 restore_error_handler();
125 if (! isset($value['lifetime'])) {