[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/Bridge/ -> TwigProfileCollector.php (source)

   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   * @deprecated Use `\Debugbar\Bridge\NamespacedTwigProfileCollector` instead for Twig 2.x and 3.x
  39   */
  40  class TwigProfileCollector extends DataCollector implements Renderable, AssetProvider
  41  {
  42      /**
  43       * @var \Twig_Profiler_Profile
  44       */
  45      private $profile;
  46      /**
  47       * @var \Twig_LoaderInterface
  48       */
  49      private $loader;
  50      /** @var int */
  51      private $templateCount;
  52      /** @var int */
  53      private $blockCount;
  54      /** @var int */
  55      private $macroCount;
  56      /**
  57       * @var array[] {
  58       * @var string $name
  59       * @var int    $render_time
  60       * @var string $render_time_str
  61       * @var string $memory_str
  62       * @var string $xdebug_link
  63       * }
  64       */
  65      private $templates;
  66  
  67      /**
  68       * TwigProfileCollector constructor.
  69       *
  70       * @param \Twig_Profiler_Profile $profile
  71       * @param \Twig_LoaderInterface|\Twig_Environment $loaderOrEnv
  72       */
  73      public function __construct(\Twig_Profiler_Profile $profile, $loaderOrEnv = null)
  74      {
  75          $this->profile     = $profile;
  76          if ($loaderOrEnv instanceof \Twig_Environment) {
  77              $loaderOrEnv = $loaderOrEnv->getLoader();
  78          }
  79          $this->loader = $loaderOrEnv;
  80      }
  81  
  82      /**
  83       * Returns a hash where keys are control names and their values
  84       * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()}
  85       *
  86       * @return array
  87       */
  88      public function getWidgets()
  89      {
  90          return array(
  91              'twig'       => array(
  92                  'icon'    => 'leaf',
  93                  'widget'  => 'PhpDebugBar.Widgets.TemplatesWidget',
  94                  'map'     => 'twig',
  95                  'default' => json_encode(array('templates' => array())),
  96              ),
  97              'twig:badge' => array(
  98                  'map'     => 'twig.badge',
  99                  'default' => 0,
 100              ),
 101          );
 102      }
 103  
 104      /**
 105       * @return array
 106       */
 107      public function getAssets()
 108      {
 109          return array(
 110              'css' => 'widgets/templates/widget.css',
 111              'js'  => 'widgets/templates/widget.js',
 112          );
 113      }
 114  
 115      /**
 116       * Called by the DebugBar when data needs to be collected
 117       *
 118       * @return array Collected data
 119       */
 120      public function collect()
 121      {
 122          $this->templateCount = $this->blockCount = $this->macroCount = 0;
 123          $this->templates     = array();
 124          $this->computeData($this->profile);
 125  
 126          return array(
 127              'nb_templates'                => $this->templateCount,
 128              'nb_blocks'                   => $this->blockCount,
 129              'nb_macros'                   => $this->macroCount,
 130              'templates'                   => $this->templates,
 131              'accumulated_render_time'     => $this->profile->getDuration(),
 132              'accumulated_render_time_str' => $this->getDataFormatter()->formatDuration($this->profile->getDuration()),
 133              'memory_usage_str'            => $this->getDataFormatter()->formatBytes($this->profile->getMemoryUsage()),
 134              'callgraph'                   => $this->getHtmlCallGraph(),
 135              'badge'                       => implode(
 136                  '/',
 137                  array(
 138                      $this->templateCount,
 139                      $this->blockCount,
 140                      $this->macroCount,
 141                  )
 142              ),
 143          );
 144      }
 145  
 146      /**
 147       * Returns the unique name of the collector
 148       *
 149       * @return string
 150       */
 151      public function getName()
 152      {
 153          return 'twig';
 154      }
 155  
 156      public function getHtmlCallGraph()
 157      {
 158          $dumper = new \Twig_Profiler_Dumper_Html();
 159  
 160          return $dumper->dump($this->profile);
 161      }
 162  
 163      /**
 164       * Get an Xdebug Link to a file
 165       *
 166       * @return array {
 167       *  @var string url
 168       *  @var bool ajax
 169       * }
 170       */
 171      public function getXdebugLink($template, $line = 1)
 172      {
 173          if (is_null($this->loader)) {
 174              return null;
 175          }
 176          $file = $this->loader->getSourceContext($template)->getPath();
 177  
 178          return parent::getXdebugLink($file, $line);
 179      }
 180  
 181      private function computeData(\Twig_Profiler_Profile $profile)
 182      {
 183          $this->templateCount += ($profile->isTemplate() ? 1 : 0);
 184          $this->blockCount    += ($profile->isBlock() ? 1 : 0);
 185          $this->macroCount    += ($profile->isMacro() ? 1 : 0);
 186          if ($profile->isTemplate()) {
 187              $this->templates[] = array(
 188                  'name'            => $profile->getName(),
 189                  'render_time'     => $profile->getDuration(),
 190                  'render_time_str' => $this->getDataFormatter()->formatDuration($profile->getDuration()),
 191                  'memory_str'      => $this->getDataFormatter()->formatBytes($profile->getMemoryUsage()),
 192                  'xdebug_link'     => $this->getXdebugLink($profile->getTemplate()),
 193              );
 194          }
 195          foreach ($profile as $p) {
 196              $this->computeData($p);
 197          }
 198      }
 199  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer