DumpDataCollector.php 9.2KB

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