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_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS;
75 if (\PHP_VERSION_ID >= 50400) {
76 $trace = debug_backtrace($trace, 7);
78 $trace = debug_backtrace($trace);
81 $file = $trace[0]['file'];
82 $line = $trace[0]['line'];
86 for ($i = 1; $i < 7; ++$i) {
87 if (isset($trace[$i]['class'], $trace[$i]['function'])
88 && 'dump' === $trace[$i]['function']
89 && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
91 $file = $trace[$i]['file'];
92 $line = $trace[$i]['line'];
95 if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
96 $file = $trace[$i]['file'];
97 $line = $trace[$i]['line'];
100 } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) {
101 $template = $trace[$i]['object'];
102 $name = $template->getTemplateName();
103 $src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
104 $info = $template->getDebugInfo();
105 if (isset($info[$trace[$i - 1]['line']])) {
106 $line = $info[$trace[$i - 1]['line']];
107 $file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;
110 $src = explode("\n", $src);
111 $fileExcerpt = array();
113 for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) {
114 $fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
117 $fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
127 if (false === $name) {
128 $name = str_replace('\\', '/', $file);
129 $name = substr($name, strrpos($name, '/') + 1);
133 $this->doDump($data, $name, $file, $line);
136 $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
139 if ($this->stopwatch) {
140 $this->stopwatch->stop('dump');
144 public function collect(Request $request, Response $response, \Exception $exception = null)
146 // Sub-requests and programmatic calls stay in the collected profile.
147 if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
151 // In all other conditions that remove the web debug toolbar, dumps are written on the output.
152 if (!$this->requestStack
153 || !$response->headers->has('X-Debug-Token')
154 || $response->isRedirection()
155 || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
156 || 'html' !== $request->getRequestFormat()
157 || false === strripos($response->getContent(), '</body>')
159 if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
160 $this->dumper = new HtmlDumper('php://output', $this->charset);
162 $this->dumper = new CliDumper('php://output', $this->charset);
165 foreach ($this->data as $dump) {
166 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
171 public function serialize()
173 if ($this->clonesCount !== $this->clonesIndex) {
177 $this->data[] = $this->fileLinkFormat;
178 $this->data[] = $this->charset;
179 $ser = serialize($this->data);
180 $this->data = array();
181 $this->dataCount = 0;
182 $this->isCollected = true;
183 if (!$this->dumperIsInjected) {
184 $this->dumper = null;
190 public function unserialize($data)
192 parent::unserialize($data);
193 $charset = array_pop($this->data);
194 $fileLinkFormat = array_pop($this->data);
195 $this->dataCount = count($this->data);
196 self::__construct($this->stopwatch, $fileLinkFormat, $charset);
199 public function getDumpsCount()
201 return $this->dataCount;
204 public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
206 $data = fopen('php://memory', 'r+b');
208 if ('html' === $format) {
209 $dumper = new HtmlDumper($data, $this->charset);
211 throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
215 foreach ($this->data as $dump) {
216 if (method_exists($dump['data'], 'withMaxDepth')) {
217 $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
219 // getLimitedClone is @deprecated, to be removed in 3.0
220 $dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth));
222 $dump['data'] = stream_get_contents($data, -1, 0);
231 public function getName()
236 public function __destruct()
238 if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
239 $this->clonesCount = 0;
240 $this->isCollected = true;
244 array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
245 while (0 !== stripos($h[$i], 'Content-Type:')) {
249 if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
250 $this->dumper = new HtmlDumper('php://output', $this->charset);
252 $this->dumper = new CliDumper('php://output', $this->charset);
255 foreach ($this->data as $i => $dump) {
256 $this->data[$i] = null;
257 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
260 $this->data = array();
261 $this->dataCount = 0;
265 private function doDump($data, $name, $file, $line)
267 if (\PHP_VERSION_ID >= 50400 && $this->dumper instanceof CliDumper) {
268 $contextDumper = function ($name, $file, $line, $fileLinkFormat) {
269 if ($this instanceof HtmlDumper) {
271 $s = $this->style('meta', '%s');
272 $name = strip_tags($this->style('', $name));
273 $file = strip_tags($this->style('', $file));
274 if ($fileLinkFormat) {
275 $link = strtr(strip_tags($this->style('', $fileLinkFormat)), array('%f' => $file, '%l' => (int) $line));
276 $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', $link, $file, $name);
278 $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $file, $name);
281 $name = $this->style('meta', $name);
283 $this->line = $name.' on line '.$this->style('meta', $line).':';
285 $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
289 $contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
290 $contextDumper($name, $file, $line, $this->fileLinkFormat);
292 $cloner = new VarCloner();
293 $this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
295 $this->dumper->dump($data);
298 private function htmlEncode($s)
302 $dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
303 $dumper->setDumpHeader('');
304 $dumper->setDumpBoundaries('', '');
306 $cloner = new VarCloner();
307 $dumper->dump($cloner->cloneVar($s));
309 return substr(strip_tags($html), 1, -1);