Text.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2015 Fabien Potencier
  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. * @author Fabien Potencier <fabien@symfony.com>
  12. */
  13. class Twig_Profiler_Dumper_Text
  14. {
  15. private $root;
  16. public function dump(Twig_Profiler_Profile $profile)
  17. {
  18. return $this->dumpProfile($profile);
  19. }
  20. protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix)
  21. {
  22. return sprintf('%s└ %s', $prefix, $profile->getTemplate());
  23. }
  24. protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix)
  25. {
  26. return sprintf('%s└ %s::%s(%s)', $prefix, $profile->getTemplate(), $profile->getType(), $profile->getName());
  27. }
  28. protected function formatTime(Twig_Profiler_Profile $profile, $percent)
  29. {
  30. return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent);
  31. }
  32. private function dumpProfile(Twig_Profiler_Profile $profile, $prefix = '', $sibling = false)
  33. {
  34. if ($profile->isRoot()) {
  35. $this->root = $profile->getDuration();
  36. $start = $profile->getName();
  37. } else {
  38. if ($profile->isTemplate()) {
  39. $start = $this->formatTemplate($profile, $prefix);
  40. } else {
  41. $start = $this->formatNonTemplate($profile, $prefix);
  42. }
  43. $prefix .= $sibling ? '│ ' : ' ';
  44. }
  45. $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0;
  46. if ($profile->getDuration() * 1000 < 1) {
  47. $str = $start."\n";
  48. } else {
  49. $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent));
  50. }
  51. $nCount = count($profile->getProfiles());
  52. foreach ($profile as $i => $p) {
  53. $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount);
  54. }
  55. return $str;
  56. }
  57. }