Drupal investigation

DumpDataCollector.php 11KB

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