Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / http-kernel / DataCollector / DataCollector.php
index 5dca65298d059d0c64438759953423fbdf6e75ba..3808852dfaafb6e650ab6d3c56aa89529ef2d0f6 100644 (file)
 namespace Symfony\Component\HttpKernel\DataCollector;
 
 use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
+use Symfony\Component\VarDumper\Caster\ClassStub;
+use Symfony\Component\VarDumper\Caster\LinkStub;
+use Symfony\Component\VarDumper\Caster\StubCaster;
+use Symfony\Component\VarDumper\Cloner\ClonerInterface;
+use Symfony\Component\VarDumper\Cloner\Data;
+use Symfony\Component\VarDumper\Cloner\Stub;
+use Symfony\Component\VarDumper\Cloner\VarCloner;
 
 /**
  * DataCollector.
@@ -30,6 +37,13 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable
      */
     private $valueExporter;
 
+    /**
+     * @var ClonerInterface
+     */
+    private $cloner;
+
+    private static $stubsCache = array();
+
     public function serialize()
     {
         return serialize($this->data);
@@ -40,19 +54,92 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable
         $this->data = unserialize($data);
     }
 
+    /**
+     * Converts the variable into a serializable Data instance.
+     *
+     * This array can be displayed in the template using
+     * the VarDumper component.
+     *
+     * @param mixed $var
+     *
+     * @return Data
+     */
+    protected function cloneVar($var)
+    {
+        if (null === $this->cloner) {
+            if (class_exists(ClassStub::class)) {
+                $this->cloner = new VarCloner();
+                $this->cloner->setMaxItems(250);
+                $this->cloner->addCasters(array(
+                    Stub::class => function (Stub $v, array $a, Stub $s, $isNested) {
+                        return $isNested ? $a : StubCaster::castStub($v, $a, $s, true);
+                    },
+                ));
+            } else {
+                @trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
+                $this->cloner = false;
+            }
+        }
+        if (false === $this->cloner) {
+            if (null === $this->valueExporter) {
+                $this->valueExporter = new ValueExporter();
+            }
+
+            return $this->valueExporter->exportValue($var);
+        }
+
+        return $this->cloner->cloneVar($this->decorateVar($var));
+    }
+
     /**
      * Converts a PHP variable to a string.
      *
      * @param mixed $var A PHP variable
      *
      * @return string The string representation of the variable
+     *
+     * @deprecated Deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
      */
     protected function varToString($var)
     {
+        @trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use cloneVar() instead.', __METHOD__), E_USER_DEPRECATED);
+
         if (null === $this->valueExporter) {
             $this->valueExporter = new ValueExporter();
         }
 
         return $this->valueExporter->exportValue($var);
     }
+
+    private function decorateVar($var)
+    {
+        if (is_array($var)) {
+            if (isset($var[0], $var[1]) && is_callable($var)) {
+                return ClassStub::wrapCallable($var);
+            }
+            foreach ($var as $k => $v) {
+                if ($v !== $d = $this->decorateVar($v)) {
+                    $var[$k] = $d;
+                }
+            }
+
+            return $var;
+        }
+        if (is_string($var)) {
+            if (isset(self::$stubsCache[$var])) {
+                return self::$stubsCache[$var];
+            }
+            if (false !== strpos($var, '\\')) {
+                $c = (false !== $i = strpos($var, '::')) ? substr($var, 0, $i) : $var;
+                if (class_exists($c, false) || interface_exists($c, false) || trait_exists($c, false)) {
+                    return self::$stubsCache[$var] = new ClassStub($var);
+                }
+            }
+            if (false !== strpos($var, DIRECTORY_SEPARATOR) && false === strpos($var, '://') && false === strpos($var, "\0") && @is_file($var)) {
+                return self::$stubsCache[$var] = new LinkStub($var);
+            }
+        }
+
+        return $var;
+    }
 }