vendor/symfony/web-profiler-bundle/Twig/WebProfilerExtension.php line 84

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\WebProfilerBundle\Twig;
  11. use Symfony\Component\VarDumper\Cloner\Data;
  12. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  13. use Twig\Environment;
  14. use Twig\Extension\EscaperExtension;
  15. use Twig\Extension\ProfilerExtension;
  16. use Twig\Profiler\Profile;
  17. use Twig\Runtime\EscaperRuntime;
  18. use Twig\TwigFunction;
  19. /**
  20.  * Twig extension for the profiler.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @internal
  25.  */
  26. class WebProfilerExtension extends ProfilerExtension
  27. {
  28.     /**
  29.      * @var HtmlDumper
  30.      */
  31.     private $dumper;
  32.     /**
  33.      * @var resource
  34.      */
  35.     private $output;
  36.     /**
  37.      * @var int
  38.      */
  39.     private $stackLevel 0;
  40.     public function __construct(?HtmlDumper $dumper null)
  41.     {
  42.         $this->dumper $dumper ?? new HtmlDumper();
  43.         $this->dumper->setOutput($this->output fopen('php://memory''r+'));
  44.     }
  45.     public function enter(Profile $profile): void
  46.     {
  47.         ++$this->stackLevel;
  48.     }
  49.     public function leave(Profile $profile): void
  50.     {
  51.         if (=== --$this->stackLevel) {
  52.             $this->dumper->setOutput($this->output fopen('php://memory''r+'));
  53.         }
  54.     }
  55.     public function getFunctions(): array
  56.     {
  57.         return [
  58.             new TwigFunction('profiler_dump', [$this'dumpData'], ['is_safe' => ['html'], 'needs_environment' => true]),
  59.             new TwigFunction('profiler_dump_log', [$this'dumpLog'], ['is_safe' => ['html'], 'needs_environment' => true]),
  60.         ];
  61.     }
  62.     public function dumpData(Environment $envData $dataint $maxDepth 0)
  63.     {
  64.         $this->dumper->setCharset($env->getCharset());
  65.         $this->dumper->dump($datanull, [
  66.             'maxDepth' => $maxDepth,
  67.         ]);
  68.         $dump stream_get_contents($this->output, -10);
  69.         rewind($this->output);
  70.         ftruncate($this->output0);
  71.         return str_replace("\n</pre"'</pre'rtrim($dump));
  72.     }
  73.     public function dumpLog(Environment $envstring $message, ?Data $context null)
  74.     {
  75.         $message self::escape($env$message);
  76.         $message preg_replace('/&quot;(.*?)&quot;/''&quot;<b>$1</b>&quot;'$message);
  77.         $replacements = [];
  78.         foreach ($context ?? [] as $k => $v) {
  79.             $k '{'.self::escape($env$k).'}';
  80.             if (str_contains($message$k)) {
  81.                 $replacements[$k] = $v;
  82.             }
  83.         }
  84.         if (!$replacements) {
  85.             return '<span class="dump-inline">'.$message.'</span>';
  86.         }
  87.         foreach ($replacements as $k => $v) {
  88.             $replacements['&quot;<b>'.$k.'</b>&quot;'] = $replacements['&quot;'.$k.'&quot;'] = $replacements[$k] = $this->dumpData($env$v);
  89.         }
  90.         return '<span class="dump-inline">'.strtr($message$replacements).'</span>';
  91.     }
  92.     public function getName()
  93.     {
  94.         return 'profiler';
  95.     }
  96.     private static function escape(Environment $envstring $s): string
  97.     {
  98.         // Twig 3.10 and above
  99.         if (class_exists(EscaperRuntime::class)) {
  100.             return $env->getRuntime(EscaperRuntime::class)->escape($s);
  101.         }
  102.         // Twig 3.9
  103.         if (method_exists(EscaperExtension::class, 'escape')) {
  104.             return EscaperExtension::escape($env$s);
  105.         }
  106.         // to be removed when support for Twig 3 is dropped
  107.         return twig_escape_filter($env$s);
  108.     }
  109. }