PageRenderTime 59ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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