Beispiel #1
0
    /**
     * 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));
    }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function dump(\Twig_Environment $env, $context)
 {
     // Return if 'debug' is `false` in Twig, or there's no logged on user _and_ `debug_show_loggedoff` in
     // config.yml is `false`.
     if (!$env->isDebug() || $this->users->getCurrentUser() === null && !$this->debugShowLoggedoff) {
         return null;
     }
     if (func_num_args() === 2) {
         $vars = [];
         foreach ($context as $key => $value) {
             if (!$value instanceof \Twig_Template) {
                 $vars[$key] = $value;
             }
         }
         $vars = [$vars];
     } else {
         $vars = func_get_args();
         unset($vars[0], $vars[1]);
     }
     $output = fopen('php://memory', 'r+b');
     $prevOutput = $this->dumper->setOutput($output);
     foreach ($vars as $value) {
         $this->dumper->dump($this->cloner->cloneVar($value));
     }
     $this->dumper->setOutput($prevOutput);
     rewind($output);
     return stream_get_contents($output);
 }
Beispiel #3
0
 /**
  * {@inheritdoc}
  */
 public function dump(\Twig_Environment $env, $context)
 {
     if (!$env->isDebug()) {
         return null;
     }
     if (func_num_args() === 2) {
         $vars = [];
         foreach ($context as $key => $value) {
             if (!$value instanceof \Twig_Template) {
                 $vars[$key] = $value;
             }
         }
         $vars = [$vars];
     } else {
         $vars = func_get_args();
         unset($vars[0], $vars[1]);
     }
     $output = fopen('php://memory', 'r+b');
     $prevOutput = $this->dumper->setOutput($output);
     foreach ($vars as $value) {
         $this->dumper->dump($this->cloner->cloneVar($value));
     }
     $this->dumper->setOutput($prevOutput);
     rewind($output);
     return stream_get_contents($output);
 }
Beispiel #4
0
    /**
     * {@inheritdoc}
     */
    protected function cloneVar($var, $isClass = false)
    {
        if ($var instanceof Data) {
            return $var;
        }
        if (null === $this->cloner) {
            if (class_exists(ClassStub::class)) {
                $this->cloner = new VarCloner();
                $this->cloner->setMaxItems(25);
                $this->cloner->addCasters(array(
                    '*' => function ($v, array $a, Stub $s, $isNested) {
                        if ($isNested && !$v instanceof \DateTimeInterface) {
                            $s->cut = -1;
                            $a = array();
                        }

                        return $a;
                    },
                    \Exception::class => function (\Exception $e, array $a, Stub $s) {
                        if (isset($a[$k = "\0Exception\0previous"])) {
                            unset($a[$k]);
                            ++$s->cut;
                        }

                        return $a;
                    },
                    FormInterface::class => function (FormInterface $f, array $a) {
                        return array(
                            Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
                            Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
                        );
                    },
                    ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
                        return array(
                            Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
                            Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
                            Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
                        );
                    },
                ));
            } 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);
        }
        if (null === $var) {
            $type = $hash = 'null';
        } elseif (array() === $var) {
            $type = $hash = 'array';
        } elseif ('object' === $type = gettype($var)) {
            $hash = spl_object_hash($var);
        } elseif ('double' === $type) {
            $hash = (string) $var;
        } elseif ('integer' === $type || 'string' === $type) {
            $hash = $var;
        } else {
            $type = null;
        }
        if (null !== $type && null !== $cache = &$this->clonerCache[$type][$hash]) {
            return $cache;
        }
        if ($isClass) {
            return $cache = $this->cloner->cloneVar(array(new ClassStub($var)))->seek(0);
        }

        return $cache = $this->cloner->cloneVar($var);
    }