You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB

3 years ago
  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. * Converts the variable into a serializable Data instance.
  37. *
  38. * This array can be displayed in the template using
  39. * the VarDumper component.
  40. *
  41. * @param mixed $var
  42. *
  43. * @return Data
  44. */
  45. protected function cloneVar($var)
  46. {
  47. if ($var instanceof Data) {
  48. return $var;
  49. }
  50. if (null === $this->cloner) {
  51. $this->cloner = new VarCloner();
  52. $this->cloner->setMaxItems(-1);
  53. $this->cloner->addCasters($this->getCasters());
  54. }
  55. return $this->cloner->cloneVar($var);
  56. }
  57. /**
  58. * @return callable[] The casters to add to the cloner
  59. */
  60. protected function getCasters()
  61. {
  62. $casters = [
  63. '*' => function ($v, array $a, Stub $s, $isNested) {
  64. if (!$v instanceof Stub) {
  65. foreach ($a as $k => $v) {
  66. if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  67. $a[$k] = new CutStub($v);
  68. }
  69. }
  70. }
  71. return $a;
  72. },
  73. ] + ReflectionCaster::UNSET_CLOSURE_FILE_INFO;
  74. return $casters;
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function __sleep()
  80. {
  81. return ['data'];
  82. }
  83. public function __wakeup()
  84. {
  85. }
  86. /**
  87. * @internal to prevent implementing \Serializable
  88. */
  89. final protected function serialize()
  90. {
  91. }
  92. /**
  93. * @internal to prevent implementing \Serializable
  94. */
  95. final protected function unserialize($data)
  96. {
  97. }
  98. }