No Description

DataCollector.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Caster\LinkStub;
  14. use Symfony\Component\VarDumper\Caster\StubCaster;
  15. use Symfony\Component\VarDumper\Cloner\ClonerInterface;
  16. use Symfony\Component\VarDumper\Cloner\Data;
  17. use Symfony\Component\VarDumper\Cloner\Stub;
  18. use Symfony\Component\VarDumper\Cloner\VarCloner;
  19. /**
  20. * DataCollector.
  21. *
  22. * Children of this class must store the collected data in the data property.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Bernhard Schussek <bschussek@symfony.com>
  26. */
  27. abstract class DataCollector implements DataCollectorInterface, \Serializable
  28. {
  29. protected $data = array();
  30. /**
  31. * @var ValueExporter
  32. */
  33. private $valueExporter;
  34. /**
  35. * @var ClonerInterface
  36. */
  37. private $cloner;
  38. private static $stubsCache = array();
  39. public function serialize()
  40. {
  41. return serialize($this->data);
  42. }
  43. public function unserialize($data)
  44. {
  45. $this->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 (null === $this->cloner) {
  60. if (class_exists(ClassStub::class)) {
  61. $this->cloner = new VarCloner();
  62. $this->cloner->setMaxItems(250);
  63. $this->cloner->addCasters(array(
  64. Stub::class => function (Stub $v, array $a, Stub $s, $isNested) {
  65. return $isNested ? $a : StubCaster::castStub($v, $a, $s, true);
  66. },
  67. ));
  68. } else {
  69. @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);
  70. $this->cloner = false;
  71. }
  72. }
  73. if (false === $this->cloner) {
  74. if (null === $this->valueExporter) {
  75. $this->valueExporter = new ValueExporter();
  76. }
  77. return $this->valueExporter->exportValue($var);
  78. }
  79. return $this->cloner->cloneVar($this->decorateVar($var));
  80. }
  81. /**
  82. * Converts a PHP variable to a string.
  83. *
  84. * @param mixed $var A PHP variable
  85. *
  86. * @return string The string representation of the variable
  87. *
  88. * @deprecated Deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
  89. */
  90. protected function varToString($var)
  91. {
  92. @trigger_error(sprintf('The %() method is deprecated since version 3.2 and will be removed in 4.0. Use cloneVar() instead.', __METHOD__), E_USER_DEPRECATED);
  93. if (null === $this->valueExporter) {
  94. $this->valueExporter = new ValueExporter();
  95. }
  96. return $this->valueExporter->exportValue($var);
  97. }
  98. private function decorateVar($var)
  99. {
  100. if (is_array($var)) {
  101. if (isset($var[0], $var[1]) && is_callable($var)) {
  102. return ClassStub::wrapCallable($var);
  103. }
  104. foreach ($var as $k => $v) {
  105. if ($v !== $d = $this->decorateVar($v)) {
  106. $var[$k] = $d;
  107. }
  108. }
  109. return $var;
  110. }
  111. if (is_string($var)) {
  112. if (isset(self::$stubsCache[$var])) {
  113. return self::$stubsCache[$var];
  114. }
  115. if (false !== strpos($var, '\\')) {
  116. $c = (false !== $i = strpos($var, '::')) ? substr($var, 0, $i) : $var;
  117. if (class_exists($c, false) || interface_exists($c, false) || trait_exists($c, false)) {
  118. return self::$stubsCache[$var] = new ClassStub($var);
  119. }
  120. }
  121. if (false !== strpos($var, DIRECTORY_SEPARATOR) && false === strpos($var, '://') && false === strpos($var, "\0") && @is_file($var)) {
  122. return self::$stubsCache[$var] = new LinkStub($var);
  123. }
  124. }
  125. return $var;
  126. }
  127. }