Dashboard sipadu mbip
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.

DataCollector.php 4.4KB

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\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. protected $data = [];
  28. /**
  29. * @var ClonerInterface
  30. */
  31. private $cloner;
  32. /**
  33. * @deprecated since Symfony 4.3, store all the serialized state in the data property instead
  34. */
  35. public function serialize()
  36. {
  37. @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);
  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. /**
  43. * @deprecated since Symfony 4.3, store all the serialized state in the data property instead
  44. */
  45. public function unserialize($data)
  46. {
  47. @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);
  48. $this->data = \is_array($data) ? $data : unserialize($data);
  49. }
  50. /**
  51. * Converts the variable into a serializable Data instance.
  52. *
  53. * This array can be displayed in the template using
  54. * the VarDumper component.
  55. *
  56. * @param mixed $var
  57. *
  58. * @return Data
  59. */
  60. protected function cloneVar($var)
  61. {
  62. if ($var instanceof Data) {
  63. return $var;
  64. }
  65. if (null === $this->cloner) {
  66. if (!class_exists(CutStub::class)) {
  67. throw new \LogicException(sprintf('The VarDumper component is needed for the %s() method. Install symfony/var-dumper version 3.4 or above.', __METHOD__));
  68. }
  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. ];
  92. if (method_exists(ReflectionCaster::class, 'unsetClosureFileInfo')) {
  93. $casters += ReflectionCaster::UNSET_CLOSURE_FILE_INFO;
  94. }
  95. return $casters;
  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. }