TwigDataCollector.php 5.2 KB

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