DumpDataCollector.php 9.9 KB

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