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