菜谱项目

DataCollector.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = array();
  28. /**
  29. * @var ValueExporter
  30. */
  31. private $valueExporter;
  32. /**
  33. * @var ClonerInterface
  34. */
  35. private $cloner;
  36. public function serialize()
  37. {
  38. return serialize($this->data);
  39. }
  40. public function unserialize($data)
  41. {
  42. $this->data = unserialize($data);
  43. }
  44. /**
  45. * Converts the variable into a serializable Data instance.
  46. *
  47. * This array can be displayed in the template using
  48. * the VarDumper component.
  49. *
  50. * @param mixed $var
  51. *
  52. * @return Data
  53. */
  54. protected function cloneVar($var)
  55. {
  56. if ($var instanceof Data) {
  57. return $var;
  58. }
  59. if (null === $this->cloner) {
  60. if (class_exists(CutStub::class)) {
  61. $this->cloner = new VarCloner();
  62. $this->cloner->setMaxItems(-1);
  63. $this->cloner->addCasters($this->getCasters());
  64. } else {
  65. @trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
  66. $this->cloner = false;
  67. }
  68. }
  69. if (false === $this->cloner) {
  70. if (null === $this->valueExporter) {
  71. $this->valueExporter = new ValueExporter();
  72. }
  73. return $this->valueExporter->exportValue($var);
  74. }
  75. return $this->cloner->cloneVar($var);
  76. }
  77. /**
  78. * Converts a PHP variable to a string.
  79. *
  80. * @param mixed $var A PHP variable
  81. *
  82. * @return string The string representation of the variable
  83. *
  84. * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
  85. */
  86. protected function varToString($var)
  87. {
  88. @trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use cloneVar() instead.', __METHOD__), E_USER_DEPRECATED);
  89. if (null === $this->valueExporter) {
  90. $this->valueExporter = new ValueExporter();
  91. }
  92. return $this->valueExporter->exportValue($var);
  93. }
  94. /**
  95. * @return callable[] The casters to add to the cloner
  96. */
  97. protected function getCasters()
  98. {
  99. return array(
  100. '*' => function ($v, array $a, Stub $s, $isNested) {
  101. if (!$v instanceof Stub) {
  102. foreach ($a as $k => $v) {
  103. if (is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  104. $a[$k] = new CutStub($v);
  105. }
  106. }
  107. }
  108. return $a;
  109. },
  110. );
  111. }
  112. }