|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DebugBar\Bridge; |
| 6 | + |
| 7 | +use DebugBar\DataCollector\AssetProvider; |
| 8 | +use DebugBar\DataCollector\DataCollector; |
| 9 | +use DebugBar\DataCollector\Renderable; |
| 10 | +use Twig\Environment; |
| 11 | +use Twig\Loader\LoaderInterface; |
| 12 | +use Twig\Profiler\Dumper\HtmlDumper; |
| 13 | +use Twig\Profiler\Profile; |
| 14 | + |
| 15 | +/** |
| 16 | + * Collects data about rendered templates |
| 17 | + * |
| 18 | + * http://twig.sensiolabs.org/ |
| 19 | + * |
| 20 | + * A \Twig\Profiler\Profile should be added to your \Twig\Environment |
| 21 | + * The root-Twig\Profiler\Profile-object should then be injected into this collector |
| 22 | + * |
| 23 | + * you can optionally provide the \Twig\Environment or the \Twig\Loader to also create |
| 24 | + * debug-links. |
| 25 | + * |
| 26 | + * @see \Twig\Extension\ProfilerExtension, \Twig\Profiler\Profile |
| 27 | + * |
| 28 | + * <code> |
| 29 | + * $env = new \Twig\Environment($loader); // Or from a PSR11-container |
| 30 | + * $profile = new \Twig\Profiler\Profile(); |
| 31 | + * $env->addExtension(new \Twig\Extension\ProfilerExtension($profile)); |
| 32 | + * $debugbar->addCollector(new ModernTwigProfileCollector($profile, $env)); |
| 33 | + * // or: $debugbar->addCollector(new ModernTwigProfileCollector($profile, $loader)); |
| 34 | + * </code> |
| 35 | + */ |
| 36 | +class NamespacedTwigProfileCollector extends DataCollector implements Renderable, AssetProvider |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @var Profile |
| 40 | + */ |
| 41 | + private $profile; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var LoaderInterface|Environment|null |
| 45 | + */ |
| 46 | + private $loader; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var int |
| 50 | + */ |
| 51 | + private $templateCount; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var int |
| 55 | + */ |
| 56 | + private $blockCount; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var int |
| 60 | + */ |
| 61 | + private $macroCount; |
| 62 | + /** |
| 63 | + * @var array[] { |
| 64 | + * @var string $name |
| 65 | + * @var int $render_time |
| 66 | + * @var string $render_time_str |
| 67 | + * @var string $memory_str |
| 68 | + * @var string $xdebug_link |
| 69 | + * } |
| 70 | + */ |
| 71 | + private $templates; |
| 72 | + |
| 73 | + /** |
| 74 | + * TwigProfileCollector constructor. |
| 75 | + * |
| 76 | + * @param Profile $profile |
| 77 | + * @param LoaderInterface|Environment $loaderOrEnv |
| 78 | + */ |
| 79 | + public function __construct(Profile $profile, $loaderOrEnv = null) |
| 80 | + { |
| 81 | + $this->profile = $profile; |
| 82 | + if ($loaderOrEnv instanceof Environment) { |
| 83 | + $loaderOrEnv = $loaderOrEnv->getLoader(); |
| 84 | + } |
| 85 | + $this->loader = $loaderOrEnv; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a hash where keys are control names and their values |
| 90 | + * an array of options as defined in {@see \DebugBar\JavascriptRenderer::addControl()} |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function getWidgets() |
| 95 | + { |
| 96 | + return [ |
| 97 | + 'twig' => [ |
| 98 | + 'icon' => 'leaf', |
| 99 | + 'widget' => 'PhpDebugBar.Widgets.TemplatesWidget', |
| 100 | + 'map' => 'twig', |
| 101 | + 'default' => json_encode(['templates' => []]), |
| 102 | + ], |
| 103 | + 'twig:badge' => [ |
| 104 | + 'map' => 'twig.badge', |
| 105 | + 'default' => 0, |
| 106 | + ], |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + public function getAssets() |
| 114 | + { |
| 115 | + return [ |
| 116 | + 'css' => 'widgets/templates/widget.css', |
| 117 | + 'js' => 'widgets/templates/widget.js', |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Called by the DebugBar when data needs to be collected |
| 123 | + * |
| 124 | + * @return array Collected data |
| 125 | + */ |
| 126 | + public function collect() |
| 127 | + { |
| 128 | + $this->templateCount = $this->blockCount = $this->macroCount = 0; |
| 129 | + $this->templates = []; |
| 130 | + $this->computeData($this->profile); |
| 131 | + |
| 132 | + return [ |
| 133 | + 'nb_templates' => $this->templateCount, |
| 134 | + 'nb_blocks' => $this->blockCount, |
| 135 | + 'nb_macros' => $this->macroCount, |
| 136 | + 'templates' => $this->templates, |
| 137 | + 'accumulated_render_time' => $this->profile->getDuration(), |
| 138 | + 'accumulated_render_time_str' => $this->getDataFormatter()->formatDuration($this->profile->getDuration()), |
| 139 | + 'memory_usage_str' => $this->getDataFormatter()->formatBytes($this->profile->getMemoryUsage()), |
| 140 | + 'callgraph' => $this->getHtmlCallGraph(), |
| 141 | + 'badge' => implode( |
| 142 | + '/', |
| 143 | + [ |
| 144 | + $this->templateCount, |
| 145 | + $this->blockCount, |
| 146 | + $this->macroCount, |
| 147 | + ] |
| 148 | + ), |
| 149 | + ]; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns the unique name of the collector |
| 154 | + * |
| 155 | + * @return string |
| 156 | + */ |
| 157 | + public function getName() |
| 158 | + { |
| 159 | + return 'twig'; |
| 160 | + } |
| 161 | + |
| 162 | + public function getHtmlCallGraph() |
| 163 | + { |
| 164 | + $dumper = new HtmlDumper(); |
| 165 | + return $dumper->dump($this->profile); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get an Xdebug Link to a file |
| 170 | + * |
| 171 | + * @return array { |
| 172 | + * @var string url |
| 173 | + * @var bool ajax |
| 174 | + * } |
| 175 | + */ |
| 176 | + public function getXdebugLink($template, $line = 1) |
| 177 | + { |
| 178 | + if (is_null($this->loader)) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + $file = $this->loader->getSourceContext($template)->getPath(); |
| 182 | + |
| 183 | + return parent::getXdebugLink($file, $line); |
| 184 | + } |
| 185 | + |
| 186 | + private function computeData(Profile $profile) |
| 187 | + { |
| 188 | + $this->templateCount += ($profile->isTemplate() ? 1 : 0); |
| 189 | + $this->blockCount += ($profile->isBlock() ? 1 : 0); |
| 190 | + $this->macroCount += ($profile->isMacro() ? 1 : 0); |
| 191 | + if ($profile->isTemplate()) { |
| 192 | + $this->templates[] = [ |
| 193 | + 'name' => $profile->getName(), |
| 194 | + 'render_time' => $profile->getDuration(), |
| 195 | + 'render_time_str' => $this->getDataFormatter()->formatDuration($profile->getDuration()), |
| 196 | + 'memory_str' => $this->getDataFormatter()->formatBytes($profile->getMemoryUsage()), |
| 197 | + 'xdebug_link' => $this->getXdebugLink($profile->getTemplate()), |
| 198 | + ]; |
| 199 | + } |
| 200 | + foreach ($profile as $p) { |
| 201 | + $this->computeData($p); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments