DumpDataCollector.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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
  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. public function collect(Request $request, Response $response, \Throwable $exception = null)
  88. {
  89. if (!$this->dataCount) {
  90. $this->data = [];
  91. }
  92. // Sub-requests and programmatic calls stay in the collected profile.
  93. if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
  94. return;
  95. }
  96. // In all other conditions that remove the web debug toolbar, dumps are written on the output.
  97. if (!$this->requestStack
  98. || !$response->headers->has('X-Debug-Token')
  99. || $response->isRedirection()
  100. || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
  101. || 'html' !== $request->getRequestFormat()
  102. || false === strripos($response->getContent(), '</body>')
  103. ) {
  104. if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
  105. $dumper = new HtmlDumper('php://output', $this->charset);
  106. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  107. } else {
  108. $dumper = new CliDumper('php://output', $this->charset);
  109. if (method_exists($dumper, 'setDisplayOptions')) {
  110. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  111. }
  112. }
  113. foreach ($this->data as $dump) {
  114. $this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
  115. }
  116. }
  117. }
  118. public function reset()
  119. {
  120. if ($this->stopwatch) {
  121. $this->stopwatch->reset();
  122. }
  123. $this->data = [];
  124. $this->dataCount = 0;
  125. $this->isCollected = true;
  126. $this->clonesCount = 0;
  127. $this->clonesIndex = 0;
  128. }
  129. /**
  130. * @internal
  131. */
  132. public function __sleep(): array
  133. {
  134. if (!$this->dataCount) {
  135. $this->data = [];
  136. }
  137. if ($this->clonesCount !== $this->clonesIndex) {
  138. return [];
  139. }
  140. $this->data[] = $this->fileLinkFormat;
  141. $this->data[] = $this->charset;
  142. $this->dataCount = 0;
  143. $this->isCollected = true;
  144. return parent::__sleep();
  145. }
  146. /**
  147. * @internal
  148. */
  149. public function __wakeup()
  150. {
  151. parent::__wakeup();
  152. $charset = array_pop($this->data);
  153. $fileLinkFormat = array_pop($this->data);
  154. $this->dataCount = \count($this->data);
  155. self::__construct($this->stopwatch, $fileLinkFormat, $charset);
  156. }
  157. public function getDumpsCount(): int
  158. {
  159. return $this->dataCount;
  160. }
  161. public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1): array
  162. {
  163. $data = fopen('php://memory', 'r+b');
  164. if ('html' === $format) {
  165. $dumper = new HtmlDumper($data, $this->charset);
  166. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  167. } else {
  168. throw new \InvalidArgumentException(sprintf('Invalid dump format: "%s".', $format));
  169. }
  170. $dumps = [];
  171. if (!$this->dataCount) {
  172. return $this->data = [];
  173. }
  174. foreach ($this->data as $dump) {
  175. $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
  176. $dump['data'] = stream_get_contents($data, -1, 0);
  177. ftruncate($data, 0);
  178. rewind($data);
  179. $dumps[] = $dump;
  180. }
  181. return $dumps;
  182. }
  183. public function getName(): string
  184. {
  185. return 'dump';
  186. }
  187. public function __destruct()
  188. {
  189. if (0 === $this->clonesCount-- && !$this->isCollected && $this->dataCount) {
  190. $this->clonesCount = 0;
  191. $this->isCollected = true;
  192. $h = headers_list();
  193. $i = \count($h);
  194. array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
  195. while (0 !== stripos($h[$i], 'Content-Type:')) {
  196. --$i;
  197. }
  198. if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && stripos($h[$i], 'html')) {
  199. $dumper = new HtmlDumper('php://output', $this->charset);
  200. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  201. } else {
  202. $dumper = new CliDumper('php://output', $this->charset);
  203. if (method_exists($dumper, 'setDisplayOptions')) {
  204. $dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
  205. }
  206. }
  207. foreach ($this->data as $i => $dump) {
  208. $this->data[$i] = null;
  209. $this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
  210. }
  211. $this->data = [];
  212. $this->dataCount = 0;
  213. }
  214. }
  215. private function doDump(DataDumperInterface $dumper, $data, string $name, string $file, int $line)
  216. {
  217. if ($dumper instanceof CliDumper) {
  218. $contextDumper = function ($name, $file, $line, $fmt) {
  219. if ($this instanceof HtmlDumper) {
  220. if ($file) {
  221. $s = $this->style('meta', '%s');
  222. $f = strip_tags($this->style('', $file));
  223. $name = strip_tags($this->style('', $name));
  224. if ($fmt && $link = \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : $fmt->format($file, $line)) {
  225. $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
  226. } else {
  227. $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
  228. }
  229. } else {
  230. $name = $this->style('meta', $name);
  231. }
  232. $this->line = $name.' on line '.$this->style('meta', $line).':';
  233. } else {
  234. $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
  235. }
  236. $this->dumpLine(0);
  237. };
  238. $contextDumper = $contextDumper->bindTo($dumper, $dumper);
  239. $contextDumper($name, $file, $line, $this->fileLinkFormat);
  240. } else {
  241. $cloner = new VarCloner();
  242. $dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
  243. }
  244. $dumper->dump($data);
  245. }
  246. }