DataCollector.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\ClassStub;
  13. use Symfony\Component\VarDumper\Cloner\ClonerInterface;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  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 = array();
  27. /**
  28. * @var ValueExporter
  29. */
  30. private $valueExporter;
  31. /**
  32. * @var ClonerInterface
  33. */
  34. private static $cloner;
  35. public function serialize()
  36. {
  37. return serialize($this->data);
  38. }
  39. public function unserialize($data)
  40. {
  41. $this->data = unserialize($data);
  42. }
  43. /**
  44. * Converts the variable into a serializable Data instance.
  45. *
  46. * This array can be displayed in the template using
  47. * the VarDumper component.
  48. *
  49. * @param mixed $var
  50. *
  51. * @return Data
  52. */
  53. protected function cloneVar($var)
  54. {
  55. if (null === self::$cloner) {
  56. if (class_exists(ClassStub::class)) {
  57. self::$cloner = new VarCloner();
  58. self::$cloner->setMaxItems(-1);
  59. } else {
  60. @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);
  61. self::$cloner = false;
  62. }
  63. }
  64. if (false === self::$cloner) {
  65. if (null === $this->valueExporter) {
  66. $this->valueExporter = new ValueExporter();
  67. }
  68. return $this->valueExporter->exportValue($var);
  69. }
  70. return self::$cloner->cloneVar($var);
  71. }
  72. /**
  73. * Converts a PHP variable to a string.
  74. *
  75. * @param mixed $var A PHP variable
  76. *
  77. * @return string The string representation of the variable
  78. *
  79. * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
  80. */
  81. protected function varToString($var)
  82. {
  83. @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);
  84. if (null === $this->valueExporter) {
  85. $this->valueExporter = new ValueExporter();
  86. }
  87. return $this->valueExporter->exportValue($var);
  88. }
  89. }