Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\DataCollector; Chris@0: Chris@0: use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter; Chris@14: use Symfony\Component\VarDumper\Caster\CutStub; Chris@0: use Symfony\Component\VarDumper\Cloner\ClonerInterface; Chris@0: use Symfony\Component\VarDumper\Cloner\Data; Chris@0: use Symfony\Component\VarDumper\Cloner\Stub; Chris@0: use Symfony\Component\VarDumper\Cloner\VarCloner; Chris@0: Chris@0: /** Chris@0: * DataCollector. Chris@0: * Chris@0: * Children of this class must store the collected data in the data property. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: abstract class DataCollector implements DataCollectorInterface, \Serializable Chris@0: { Chris@17: protected $data = []; Chris@0: Chris@0: /** Chris@0: * @var ValueExporter Chris@0: */ Chris@0: private $valueExporter; Chris@0: Chris@0: /** Chris@0: * @var ClonerInterface Chris@0: */ Chris@0: private $cloner; Chris@0: Chris@0: public function serialize() Chris@0: { Chris@17: $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); Chris@17: $isCalledFromOverridingMethod = isset($trace[1]['function'], $trace[1]['object']) && 'serialize' === $trace[1]['function'] && $this === $trace[1]['object']; Chris@17: Chris@17: return $isCalledFromOverridingMethod ? $this->data : serialize($this->data); Chris@0: } Chris@0: Chris@0: public function unserialize($data) Chris@0: { Chris@17: $this->data = \is_array($data) ? $data : unserialize($data); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts the variable into a serializable Data instance. Chris@0: * Chris@0: * This array can be displayed in the template using Chris@0: * the VarDumper component. Chris@0: * Chris@0: * @param mixed $var Chris@0: * Chris@0: * @return Data Chris@0: */ Chris@0: protected function cloneVar($var) Chris@0: { Chris@14: if ($var instanceof Data) { Chris@14: return $var; Chris@14: } Chris@0: if (null === $this->cloner) { Chris@14: if (class_exists(CutStub::class)) { Chris@0: $this->cloner = new VarCloner(); Chris@14: $this->cloner->setMaxItems(-1); Chris@14: $this->cloner->addCasters($this->getCasters()); Chris@0: } else { Chris@14: @trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since Symfony 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED); Chris@0: $this->cloner = false; Chris@0: } Chris@0: } Chris@0: if (false === $this->cloner) { Chris@0: if (null === $this->valueExporter) { Chris@0: $this->valueExporter = new ValueExporter(); Chris@0: } Chris@0: Chris@0: return $this->valueExporter->exportValue($var); Chris@0: } Chris@0: Chris@14: return $this->cloner->cloneVar($var); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts a PHP variable to a string. Chris@0: * Chris@0: * @param mixed $var A PHP variable Chris@0: * Chris@0: * @return string The string representation of the variable Chris@0: * Chris@14: * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead. Chris@0: */ Chris@0: protected function varToString($var) Chris@0: { Chris@14: @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.2 and will be removed in 4.0. Use cloneVar() instead.', __METHOD__), E_USER_DEPRECATED); Chris@0: Chris@0: if (null === $this->valueExporter) { Chris@0: $this->valueExporter = new ValueExporter(); Chris@0: } Chris@0: Chris@0: return $this->valueExporter->exportValue($var); Chris@0: } Chris@0: Chris@14: /** Chris@14: * @return callable[] The casters to add to the cloner Chris@14: */ Chris@14: protected function getCasters() Chris@0: { Chris@17: return [ Chris@14: '*' => function ($v, array $a, Stub $s, $isNested) { Chris@14: if (!$v instanceof Stub) { Chris@14: foreach ($a as $k => $v) { Chris@17: if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) { Chris@14: $a[$k] = new CutStub($v); Chris@14: } Chris@14: } Chris@0: } Chris@0: Chris@14: return $a; Chris@14: }, Chris@17: ]; Chris@0: } Chris@0: }