Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\DataCollector; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpFoundation\RequestStack; Chris@0: use Symfony\Component\HttpFoundation\Response; Chris@0: use Symfony\Component\Stopwatch\Stopwatch; Chris@0: use Symfony\Component\VarDumper\Cloner\Data; Chris@0: use Symfony\Component\VarDumper\Cloner\VarCloner; Chris@0: use Symfony\Component\VarDumper\Dumper\CliDumper; Chris@17: use Symfony\Component\VarDumper\Dumper\DataDumperInterface; Chris@0: use Symfony\Component\VarDumper\Dumper\HtmlDumper; Chris@12: use Twig\Template; Chris@0: Chris@0: /** Chris@0: * @author Nicolas Grekas
Chris@0: */ Chris@0: class DumpDataCollector extends DataCollector implements DataDumperInterface Chris@0: { Chris@0: private $stopwatch; Chris@0: private $fileLinkFormat; Chris@0: private $dataCount = 0; Chris@0: private $isCollected = true; Chris@0: private $clonesCount = 0; Chris@0: private $clonesIndex = 0; Chris@0: private $rootRefs; Chris@0: private $charset; Chris@0: private $requestStack; Chris@0: private $dumper; Chris@0: private $dumperIsInjected; Chris@0: Chris@0: public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null, RequestStack $requestStack = null, DataDumperInterface $dumper = null) Chris@0: { Chris@0: $this->stopwatch = $stopwatch; Chris@0: $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); Chris@0: $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8'; Chris@0: $this->requestStack = $requestStack; Chris@0: $this->dumper = $dumper; Chris@0: $this->dumperIsInjected = null !== $dumper; Chris@0: Chris@0: // All clones share these properties by reference: Chris@17: $this->rootRefs = [ Chris@0: &$this->data, Chris@0: &$this->dataCount, Chris@0: &$this->isCollected, Chris@0: &$this->clonesCount, Chris@17: ]; Chris@0: } Chris@0: Chris@0: public function __clone() Chris@0: { Chris@0: $this->clonesIndex = ++$this->clonesCount; Chris@0: } Chris@0: Chris@0: public function dump(Data $data) Chris@0: { Chris@0: if ($this->stopwatch) { Chris@0: $this->stopwatch->start('dump'); Chris@0: } Chris@14: if ($this->isCollected && !$this->dumper) { Chris@0: $this->isCollected = false; Chris@0: } Chris@0: Chris@0: $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 7); Chris@0: Chris@0: $file = $trace[0]['file']; Chris@0: $line = $trace[0]['line']; Chris@0: $name = false; Chris@0: $fileExcerpt = false; Chris@0: Chris@0: for ($i = 1; $i < 7; ++$i) { Chris@0: if (isset($trace[$i]['class'], $trace[$i]['function']) Chris@0: && 'dump' === $trace[$i]['function'] Chris@0: && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class'] Chris@0: ) { Chris@0: $file = $trace[$i]['file']; Chris@0: $line = $trace[$i]['line']; Chris@0: Chris@0: while (++$i < 7) { Chris@0: if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) { Chris@0: $file = $trace[$i]['file']; Chris@0: $line = $trace[$i]['line']; Chris@0: Chris@0: break; Chris@12: } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) { Chris@0: $template = $trace[$i]['object']; Chris@0: $name = $template->getTemplateName(); Chris@0: $src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false); Chris@0: $info = $template->getDebugInfo(); Chris@0: if (isset($info[$trace[$i - 1]['line']])) { Chris@0: $line = $info[$trace[$i - 1]['line']]; Chris@0: $file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null; Chris@0: Chris@0: if ($src) { Chris@0: $src = explode("\n", $src); Chris@17: $fileExcerpt = []; Chris@0: Chris@17: for ($i = max($line - 3, 1), $max = min($line + 3, \count($src)); $i <= $max; ++$i) { Chris@0: $fileExcerpt[] = '
'.$this->htmlEncode($src[$i - 1]).'