annotate vendor/symfony/http-kernel/DataCollector/DumpDataCollector.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpKernel\DataCollector;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpFoundation\Request;
Chris@0 15 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 16 use Symfony\Component\HttpFoundation\Response;
Chris@0 17 use Symfony\Component\Stopwatch\Stopwatch;
Chris@0 18 use Symfony\Component\VarDumper\Cloner\Data;
Chris@0 19 use Symfony\Component\VarDumper\Cloner\VarCloner;
Chris@0 20 use Symfony\Component\VarDumper\Dumper\CliDumper;
Chris@0 21 use Symfony\Component\VarDumper\Dumper\HtmlDumper;
Chris@0 22 use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * @author Nicolas Grekas <p@tchwork.com>
Chris@0 26 */
Chris@0 27 class DumpDataCollector extends DataCollector implements DataDumperInterface
Chris@0 28 {
Chris@0 29 private $stopwatch;
Chris@0 30 private $fileLinkFormat;
Chris@0 31 private $dataCount = 0;
Chris@0 32 private $isCollected = true;
Chris@0 33 private $clonesCount = 0;
Chris@0 34 private $clonesIndex = 0;
Chris@0 35 private $rootRefs;
Chris@0 36 private $charset;
Chris@0 37 private $requestStack;
Chris@0 38 private $dumper;
Chris@0 39 private $dumperIsInjected;
Chris@0 40
Chris@0 41 public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null, RequestStack $requestStack = null, DataDumperInterface $dumper = null)
Chris@0 42 {
Chris@0 43 $this->stopwatch = $stopwatch;
Chris@0 44 $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
Chris@0 45 $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8';
Chris@0 46 $this->requestStack = $requestStack;
Chris@0 47 $this->dumper = $dumper;
Chris@0 48 $this->dumperIsInjected = null !== $dumper;
Chris@0 49
Chris@0 50 // All clones share these properties by reference:
Chris@0 51 $this->rootRefs = array(
Chris@0 52 &$this->data,
Chris@0 53 &$this->dataCount,
Chris@0 54 &$this->isCollected,
Chris@0 55 &$this->clonesCount,
Chris@0 56 );
Chris@0 57 }
Chris@0 58
Chris@0 59 public function __clone()
Chris@0 60 {
Chris@0 61 $this->clonesIndex = ++$this->clonesCount;
Chris@0 62 }
Chris@0 63
Chris@0 64 public function dump(Data $data)
Chris@0 65 {
Chris@0 66 if ($this->stopwatch) {
Chris@0 67 $this->stopwatch->start('dump');
Chris@0 68 }
Chris@0 69 if ($this->isCollected) {
Chris@0 70 $this->isCollected = false;
Chris@0 71 }
Chris@0 72
Chris@0 73 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 7);
Chris@0 74
Chris@0 75 $file = $trace[0]['file'];
Chris@0 76 $line = $trace[0]['line'];
Chris@0 77 $name = false;
Chris@0 78 $fileExcerpt = false;
Chris@0 79
Chris@0 80 for ($i = 1; $i < 7; ++$i) {
Chris@0 81 if (isset($trace[$i]['class'], $trace[$i]['function'])
Chris@0 82 && 'dump' === $trace[$i]['function']
Chris@0 83 && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
Chris@0 84 ) {
Chris@0 85 $file = $trace[$i]['file'];
Chris@0 86 $line = $trace[$i]['line'];
Chris@0 87
Chris@0 88 while (++$i < 7) {
Chris@0 89 if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
Chris@0 90 $file = $trace[$i]['file'];
Chris@0 91 $line = $trace[$i]['line'];
Chris@0 92
Chris@0 93 break;
Chris@0 94 } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) {
Chris@0 95 $template = $trace[$i]['object'];
Chris@0 96 $name = $template->getTemplateName();
Chris@0 97 $src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
Chris@0 98 $info = $template->getDebugInfo();
Chris@0 99 if (isset($info[$trace[$i - 1]['line']])) {
Chris@0 100 $line = $info[$trace[$i - 1]['line']];
Chris@0 101 $file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;
Chris@0 102
Chris@0 103 if ($src) {
Chris@0 104 $src = explode("\n", $src);
Chris@0 105 $fileExcerpt = array();
Chris@0 106
Chris@0 107 for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) {
Chris@0 108 $fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
Chris@0 109 }
Chris@0 110
Chris@0 111 $fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
Chris@0 112 }
Chris@0 113 }
Chris@0 114 break;
Chris@0 115 }
Chris@0 116 }
Chris@0 117 break;
Chris@0 118 }
Chris@0 119 }
Chris@0 120
Chris@0 121 if (false === $name) {
Chris@0 122 $name = str_replace('\\', '/', $file);
Chris@0 123 $name = substr($name, strrpos($name, '/') + 1);
Chris@0 124 }
Chris@0 125
Chris@0 126 if ($this->dumper) {
Chris@0 127 $this->doDump($data, $name, $file, $line);
Chris@0 128 }
Chris@0 129
Chris@0 130 $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
Chris@0 131 ++$this->dataCount;
Chris@0 132
Chris@0 133 if ($this->stopwatch) {
Chris@0 134 $this->stopwatch->stop('dump');
Chris@0 135 }
Chris@0 136 }
Chris@0 137
Chris@0 138 public function collect(Request $request, Response $response, \Exception $exception = null)
Chris@0 139 {
Chris@0 140 // Sub-requests and programmatic calls stay in the collected profile.
Chris@0 141 if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
Chris@0 142 return;
Chris@0 143 }
Chris@0 144
Chris@0 145 // In all other conditions that remove the web debug toolbar, dumps are written on the output.
Chris@0 146 if (!$this->requestStack
Chris@0 147 || !$response->headers->has('X-Debug-Token')
Chris@0 148 || $response->isRedirection()
Chris@0 149 || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
Chris@0 150 || 'html' !== $request->getRequestFormat()
Chris@0 151 || false === strripos($response->getContent(), '</body>')
Chris@0 152 ) {
Chris@0 153 if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
Chris@0 154 $this->dumper = new HtmlDumper('php://output', $this->charset);
Chris@0 155 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
Chris@0 156 } else {
Chris@0 157 $this->dumper = new CliDumper('php://output', $this->charset);
Chris@0 158 }
Chris@0 159
Chris@0 160 foreach ($this->data as $dump) {
Chris@0 161 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
Chris@0 162 }
Chris@0 163 }
Chris@0 164 }
Chris@0 165
Chris@0 166 public function serialize()
Chris@0 167 {
Chris@0 168 if ($this->clonesCount !== $this->clonesIndex) {
Chris@0 169 return 'a:0:{}';
Chris@0 170 }
Chris@0 171
Chris@0 172 $this->data[] = $this->fileLinkFormat;
Chris@0 173 $this->data[] = $this->charset;
Chris@0 174 $ser = serialize($this->data);
Chris@0 175 $this->data = array();
Chris@0 176 $this->dataCount = 0;
Chris@0 177 $this->isCollected = true;
Chris@0 178 if (!$this->dumperIsInjected) {
Chris@0 179 $this->dumper = null;
Chris@0 180 }
Chris@0 181
Chris@0 182 return $ser;
Chris@0 183 }
Chris@0 184
Chris@0 185 public function unserialize($data)
Chris@0 186 {
Chris@0 187 parent::unserialize($data);
Chris@0 188 $charset = array_pop($this->data);
Chris@0 189 $fileLinkFormat = array_pop($this->data);
Chris@0 190 $this->dataCount = count($this->data);
Chris@0 191 self::__construct($this->stopwatch, $fileLinkFormat, $charset);
Chris@0 192 }
Chris@0 193
Chris@0 194 public function getDumpsCount()
Chris@0 195 {
Chris@0 196 return $this->dataCount;
Chris@0 197 }
Chris@0 198
Chris@0 199 public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
Chris@0 200 {
Chris@0 201 $data = fopen('php://memory', 'r+b');
Chris@0 202
Chris@0 203 if ('html' === $format) {
Chris@0 204 $dumper = new HtmlDumper($data, $this->charset);
Chris@0 205 $dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
Chris@0 206 } else {
Chris@0 207 throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
Chris@0 208 }
Chris@0 209 $dumps = array();
Chris@0 210
Chris@0 211 foreach ($this->data as $dump) {
Chris@0 212 $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
Chris@0 213 $dump['data'] = stream_get_contents($data, -1, 0);
Chris@0 214 ftruncate($data, 0);
Chris@0 215 rewind($data);
Chris@0 216 $dumps[] = $dump;
Chris@0 217 }
Chris@0 218
Chris@0 219 return $dumps;
Chris@0 220 }
Chris@0 221
Chris@0 222 public function getName()
Chris@0 223 {
Chris@0 224 return 'dump';
Chris@0 225 }
Chris@0 226
Chris@0 227 public function __destruct()
Chris@0 228 {
Chris@0 229 if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
Chris@0 230 $this->clonesCount = 0;
Chris@0 231 $this->isCollected = true;
Chris@0 232
Chris@0 233 $h = headers_list();
Chris@0 234 $i = count($h);
Chris@0 235 array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
Chris@0 236 while (0 !== stripos($h[$i], 'Content-Type:')) {
Chris@0 237 --$i;
Chris@0 238 }
Chris@0 239
Chris@0 240 if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
Chris@0 241 $this->dumper = new HtmlDumper('php://output', $this->charset);
Chris@0 242 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
Chris@0 243 } else {
Chris@0 244 $this->dumper = new CliDumper('php://output', $this->charset);
Chris@0 245 }
Chris@0 246
Chris@0 247 foreach ($this->data as $i => $dump) {
Chris@0 248 $this->data[$i] = null;
Chris@0 249 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
Chris@0 250 }
Chris@0 251
Chris@0 252 $this->data = array();
Chris@0 253 $this->dataCount = 0;
Chris@0 254 }
Chris@0 255 }
Chris@0 256
Chris@0 257 private function doDump($data, $name, $file, $line)
Chris@0 258 {
Chris@0 259 if ($this->dumper instanceof CliDumper) {
Chris@0 260 $contextDumper = function ($name, $file, $line, $fmt) {
Chris@0 261 if ($this instanceof HtmlDumper) {
Chris@0 262 if ($file) {
Chris@0 263 $s = $this->style('meta', '%s');
Chris@0 264 $f = strip_tags($this->style('', $file));
Chris@0 265 $name = strip_tags($this->style('', $name));
Chris@0 266 if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line)) {
Chris@0 267 $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
Chris@0 268 } else {
Chris@0 269 $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
Chris@0 270 }
Chris@0 271 } else {
Chris@0 272 $name = $this->style('meta', $name);
Chris@0 273 }
Chris@0 274 $this->line = $name.' on line '.$this->style('meta', $line).':';
Chris@0 275 } else {
Chris@0 276 $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
Chris@0 277 }
Chris@0 278 $this->dumpLine(0);
Chris@0 279 };
Chris@0 280 $contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
Chris@0 281 $contextDumper($name, $file, $line, $this->fileLinkFormat);
Chris@0 282 } else {
Chris@0 283 $cloner = new VarCloner();
Chris@0 284 $this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
Chris@0 285 }
Chris@0 286 $this->dumper->dump($data);
Chris@0 287 }
Chris@0 288
Chris@0 289 private function htmlEncode($s)
Chris@0 290 {
Chris@0 291 $html = '';
Chris@0 292
Chris@0 293 $dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
Chris@0 294 $dumper->setDumpHeader('');
Chris@0 295 $dumper->setDumpBoundaries('', '');
Chris@0 296
Chris@0 297 $cloner = new VarCloner();
Chris@0 298 $dumper->dump($cloner->cloneVar($s));
Chris@0 299
Chris@0 300 return substr(strip_tags($html), 1, -1);
Chris@0 301 }
Chris@0 302 }