PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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