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\HtmlDumper;
22 use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
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) {
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 serialize()
169 if ($this->clonesCount !== $this->clonesIndex) {
173 $this->data[] = $this->fileLinkFormat;
174 $this->data[] = $this->charset;
175 $ser = serialize($this->data);
176 $this->data = array();
177 $this->dataCount = 0;
178 $this->isCollected = true;
179 if (!$this->dumperIsInjected) {
180 $this->dumper = null;
186 public function unserialize($data)
188 parent::unserialize($data);
189 $charset = array_pop($this->data);
190 $fileLinkFormat = array_pop($this->data);
191 $this->dataCount = count($this->data);
192 self::__construct($this->stopwatch, $fileLinkFormat, $charset);
195 public function getDumpsCount()
197 return $this->dataCount;
200 public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
202 $data = fopen('php://memory', 'r+b');
204 if ('html' === $format) {
205 $dumper = new HtmlDumper($data, $this->charset);
206 $dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
208 throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
212 foreach ($this->data as $dump) {
213 $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
214 $dump['data'] = stream_get_contents($data, -1, 0);
223 public function getName()
228 public function __destruct()
230 if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
231 $this->clonesCount = 0;
232 $this->isCollected = true;
236 array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
237 while (0 !== stripos($h[$i], 'Content-Type:')) {
241 if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
242 $this->dumper = new HtmlDumper('php://output', $this->charset);
243 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
245 $this->dumper = new CliDumper('php://output', $this->charset);
248 foreach ($this->data as $i => $dump) {
249 $this->data[$i] = null;
250 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
253 $this->data = array();
254 $this->dataCount = 0;
258 private function doDump($data, $name, $file, $line)
260 if ($this->dumper instanceof CliDumper) {
261 $contextDumper = function ($name, $file, $line, $fmt) {
262 if ($this instanceof HtmlDumper) {
264 $s = $this->style('meta', '%s');
265 $f = strip_tags($this->style('', $file));
266 $name = strip_tags($this->style('', $name));
267 if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line)) {
268 $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
270 $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
273 $name = $this->style('meta', $name);
275 $this->line = $name.' on line '.$this->style('meta', $line).':';
277 $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
281 $contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
282 $contextDumper($name, $file, $line, $this->fileLinkFormat);
284 $cloner = new VarCloner();
285 $this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
287 $this->dumper->dump($data);
290 private function htmlEncode($s)
294 $dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
295 $dumper->setDumpHeader('');
296 $dumper->setDumpBoundaries('', '');
298 $cloner = new VarCloner();
299 $dumper->dump($cloner->cloneVar($s));
301 return substr(strip_tags($html), 1, -1);