[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/DataCollector/ -> TwigDataCollector.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Bridge\Twig\DataCollector;
  13  
  14  use Symfony\Component\HttpFoundation\Request;
  15  use Symfony\Component\HttpFoundation\Response;
  16  use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  17  use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  18  use Twig\Environment;
  19  use Twig\Error\LoaderError;
  20  use Twig\Markup;
  21  use Twig\Profiler\Dumper\HtmlDumper;
  22  use Twig\Profiler\Profile;
  23  
  24  /**
  25   * TwigDataCollector.
  26   *
  27   * @author Fabien Potencier <fabien@symfony.com>
  28   */
  29  class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
  30  {
  31      private $profile;
  32      private $twig;
  33      private $computed;
  34  
  35      public function __construct(Profile $profile, Environment $twig = null)
  36      {
  37          $this->profile = $profile;
  38          $this->twig = $twig;
  39      }
  40  
  41      /**
  42       * {@inheritdoc}
  43       */
  44      public function collect(Request $request, Response $response, \Exception $exception = null)
  45      {
  46      }
  47  
  48      /**
  49       * {@inheritdoc}
  50       */
  51      public function reset()
  52      {
  53          $this->profile->reset();
  54          $this->computed = null;
  55          $this->data = [];
  56      }
  57  
  58      /**
  59       * {@inheritdoc}
  60       */
  61      public function lateCollect()
  62      {
  63          $this->data['profile'] = serialize($this->profile);
  64          $this->data['template_paths'] = [];
  65  
  66          if (null === $this->twig) {
  67              return;
  68          }
  69  
  70          $templateFinder = function (Profile $profile) use (&$templateFinder) {
  71              if ($profile->isTemplate()) {
  72                  try {
  73                      $template = $this->twig->load($name = $profile->getName());
  74                  } catch (LoaderError $e) {
  75                      $template = null;
  76                  }
  77  
  78                  if (null !== $template && '' !== $path = $template->getSourceContext()->getPath()) {
  79                      $this->data['template_paths'][$name] = $path;
  80                  }
  81              }
  82  
  83              foreach ($profile as $p) {
  84                  $templateFinder($p);
  85              }
  86          };
  87          $templateFinder($this->profile);
  88      }
  89  
  90      public function getTime()
  91      {
  92          return $this->getProfile()->getDuration() * 1000;
  93      }
  94  
  95      public function getTemplateCount()
  96      {
  97          return $this->getComputedData('template_count');
  98      }
  99  
 100      public function getTemplatePaths()
 101      {
 102          return $this->data['template_paths'];
 103      }
 104  
 105      public function getTemplates()
 106      {
 107          return $this->getComputedData('templates');
 108      }
 109  
 110      public function getBlockCount()
 111      {
 112          return $this->getComputedData('block_count');
 113      }
 114  
 115      public function getMacroCount()
 116      {
 117          return $this->getComputedData('macro_count');
 118      }
 119  
 120      public function getHtmlCallGraph()
 121      {
 122          $dumper = new HtmlDumper();
 123          $dump = $dumper->dump($this->getProfile());
 124  
 125          // needed to remove the hardcoded CSS styles
 126          $dump = str_replace([
 127              '<span style="background-color: #ffd">',
 128              '<span style="color: #d44">',
 129              '<span style="background-color: #dfd">',
 130          ], [
 131              '<span class="status-warning">',
 132              '<span class="status-error">',
 133              '<span class="status-success">',
 134          ], $dump);
 135  
 136          return new Markup($dump, 'UTF-8');
 137      }
 138  
 139      public function getProfile()
 140      {
 141          if (null === $this->profile) {
 142              if (\PHP_VERSION_ID >= 70000) {
 143                  $this->profile = unserialize($this->data['profile'], ['allowed_classes' => ['Twig_Profiler_Profile', 'Twig\Profiler\Profile']]);
 144              } else {
 145                  $this->profile = unserialize($this->data['profile']);
 146              }
 147          }
 148  
 149          return $this->profile;
 150      }
 151  
 152      private function getComputedData($index)
 153      {
 154          if (null === $this->computed) {
 155              $this->computed = $this->computeData($this->getProfile());
 156          }
 157  
 158          return $this->computed[$index];
 159      }
 160  
 161      private function computeData(Profile $profile)
 162      {
 163          $data = [
 164              'template_count' => 0,
 165              'block_count' => 0,
 166              'macro_count' => 0,
 167          ];
 168  
 169          $templates = [];
 170          foreach ($profile as $p) {
 171              $d = $this->computeData($p);
 172  
 173              $data['template_count'] += ($p->isTemplate() ? 1 : 0) + $d['template_count'];
 174              $data['block_count'] += ($p->isBlock() ? 1 : 0) + $d['block_count'];
 175              $data['macro_count'] += ($p->isMacro() ? 1 : 0) + $d['macro_count'];
 176  
 177              if ($p->isTemplate()) {
 178                  if (!isset($templates[$p->getTemplate()])) {
 179                      $templates[$p->getTemplate()] = 1;
 180                  } else {
 181                      ++$templates[$p->getTemplate()];
 182                  }
 183              }
 184  
 185              foreach ($d['templates'] as $template => $count) {
 186                  if (!isset($templates[$template])) {
 187                      $templates[$template] = $count;
 188                  } else {
 189                      $templates[$template] += $count;
 190                  }
 191              }
 192          }
 193          $data['templates'] = $templates;
 194  
 195          return $data;
 196      }
 197  
 198      /**
 199       * {@inheritdoc}
 200       */
 201      public function getName()
 202      {
 203          return 'twig';
 204      }
 205  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1