DataCollector.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Cloner\ClonerInterface;
  13. use Symfony\Component\VarDumper\Cloner\Data;
  14. use Symfony\Component\VarDumper\Cloner\Stub;
  15. use Symfony\Component\VarDumper\Cloner\VarCloner;
  16. /**
  17. * DataCollector.
  18. *
  19. * Children of this class must store the collected data in the data property.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Bernhard Schussek <bschussek@symfony.com>
  23. */
  24. abstract class DataCollector implements DataCollectorInterface, \Serializable
  25. {
  26. protected $data = [];
  27. /**
  28. * @var ClonerInterface
  29. */
  30. private $cloner;
  31. /**
  32. * @internal
  33. */
  34. public function serialize()
  35. {
  36. $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  37. $isCalledFromOverridingMethod = isset($trace[1]['function'], $trace[1]['object']) && 'serialize' === $trace[1]['function'] && $this === $trace[1]['object'];
  38. return $isCalledFromOverridingMethod ? $this->data : serialize($this->data);
  39. }
  40. /**
  41. * @internal
  42. */
  43. public function unserialize($data)
  44. {
  45. $this->data = \is_array($data) ? $data : unserialize($data);
  46. }
  47. /**
  48. * Converts the variable into a serializable Data instance.
  49. *
  50. * This array can be displayed in the template using
  51. * the VarDumper component.
  52. *
  53. * @param mixed $var
  54. *
  55. * @return Data
  56. */
  57. protected function cloneVar($var)
  58. {
  59. if ($var instanceof Data) {
  60. return $var;
  61. }
  62. if (null === $this->cloner) {
  63. if (!class_exists(CutStub::class)) {
  64. throw new \LogicException(sprintf('The VarDumper component is needed for the %s() method. Install symfony/var-dumper version 3.4 or above.', __METHOD__));
  65. }
  66. $this->cloner = new VarCloner();
  67. $this->cloner->setMaxItems(-1);
  68. $this->cloner->addCasters($this->getCasters());
  69. }
  70. return $this->cloner->cloneVar($var);
  71. }
  72. /**
  73. * @return callable[] The casters to add to the cloner
  74. */
  75. protected function getCasters()
  76. {
  77. return [
  78. '*' => function ($v, array $a, Stub $s, $isNested) {
  79. if (!$v instanceof Stub) {
  80. foreach ($a as $k => $v) {
  81. if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  82. $a[$k] = new CutStub($v);
  83. }
  84. }
  85. }
  86. return $a;
  87. },
  88. ];
  89. }
  90. }