Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / cache / lib / Doctrine / Common / Cache / PhpFileCache.php
index 5e7519674e3e50414772f5bd3473870f53595d12..d243ab6c74b5de064c37d215ff07c2c5126ba807 100644 (file)
@@ -29,12 +29,22 @@ class PhpFileCache extends FileCache
 {
     const EXTENSION = '.doctrinecache.php';
 
+    /**
+     * @var callable
+     *
+     * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
+     */
+    private static $emptyErrorHandler;
+
     /**
      * {@inheritdoc}
      */
     public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
     {
         parent::__construct($directory, $extension, $umask);
+
+        self::$emptyErrorHandler = function () {
+        };
     }
 
     /**
@@ -44,7 +54,7 @@ class PhpFileCache extends FileCache
     {
         $value = $this->includeFileForId($id);
 
-        if (! $value) {
+        if ($value === null) {
             return false;
         }
 
@@ -62,7 +72,7 @@ class PhpFileCache extends FileCache
     {
         $value = $this->includeFileForId($id);
 
-        if (! $value) {
+        if ($value === null) {
             return false;
         }
 
@@ -78,23 +88,20 @@ class PhpFileCache extends FileCache
             $lifeTime = time() + $lifeTime;
         }
 
-        if (is_object($data) && ! method_exists($data, '__set_state')) {
-            throw new \InvalidArgumentException(
-                "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " .
-                "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " .
-                "graphs using serialize()/deserialize()."
-            );
-        }
-
         $filename  = $this->getFilename($id);
 
-        $value = array(
+        $value = [
             'lifetime'  => $lifeTime,
             'data'      => $data
-        );
-
-        $value  = var_export($value, true);
-        $code   = sprintf('<?php return %s;', $value);
+        ];
+
+        if (is_object($data) && method_exists($data, '__set_state')) {
+            $value  = var_export($value, true);
+            $code   = sprintf('<?php return %s;', $value);
+        } else {
+            $value  = var_export(serialize($value), true);
+            $code   = sprintf('<?php return unserialize(%s);', $value);
+        }
 
         return $this->writeFile($filename, $code);
     }
@@ -102,17 +109,21 @@ class PhpFileCache extends FileCache
     /**
      * @param string $id
      *
-     * @return array|false
+     * @return array|null
      */
-    private function includeFileForId($id)
+    private function includeFileForId(string $id) : ?array
     {
         $fileName = $this->getFilename($id);
 
         // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
-        $value = @include $fileName;
+        set_error_handler(self::$emptyErrorHandler);
+
+        $value = include $fileName;
+
+        restore_error_handler();
 
         if (! isset($value['lifetime'])) {
-            return false;
+            return null;
         }
 
         return $value;