TemplateManager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Bundle\WebProfilerBundle\Profiler;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\HttpKernel\Profiler\Profiler;
  13. use Symfony\Component\HttpKernel\Profiler\Profile;
  14. /**
  15. * Profiler Templates Manager
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Artur Wielogórski <wodor@wodor.net>
  19. */
  20. class TemplateManager
  21. {
  22. protected $twig;
  23. protected $templates;
  24. protected $profiler;
  25. /**
  26. * Constructor.
  27. *
  28. * @param Profiler $profiler
  29. * @param \Twig_Environment $twig
  30. * @param array $templates
  31. */
  32. public function __construct(Profiler $profiler, \Twig_Environment $twig, array $templates)
  33. {
  34. $this->profiler = $profiler;
  35. $this->twig = $twig;
  36. $this->templates = $templates;
  37. }
  38. /**
  39. * Gets the template name for a given panel.
  40. *
  41. * @param Profile $profile
  42. * @param string $panel
  43. *
  44. * @return mixed
  45. *
  46. * @throws NotFoundHttpException
  47. */
  48. public function getName(Profile $profile, $panel)
  49. {
  50. $templates = $this->getNames($profile);
  51. if (!isset($templates[$panel])) {
  52. throw new NotFoundHttpException(sprintf('Panel "%s" is not registered in profiler or is not present in viewed profile.', $panel));
  53. }
  54. return $templates[$panel];
  55. }
  56. /**
  57. * Gets the templates for a given profile.
  58. *
  59. * @param Profile $profile
  60. *
  61. * @return array
  62. */
  63. public function getTemplates(Profile $profile)
  64. {
  65. $templates = $this->getNames($profile);
  66. foreach ($templates as $name => $template) {
  67. $templates[$name] = $this->twig->loadTemplate($template);
  68. }
  69. return $templates;
  70. }
  71. /**
  72. * Gets template names of templates that are present in the viewed profile.
  73. *
  74. * @param Profile $profile
  75. *
  76. * @return array
  77. *
  78. * @throws \UnexpectedValueException
  79. */
  80. protected function getNames(Profile $profile)
  81. {
  82. $templates = array();
  83. foreach ($this->templates as $arguments) {
  84. if (null === $arguments) {
  85. continue;
  86. }
  87. list($name, $template) = $arguments;
  88. if (!$this->profiler->has($name) || !$profile->hasCollector($name)) {
  89. continue;
  90. }
  91. if ('.html.twig' === substr($template, -10)) {
  92. $template = substr($template, 0, -10);
  93. }
  94. if (!$this->templateExists($template.'.html.twig')) {
  95. throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
  96. }
  97. $templates[$name] = $template.'.html.twig';
  98. }
  99. return $templates;
  100. }
  101. // to be removed when the minimum required version of Twig is >= 2.0
  102. protected function templateExists($template)
  103. {
  104. $loader = $this->twig->getLoader();
  105. if ($loader instanceof \Twig_ExistsLoaderInterface) {
  106. return $loader->exists($template);
  107. }
  108. try {
  109. $loader->getSource($template);
  110. return true;
  111. } catch (\Twig_Error_Loader $e) {
  112. }
  113. return false;
  114. }
  115. }