4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Drupal\webprofiler\Profiler;
14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 use Symfony\Component\HttpKernel\Profiler\Profiler as SymfonyProfiler;
16 use Symfony\Component\HttpKernel\Profiler\Profile;
19 * Profiler Templates Manager
21 class TemplateManager {
24 * @var \Twig_Environment
29 * @var \Twig_Loader_Chain
31 protected $twigLoader;
39 * @var \Symfony\Component\HttpKernel\Profiler\Profiler
46 * @param SymfonyProfiler $profiler
47 * @param \Twig_Environment $twig
48 * @param \Twig_Loader_Chain $twigLoader
49 * @param array $templates
51 public function __construct(SymfonyProfiler $profiler, \Twig_Environment $twig, \Twig_Loader_Chain $twigLoader, array $templates) {
52 $this->profiler = $profiler;
54 $this->twigLoader = $twigLoader;
55 $this->templates = $templates;
59 * Gets the template name for a given panel.
61 * @param Profile $profile
62 * @param string $panel
66 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
68 public function getName(Profile $profile, $panel) {
69 $templates = $this->getNames($profile);
71 if (!isset($templates[$panel])) {
72 throw new NotFoundHttpException(sprintf('Panel "%s" is not registered in profiler or is not present in viewed profile.', $panel));
75 return $templates[$panel];
79 * Gets the templates for a given profile.
81 * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
85 public function getTemplates(Profile $profile) {
86 $templates = $this->getNames($profile);
87 foreach ($templates as $name => $template) {
88 $templates[$name] = $this->twig->loadTemplate($template);
95 * Gets template names of templates that are present in the viewed profile.
97 * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
101 * @throws \UnexpectedValueException
103 protected function getNames(Profile $profile) {
106 foreach ($this->templates as $arguments) {
107 if (NULL === $arguments) {
111 list($name, $template) = $arguments;
113 if (!$this->profiler->has($name) || !$profile->hasCollector($name)) {
117 if ('.html.twig' === substr($template, -10)) {
118 $template = substr($template, 0, -10);
121 if (!$this->twigLoader->exists($template . '.html.twig')) {
122 throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
125 $templates[$name] = $template . '.html.twig';