4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\DataCollector;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\RequestStack;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\Stopwatch\Stopwatch;
18 use Symfony\Component\VarDumper\Cloner\Data;
19 use Symfony\Component\VarDumper\Cloner\VarCloner;
20 use Symfony\Component\VarDumper\Dumper\CliDumper;
21 use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
22 use Symfony\Component\VarDumper\Dumper\HtmlDumper;
26 * @author Nicolas Grekas <p@tchwork.com>
28 class DumpDataCollector extends DataCollector implements DataDumperInterface
31 private $fileLinkFormat;
32 private $dataCount = 0;
33 private $isCollected = true;
34 private $clonesCount = 0;
35 private $clonesIndex = 0;
38 private $requestStack;
40 private $dumperIsInjected;
42 public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null, RequestStack $requestStack = null, DataDumperInterface $dumper = null)
44 $this->stopwatch = $stopwatch;
45 $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
46 $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8';
47 $this->requestStack = $requestStack;
48 $this->dumper = $dumper;
49 $this->dumperIsInjected = null !== $dumper;
51 // All clones share these properties by reference:
52 $this->rootRefs = array(
60 public function __clone()
62 $this->clonesIndex = ++$this->clonesCount;
65 public function dump(Data $data)
67 if ($this->stopwatch) {
68 $this->stopwatch->start('dump');
70 if ($this->isCollected && !$this->dumper) {
71 $this->isCollected = false;
74 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 7);
76 $file = $trace[0]['file'];
77 $line = $trace[0]['line'];
81 for ($i = 1; $i < 7; ++$i) {
82 if (isset($trace[$i]['class'], $trace[$i]['function'])
83 && 'dump' === $trace[$i]['function']
84 && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
86 $file = $trace[$i]['file'];
87 $line = $trace[$i]['line'];
90 if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
91 $file = $trace[$i]['file'];
92 $line = $trace[$i]['line'];
95 } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) {
96 $template = $trace[$i]['object'];
97 $name = $template->getTemplateName();
98 $src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
99 $info = $template->getDebugInfo();
100 if (isset($info[$trace[$i - 1]['line']])) {
101 $line = $info[$trace[$i - 1]['line']];
102 $file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;
105 $src = explode("\n", $src);
106 $fileExcerpt = array();
108 for ($i = max($line - 3, 1), $max = min($line + 3, \count($src)); $i <= $max; ++$i) {
109 $fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
112 $fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
122 if (false === $name) {
123 $name = str_replace('\\', '/', $file);
124 $name = substr($name, strrpos($name, '/') + 1);
128 $this->doDump($data, $name, $file, $line);
131 $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
134 if ($this->stopwatch) {
135 $this->stopwatch->stop('dump');
139 public function collect(Request $request, Response $response, \Exception $exception = null)
141 // Sub-requests and programmatic calls stay in the collected profile.
142 if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
146 // In all other conditions that remove the web debug toolbar, dumps are written on the output.
147 if (!$this->requestStack
148 || !$response->headers->has('X-Debug-Token')
149 || $response->isRedirection()
150 || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
151 || 'html' !== $request->getRequestFormat()
152 || false === strripos($response->getContent(), '</body>')
154 if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
155 $this->dumper = new HtmlDumper('php://output', $this->charset);
156 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
158 $this->dumper = new CliDumper('php://output', $this->charset);
161 foreach ($this->data as $dump) {
162 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
167 public function reset()
169 if ($this->stopwatch) {
170 $this->stopwatch->reset();
172 $this->data = array();
173 $this->dataCount = 0;
174 $this->isCollected = true;
175 $this->clonesCount = 0;
176 $this->clonesIndex = 0;
179 public function serialize()
181 if ($this->clonesCount !== $this->clonesIndex) {
185 $this->data[] = $this->fileLinkFormat;
186 $this->data[] = $this->charset;
187 $ser = serialize($this->data);
188 $this->data = array();
189 $this->dataCount = 0;
190 $this->isCollected = true;
191 if (!$this->dumperIsInjected) {
192 $this->dumper = null;
198 public function unserialize($data)
200 parent::unserialize($data);
201 $charset = array_pop($this->data);
202 $fileLinkFormat = array_pop($this->data);
203 $this->dataCount = \count($this->data);
204 self::__construct($this->stopwatch, $fileLinkFormat, $charset);
207 public function getDumpsCount()
209 return $this->dataCount;
212 public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
214 $data = fopen('php://memory', 'r+b');
216 if ('html' === $format) {
217 $dumper = new HtmlDumper($data, $this->charset);
218 $dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
220 throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
224 foreach ($this->data as $dump) {
225 $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
226 $dump['data'] = stream_get_contents($data, -1, 0);
235 public function getName()
240 public function __destruct()
242 if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
243 $this->clonesCount = 0;
244 $this->isCollected = true;
248 array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
249 while (0 !== stripos($h[$i], 'Content-Type:')) {
253 if (!\in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) && stripos($h[$i], 'html')) {
254 $this->dumper = new HtmlDumper('php://output', $this->charset);
255 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
257 $this->dumper = new CliDumper('php://output', $this->charset);
260 foreach ($this->data as $i => $dump) {
261 $this->data[$i] = null;
262 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
265 $this->data = array();
266 $this->dataCount = 0;
270 private function doDump($data, $name, $file, $line)
272 if ($this->dumper instanceof CliDumper) {
273 $contextDumper = function ($name, $file, $line, $fmt) {
274 if ($this instanceof HtmlDumper) {
276 $s = $this->style('meta', '%s');
277 $f = strip_tags($this->style('', $file));
278 $name = strip_tags($this->style('', $name));
279 if ($fmt && $link = \is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line)) {
280 $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
282 $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
285 $name = $this->style('meta', $name);
287 $this->line = $name.' on line '.$this->style('meta', $line).':';
289 $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
293 $contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
294 $contextDumper($name, $file, $line, $this->fileLinkFormat);
296 $cloner = new VarCloner();
297 $this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
299 $this->dumper->dump($data);
302 private function htmlEncode($s)
306 $dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
307 $dumper->setDumpHeader('');
308 $dumper->setDumpBoundaries('', '');
310 $cloner = new VarCloner();
311 $dumper->dump($cloner->cloneVar($s));
313 return substr(strip_tags($html), 1, -1);