DataCollector.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\VarDumper\Caster\CutStub;
  12. use Symfony\Component\VarDumper\Caster\ReflectionCaster;
  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
  26. {
  27. /**
  28. * @var array|Data
  29. */
  30. protected $data = [];
  31. /**
  32. * @var ClonerInterface
  33. */
  34. private $cloner;
  35. /**
  36. * @deprecated since Symfony 4.3, store all the serialized state in the data property instead
  37. */
  38. public function serialize()
  39. {
  40. @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.3, store all the serialized state in the data property instead.', __METHOD__), E_USER_DEPRECATED);
  41. $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  42. $isCalledFromOverridingMethod = isset($trace[1]['function'], $trace[1]['object']) && 'serialize' === $trace[1]['function'] && $this === $trace[1]['object'];
  43. return $isCalledFromOverridingMethod ? $this->data : serialize($this->data);
  44. }
  45. /**
  46. * @deprecated since Symfony 4.3, store all the serialized state in the data property instead
  47. */
  48. public function unserialize($data)
  49. {
  50. @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.3, store all the serialized state in the data property instead.', __METHOD__), E_USER_DEPRECATED);
  51. $this->data = \is_array($data) ? $data : unserialize($data);
  52. }
  53. /**
  54. * Converts the variable into a serializable Data instance.
  55. *
  56. * This array can be displayed in the template using
  57. * the VarDumper component.
  58. *
  59. * @param mixed $var
  60. *
  61. * @return Data
  62. */
  63. protected function cloneVar($var)
  64. {
  65. if ($var instanceof Data) {
  66. return $var;
  67. }
  68. if (null === $this->cloner) {
  69. $this->cloner = new VarCloner();
  70. $this->cloner->setMaxItems(-1);
  71. $this->cloner->addCasters($this->getCasters());
  72. }
  73. return $this->cloner->cloneVar($var);
  74. }
  75. /**
  76. * @return callable[] The casters to add to the cloner
  77. */
  78. protected function getCasters()
  79. {
  80. $casters = [
  81. '*' => function ($v, array $a, Stub $s, $isNested) {
  82. if (!$v instanceof Stub) {
  83. foreach ($a as $k => $v) {
  84. if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  85. $a[$k] = new CutStub($v);
  86. }
  87. }
  88. }
  89. return $a;
  90. },
  91. ] + ReflectionCaster::UNSET_CLOSURE_FILE_INFO;
  92. return $casters;
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function __sleep()
  98. {
  99. if (__CLASS__ !== $c = (new \ReflectionMethod($this, 'serialize'))->getDeclaringClass()->name) {
  100. @trigger_error(sprintf('Implementing the "%s::serialize()" method is deprecated since Symfony 4.3, store all the serialized state in the "data" property instead.', $c), E_USER_DEPRECATED);
  101. $this->data = $this->serialize();
  102. }
  103. return ['data'];
  104. }
  105. public function __wakeup()
  106. {
  107. if (__CLASS__ !== $c = (new \ReflectionMethod($this, 'unserialize'))->getDeclaringClass()->name) {
  108. @trigger_error(sprintf('Implementing the "%s::unserialize()" method is deprecated since Symfony 4.3, store all the serialized state in the "data" property instead.', $c), E_USER_DEPRECATED);
  109. $this->unserialize($this->data);
  110. }
  111. }
  112. }