DumpDataCollector.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Symfony\Component\VarDumper\Cloner\Data;
  16. use Symfony\Component\VarDumper\Cloner\VarCloner;
  17. use Symfony\Component\VarDumper\Dumper\CliDumper;
  18. use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
  19. use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
  20. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  21. use Symfony\Component\VarDumper\Server\Connection;
  22. /**
  23. * @author Nicolas Grekas <p@tchwork.com>
  24. */
  25. class DumpDataCollector extends DataCollector implements DataDumperInterface
  26. {
  27. private $stopwatch;
  28. private $fileLinkFormat;
  29. private $dataCount = 0;
  30. private $isCollected = true;
  31. private $clonesCount = 0;
  32. private $clonesIndex = 0;
  33. private $rootRefs;
  34. private $charset;
  35. private $requestStack;
  36. private $dumper;
  37. private $sourceContextProvider;
  38. /**
  39. * @param DataDumperInterface|Connection|null $dumper
  40. */
  41. public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, string $charset = null, RequestStack $requestStack = null, $dumper = null)
  42. {
  43. $this->stopwatch = $stopwatch;
  44. $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  45. $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8';
  46. $this->requestStack = $requestStack;
  47. $this->dumper = $dumper;
  48. // All clones share these properties by reference:
  49. $this->rootRefs = [
  50. &$this->data,
  51. &$this->dataCount,
  52. &$this->isCollected,
  53. &$this->clonesCount,
  54. ];
  55. $this->sourceContextProvider = $dumper instanceof Connection && isset($dumper->getContextProviders()['source']) ? $dumper->getContextProviders()['source'] : new SourceContextProvider($this->charset);
  56. }
  57. public function __clone()
  58. {
  59. $this->clonesIndex = ++$this->clonesCount;
  60. }
  61. public function dump(Data $data)
  62. {
  63. if ($this->stopwatch) {
  64. $this->stopwatch->start('dump');
  65. }
  66. list('name' => $name, 'file' => $file, 'line' => $line, 'file_excerpt' => $fileExcerpt) = $this->sourceContextProvider->getContext();
  67. if ($this->dumper instanceof Connection) {
  68. if (!$this->dumper->write($data)) {
  69. $this->isCollected = false;
  70. }
  71. } elseif ($this->dumper) {
  72. $this->doDump($this->dumper, $data, $name, $file, $line);
  73. } else {
  74. $this->isCollected = false;
  75. }
  76. $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
  77. ++$this->dataCount;
  78. if ($this->stopwatch) {
  79. $this->stopwatch->stop('dump');
  80. }
  81. }
  82. public function collect(Request $request, Response $response, \Exception $exception = null)
  83. {
  84. // Sub-requests and programmatic calls stay in the collected profile.
  85. if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
  86. return;
  87. }
  88. // In all other conditions that remove the web debug toolbar, dumps are written on the output.
  89. if (!$this->requestStack
  90. || !$response->headers->has('X-Debug-Token')
  91. || $response->isRedirection()
  92. || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
  93. || 'html' !== $request->getRequestFormat()
  94. || false === strripos($response->getContent(), '</body>')
  95. ) {
  96. if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
  97. $dumper = new HtmlDumper('php://output', $this->charset);
  98. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  99. } else {
  100. $dumper = new CliDumper('php://output', $this->charset);
  101. }
  102. foreach ($this->data as $dump) {
  103. $this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
  104. }
  105. }
  106. }
  107. public function reset()
  108. {
  109. if ($this->stopwatch) {
  110. $this->stopwatch->reset();
  111. }
  112. $this->data = [];
  113. $this->dataCount = 0;
  114. $this->isCollected = true;
  115. $this->clonesCount = 0;
  116. $this->clonesIndex = 0;
  117. }
  118. /**
  119. * @internal
  120. */
  121. public function serialize()
  122. {
  123. if ($this->clonesCount !== $this->clonesIndex) {
  124. return 'a:0:{}';
  125. }
  126. $this->data[] = $this->fileLinkFormat;
  127. $this->data[] = $this->charset;
  128. $ser = serialize($this->data);
  129. $this->data = [];
  130. $this->dataCount = 0;
  131. $this->isCollected = true;
  132. return $ser;
  133. }
  134. /**
  135. * @internal
  136. */
  137. public function unserialize($data)
  138. {
  139. $this->data = unserialize($data);
  140. $charset = array_pop($this->data);
  141. $fileLinkFormat = array_pop($this->data);
  142. $this->dataCount = \count($this->data);
  143. self::__construct($this->stopwatch, $fileLinkFormat, $charset);
  144. }
  145. public function getDumpsCount()
  146. {
  147. return $this->dataCount;
  148. }
  149. public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
  150. {
  151. $data = fopen('php://memory', 'r+b');
  152. if ('html' === $format) {
  153. $dumper = new HtmlDumper($data, $this->charset);
  154. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  155. } else {
  156. throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
  157. }
  158. $dumps = [];
  159. foreach ($this->data as $dump) {
  160. $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
  161. $dump['data'] = stream_get_contents($data, -1, 0);
  162. ftruncate($data, 0);
  163. rewind($data);
  164. $dumps[] = $dump;
  165. }
  166. return $dumps;
  167. }
  168. public function getName()
  169. {
  170. return 'dump';
  171. }
  172. public function __destruct()
  173. {
  174. if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
  175. $this->clonesCount = 0;
  176. $this->isCollected = true;
  177. $h = headers_list();
  178. $i = \count($h);
  179. array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
  180. while (0 !== stripos($h[$i], 'Content-Type:')) {
  181. --$i;
  182. }
  183. if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
  184. $html = 'html' === $_SERVER['VAR_DUMPER_FORMAT'];
  185. } else {
  186. $html = !\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && stripos($h[$i], 'html');
  187. }
  188. if ($html) {
  189. $dumper = new HtmlDumper('php://output', $this->charset);
  190. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  191. } else {
  192. $dumper = new CliDumper('php://output', $this->charset);
  193. }
  194. foreach ($this->data as $i => $dump) {
  195. $this->data[$i] = null;
  196. $this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
  197. }
  198. $this->data = [];
  199. $this->dataCount = 0;
  200. }
  201. }
  202. private function doDump(DataDumperInterface $dumper, $data, $name, $file, $line)
  203. {
  204. if ($dumper instanceof CliDumper) {
  205. $contextDumper = function ($name, $file, $line, $fmt) {
  206. if ($this instanceof HtmlDumper) {
  207. if ($file) {
  208. $s = $this->style('meta', '%s');
  209. $f = strip_tags($this->style('', $file));
  210. $name = strip_tags($this->style('', $name));
  211. if ($fmt && $link = \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : $fmt->format($file, $line)) {
  212. $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
  213. } else {
  214. $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
  215. }
  216. } else {
  217. $name = $this->style('meta', $name);
  218. }
  219. $this->line = $name.' on line '.$this->style('meta', $line).':';
  220. } else {
  221. $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
  222. }
  223. $this->dumpLine(0);
  224. };
  225. $contextDumper = $contextDumper->bindTo($dumper, $dumper);
  226. $contextDumper($name, $file, $line, $this->fileLinkFormat);
  227. } else {
  228. $cloner = new VarCloner();
  229. $dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
  230. }
  231. $dumper->dump($data);
  232. }
  233. }