PageRenderTime 25ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/DataCollector/DumpDataCollector.php

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