TwigDataCollector.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\HttpKernel\DataCollector\DataCollector;
  12. use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Twig\Markup;
  16. use Twig\Profiler\Dumper\HtmlDumper;
  17. use Twig\Profiler\Profile;
  18. /**
  19. * TwigDataCollector.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
  24. {
  25. private $profile;
  26. private $computed;
  27. public function __construct(Profile $profile)
  28. {
  29. $this->profile = $profile;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function collect(Request $request, Response $response, \Exception $exception = null)
  35. {
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function lateCollect()
  41. {
  42. $this->data['profile'] = serialize($this->profile);
  43. }
  44. public function getTime()
  45. {
  46. return $this->getProfile()->getDuration() * 1000;
  47. }
  48. public function getTemplateCount()
  49. {
  50. return $this->getComputedData('template_count');
  51. }
  52. public function getTemplates()
  53. {
  54. return $this->getComputedData('templates');
  55. }
  56. public function getBlockCount()
  57. {
  58. return $this->getComputedData('block_count');
  59. }
  60. public function getMacroCount()
  61. {
  62. return $this->getComputedData('macro_count');
  63. }
  64. public function getHtmlCallGraph()
  65. {
  66. $dumper = new HtmlDumper();
  67. $dump = $dumper->dump($this->getProfile());
  68. // needed to remove the hardcoded CSS styles
  69. $dump = str_replace(array(
  70. '<span style="background-color: #ffd">',
  71. '<span style="color: #d44">',
  72. '<span style="background-color: #dfd">',
  73. ), array(
  74. '<span class="status-warning">',
  75. '<span class="status-error">',
  76. '<span class="status-success">',
  77. ), $dump);
  78. return new Markup($dump, 'UTF-8');
  79. }
  80. public function getProfile()
  81. {
  82. if (null === $this->profile) {
  83. if (\PHP_VERSION_ID >= 70000) {
  84. $this->profile = unserialize($this->data['profile'], array('allowed_classes' => array('Twig_Profiler_Profile', 'Twig\Profiler\Profile')));
  85. } else {
  86. $this->profile = unserialize($this->data['profile']);
  87. }
  88. }
  89. return $this->profile;
  90. }
  91. private function getComputedData($index)
  92. {
  93. if (null === $this->computed) {
  94. $this->computed = $this->computeData($this->getProfile());
  95. }
  96. return $this->computed[$index];
  97. }
  98. private function computeData(Profile $profile)
  99. {
  100. $data = array(
  101. 'template_count' => 0,
  102. 'block_count' => 0,
  103. 'macro_count' => 0,
  104. );
  105. $templates = array();
  106. foreach ($profile as $p) {
  107. $d = $this->computeData($p);
  108. $data['template_count'] += ($p->isTemplate() ? 1 : 0) + $d['template_count'];
  109. $data['block_count'] += ($p->isBlock() ? 1 : 0) + $d['block_count'];
  110. $data['macro_count'] += ($p->isMacro() ? 1 : 0) + $d['macro_count'];
  111. if ($p->isTemplate()) {
  112. if (!isset($templates[$p->getTemplate()])) {
  113. $templates[$p->getTemplate()] = 1;
  114. } else {
  115. ++$templates[$p->getTemplate()];
  116. }
  117. }
  118. foreach ($d['templates'] as $template => $count) {
  119. if (!isset($templates[$template])) {
  120. $templates[$template] = $count;
  121. } else {
  122. $templates[$template] += $count;
  123. }
  124. }
  125. }
  126. $data['templates'] = $templates;
  127. return $data;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getName()
  133. {
  134. return 'twig';
  135. }
  136. }