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@0: use Symfony\Component\VarDumper\Caster\ClassStub; Chris@0: use Symfony\Component\VarDumper\Caster\LinkStub; Chris@0: use Symfony\Component\VarDumper\Caster\StubCaster; 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@0: protected $data = array(); 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: private static $stubsCache = array(); Chris@0: Chris@0: public function serialize() Chris@0: { Chris@0: return serialize($this->data); Chris@0: } Chris@0: Chris@0: public function unserialize($data) Chris@0: { Chris@0: $this->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@0: if (null === $this->cloner) { Chris@0: if (class_exists(ClassStub::class)) { Chris@0: $this->cloner = new VarCloner(); Chris@0: $this->cloner->setMaxItems(250); Chris@0: $this->cloner->addCasters(array( Chris@0: Stub::class => function (Stub $v, array $a, Stub $s, $isNested) { Chris@0: return $isNested ? $a : StubCaster::castStub($v, $a, $s, true); Chris@0: }, Chris@0: )); Chris@0: } else { Chris@0: @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); 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@0: return $this->cloner->cloneVar($this->decorateVar($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@0: * @deprecated 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@0: @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); 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@0: private function decorateVar($var) Chris@0: { Chris@0: if (is_array($var)) { Chris@0: if (isset($var[0], $var[1]) && is_callable($var)) { Chris@0: return ClassStub::wrapCallable($var); Chris@0: } Chris@0: foreach ($var as $k => $v) { Chris@0: if ($v !== $d = $this->decorateVar($v)) { Chris@0: $var[$k] = $d; Chris@0: } Chris@0: } Chris@0: Chris@0: return $var; Chris@0: } Chris@0: if (is_string($var)) { Chris@0: if (isset(self::$stubsCache[$var])) { Chris@0: return self::$stubsCache[$var]; Chris@0: } Chris@0: if (false !== strpos($var, '\\')) { Chris@0: $c = (false !== $i = strpos($var, '::')) ? substr($var, 0, $i) : $var; Chris@0: if (class_exists($c, false) || interface_exists($c, false) || trait_exists($c, false)) { Chris@0: return self::$stubsCache[$var] = new ClassStub($var); Chris@0: } Chris@0: } Chris@0: if (false !== strpos($var, DIRECTORY_SEPARATOR) && false === strpos($var, '://') && false === strpos($var, "\0") && @is_file($var)) { Chris@0: return self::$stubsCache[$var] = new LinkStub($var); Chris@0: } Chris@0: } Chris@0: Chris@0: return $var; Chris@0: } Chris@0: }