DataCollector.php 3.9 KB

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