Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / cache / lib / Doctrine / Common / Cache / PhpFileCache.php
1 <?php
2 /*
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.
14  *
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>.
18  */
19
20 namespace Doctrine\Common\Cache;
21
22 /**
23  * Php file cache driver.
24  *
25  * @since  2.3
26  * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
27  */
28 class PhpFileCache extends FileCache
29 {
30     const EXTENSION = '.doctrinecache.php';
31
32     /**
33      * @var callable
34      *
35      * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
36      */
37     private static $emptyErrorHandler;
38
39     /**
40      * {@inheritdoc}
41      */
42     public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
43     {
44         parent::__construct($directory, $extension, $umask);
45
46         self::$emptyErrorHandler = function () {
47         };
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     protected function doFetch($id)
54     {
55         $value = $this->includeFileForId($id);
56
57         if ($value === null) {
58             return false;
59         }
60
61         if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
62             return false;
63         }
64
65         return $value['data'];
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     protected function doContains($id)
72     {
73         $value = $this->includeFileForId($id);
74
75         if ($value === null) {
76             return false;
77         }
78
79         return $value['lifetime'] === 0 || $value['lifetime'] > time();
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     protected function doSave($id, $data, $lifeTime = 0)
86     {
87         if ($lifeTime > 0) {
88             $lifeTime = time() + $lifeTime;
89         }
90
91         $filename  = $this->getFilename($id);
92
93         $value = [
94             'lifetime'  => $lifeTime,
95             'data'      => $data
96         ];
97
98         if (is_object($data) && method_exists($data, '__set_state')) {
99             $value  = var_export($value, true);
100             $code   = sprintf('<?php return %s;', $value);
101         } else {
102             $value  = var_export(serialize($value), true);
103             $code   = sprintf('<?php return unserialize(%s);', $value);
104         }
105
106         return $this->writeFile($filename, $code);
107     }
108
109     /**
110      * @param string $id
111      *
112      * @return array|null
113      */
114     private function includeFileForId(string $id) : ?array
115     {
116         $fileName = $this->getFilename($id);
117
118         // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
119         set_error_handler(self::$emptyErrorHandler);
120
121         $value = include $fileName;
122
123         restore_error_handler();
124
125         if (! isset($value['lifetime'])) {
126             return null;
127         }
128
129         return $value;
130     }
131 }