* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\DataCollector; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\CliDumper; use Symfony\Component\VarDumper\Dumper\HtmlDumper; use Symfony\Component\VarDumper\Dumper\DataDumperInterface; /** * @author Nicolas Grekas */ class DumpDataCollector extends DataCollector implements DataDumperInterface { private $stopwatch; private $fileLinkFormat; private $dataCount = 0; private $isCollected = true; private $clonesCount = 0; private $clonesIndex = 0; private $rootRefs; private $charset; public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null) { $this->stopwatch = $stopwatch; $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8'; // All clones share these properties by reference: $this->rootRefs = array( &$this->data, &$this->dataCount, &$this->isCollected, &$this->clonesCount, ); } public function __clone() { $this->clonesIndex = ++$this->clonesCount; } public function dump(Data $data) { if ($this->stopwatch) { $this->stopwatch->start('dump'); } if ($this->isCollected) { $this->isCollected = false; } $trace = DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS; if (PHP_VERSION_ID >= 50400) { $trace = debug_backtrace($trace, 7); } else { $trace = debug_backtrace($trace); } $file = $trace[0]['file']; $line = $trace[0]['line']; $name = false; $fileExcerpt = false; for ($i = 1; $i < 7; ++$i) { if (isset($trace[$i]['class'], $trace[$i]['function']) && 'dump' === $trace[$i]['function'] && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class'] ) { $file = $trace[$i]['file']; $line = $trace[$i]['line']; while (++$i < 7) { if (isset($trace[$i]['function']) && empty($trace[$i]['class']) && 'call_user_func' !== $trace[$i]['function']) { $file = $trace[$i]['file']; $line = $trace[$i]['line']; break; } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) { $info = $trace[$i]['object']; $name = $info->getTemplateName(); $src = $info->getEnvironment()->getLoader()->getSource($name); $info = $info->getDebugInfo(); if (isset($info[$trace[$i-1]['line']])) { $file = false; $line = $info[$trace[$i-1]['line']]; $src = explode("\n", $src); $fileExcerpt = array(); for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) { $fileExcerpt[] = ''.$this->htmlEncode($src[$i - 1]).''; } $fileExcerpt = '
    '.implode("\n", $fileExcerpt).'
'; } break; } } break; } } if (false === $name) { $name = strtr($file, '\\', '/'); $name = substr($file, strrpos($file, '/') + 1); } $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt'); ++$this->dataCount; if ($this->stopwatch) { $this->stopwatch->stop('dump'); } } public function collect(Request $request, Response $response, \Exception $exception = null) { } public function serialize() { if ($this->clonesCount !== $this->clonesIndex) { return 'a:0:{}'; } $ser = serialize($this->data); $this->data = array(); $this->dataCount = 0; $this->isCollected = true; return $ser; } public function unserialize($data) { parent::unserialize($data); $this->dataCount = count($this->data); self::__construct($this->stopwatch); } public function getDumpsCount() { return $this->dataCount; } public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1) { $data = fopen('php://memory', 'r+b'); if ('html' === $format) { $dumper = new HtmlDumper($data, $this->charset); } else { throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format)); } $dumps = array(); foreach ($this->data as $dump) { $dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth)); rewind($data); $dump['data'] = stream_get_contents($data); ftruncate($data, 0); rewind($data); $dumps[] = $dump; } return $dumps; } public function getName() { return 'dump'; } public function __destruct() { if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) { $this->clonesCount = 0; $this->isCollected = true; $h = headers_list(); $i = count($h); array_unshift($h, 'Content-Type: '.ini_get('default_mimetype')); while (0 !== stripos($h[$i], 'Content-Type:')) { --$i; } if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) { $dumper = new HtmlDumper('php://output', $this->charset); } else { $dumper = new CliDumper('php://output', $this->charset); $dumper->setColors(false); } foreach ($this->data as $i => $dump) { $this->data[$i] = null; if ($dumper instanceof HtmlDumper) { $dump['name'] = $this->htmlEncode($dump['name']); $dump['file'] = $this->htmlEncode($dump['file']); if ('' !== $dump['file']) { if ($this->fileLinkFormat) { $link = strtr($this->fileLinkFormat, array('%f' => $dump['file'], '%l' => $dump['line'])); $dump['name'] = sprintf('%s', $link, $dump['file'], $dump['name']); } else { $dump['name'] = sprintf('%s', $dump['file'], $dump['name']); } } echo "\n{$dump['name']} on line {$dump['line']}:"; } else { echo "{$dump['name']} on line {$dump['line']}:\n"; } $dumper->dump($dump['data']); } $this->data = array(); $this->dataCount = 0; } } private function htmlEncode($s) { $html = ''; $dumper = new HtmlDumper(function ($line) use (&$html) {$html .= $line;}, $this->charset); $dumper->setDumpHeader(''); $dumper->setDumpBoundaries('', ''); $cloner = new VarCloner(); $dumper->dump($cloner->cloneVar($s)); return substr(strip_tags($html), 1, -1); } }