annotate vendor/symfony/http-kernel/DataCollector/DataCollector.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpKernel\DataCollector;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
Chris@14 15 use Symfony\Component\VarDumper\Caster\CutStub;
Chris@0 16 use Symfony\Component\VarDumper\Cloner\ClonerInterface;
Chris@0 17 use Symfony\Component\VarDumper\Cloner\Data;
Chris@0 18 use Symfony\Component\VarDumper\Cloner\Stub;
Chris@0 19 use Symfony\Component\VarDumper\Cloner\VarCloner;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * DataCollector.
Chris@0 23 *
Chris@0 24 * Children of this class must store the collected data in the data property.
Chris@0 25 *
Chris@0 26 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 27 * @author Bernhard Schussek <bschussek@symfony.com>
Chris@0 28 */
Chris@0 29 abstract class DataCollector implements DataCollectorInterface, \Serializable
Chris@0 30 {
Chris@17 31 protected $data = [];
Chris@0 32
Chris@0 33 /**
Chris@0 34 * @var ValueExporter
Chris@0 35 */
Chris@0 36 private $valueExporter;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * @var ClonerInterface
Chris@0 40 */
Chris@0 41 private $cloner;
Chris@0 42
Chris@17 43 /**
Chris@17 44 * @internal
Chris@17 45 */
Chris@0 46 public function serialize()
Chris@0 47 {
Chris@17 48 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
Chris@17 49 $isCalledFromOverridingMethod = isset($trace[1]['function'], $trace[1]['object']) && 'serialize' === $trace[1]['function'] && $this === $trace[1]['object'];
Chris@17 50
Chris@17 51 return $isCalledFromOverridingMethod ? $this->data : serialize($this->data);
Chris@0 52 }
Chris@0 53
Chris@17 54 /**
Chris@17 55 * @internal
Chris@17 56 */
Chris@0 57 public function unserialize($data)
Chris@0 58 {
Chris@17 59 $this->data = \is_array($data) ? $data : unserialize($data);
Chris@0 60 }
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Converts the variable into a serializable Data instance.
Chris@0 64 *
Chris@0 65 * This array can be displayed in the template using
Chris@0 66 * the VarDumper component.
Chris@0 67 *
Chris@0 68 * @param mixed $var
Chris@0 69 *
Chris@0 70 * @return Data
Chris@0 71 */
Chris@0 72 protected function cloneVar($var)
Chris@0 73 {
Chris@14 74 if ($var instanceof Data) {
Chris@14 75 return $var;
Chris@14 76 }
Chris@0 77 if (null === $this->cloner) {
Chris@14 78 if (class_exists(CutStub::class)) {
Chris@0 79 $this->cloner = new VarCloner();
Chris@14 80 $this->cloner->setMaxItems(-1);
Chris@14 81 $this->cloner->addCasters($this->getCasters());
Chris@0 82 } else {
Chris@14 83 @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 84 $this->cloner = false;
Chris@0 85 }
Chris@0 86 }
Chris@0 87 if (false === $this->cloner) {
Chris@0 88 if (null === $this->valueExporter) {
Chris@0 89 $this->valueExporter = new ValueExporter();
Chris@0 90 }
Chris@0 91
Chris@0 92 return $this->valueExporter->exportValue($var);
Chris@0 93 }
Chris@0 94
Chris@14 95 return $this->cloner->cloneVar($var);
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Converts a PHP variable to a string.
Chris@0 100 *
Chris@0 101 * @param mixed $var A PHP variable
Chris@0 102 *
Chris@0 103 * @return string The string representation of the variable
Chris@0 104 *
Chris@14 105 * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
Chris@0 106 */
Chris@0 107 protected function varToString($var)
Chris@0 108 {
Chris@14 109 @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 110
Chris@0 111 if (null === $this->valueExporter) {
Chris@0 112 $this->valueExporter = new ValueExporter();
Chris@0 113 }
Chris@0 114
Chris@0 115 return $this->valueExporter->exportValue($var);
Chris@0 116 }
Chris@0 117
Chris@14 118 /**
Chris@14 119 * @return callable[] The casters to add to the cloner
Chris@14 120 */
Chris@14 121 protected function getCasters()
Chris@0 122 {
Chris@17 123 return [
Chris@14 124 '*' => function ($v, array $a, Stub $s, $isNested) {
Chris@14 125 if (!$v instanceof Stub) {
Chris@14 126 foreach ($a as $k => $v) {
Chris@17 127 if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
Chris@14 128 $a[$k] = new CutStub($v);
Chris@14 129 }
Chris@14 130 }
Chris@0 131 }
Chris@0 132
Chris@14 133 return $a;
Chris@14 134 },
Chris@17 135 ];
Chris@0 136 }
Chris@0 137 }