Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Profiler / TemplateManager.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Drupal\webprofiler\Profiler;
13
14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 use Symfony\Component\HttpKernel\Profiler\Profiler as SymfonyProfiler;
16 use Symfony\Component\HttpKernel\Profiler\Profile;
17
18 /**
19  * Profiler Templates Manager
20  */
21 class TemplateManager {
22
23   /**
24    * @var \Twig_Environment
25    */
26   protected $twig;
27
28   /**
29    * @var \Twig_Loader_Chain
30    */
31   protected $twigLoader;
32
33   /**
34    * @var array
35    */
36   protected $templates;
37
38   /**
39    * @var \Symfony\Component\HttpKernel\Profiler\Profiler
40    */
41   protected $profiler;
42
43   /**
44    * Constructor.
45    *
46    * @param SymfonyProfiler $profiler
47    * @param \Twig_Environment $twig
48    * @param \Twig_Loader_Chain $twigLoader
49    * @param array $templates
50    */
51   public function __construct(SymfonyProfiler $profiler, \Twig_Environment $twig, \Twig_Loader_Chain $twigLoader, array $templates) {
52     $this->profiler = $profiler;
53     $this->twig = $twig;
54     $this->twigLoader = $twigLoader;
55     $this->templates = $templates;
56   }
57
58   /**
59    * Gets the template name for a given panel.
60    *
61    * @param Profile $profile
62    * @param string $panel
63    *
64    * @return mixed
65    *
66    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
67    */
68   public function getName(Profile $profile, $panel) {
69     $templates = $this->getNames($profile);
70
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));
73     }
74
75     return $templates[$panel];
76   }
77
78   /**
79    * Gets the templates for a given profile.
80    *
81    * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
82    *
83    * @return array
84    */
85   public function getTemplates(Profile $profile) {
86     $templates = $this->getNames($profile);
87     foreach ($templates as $name => $template) {
88       $templates[$name] = $this->twig->loadTemplate($template);
89     }
90
91     return $templates;
92   }
93
94   /**
95    * Gets template names of templates that are present in the viewed profile.
96    *
97    * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
98    *
99    * @return array
100    *
101    * @throws \UnexpectedValueException
102    */
103   protected function getNames(Profile $profile) {
104     $templates = [];
105
106     foreach ($this->templates as $arguments) {
107       if (NULL === $arguments) {
108         continue;
109       }
110
111       list($name, $template) = $arguments;
112
113       if (!$this->profiler->has($name) || !$profile->hasCollector($name)) {
114         continue;
115       }
116
117       if ('.html.twig' === substr($template, -10)) {
118         $template = substr($template, 0, -10);
119       }
120
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));
123       }
124
125       $templates[$name] = $template . '.html.twig';
126     }
127
128     return $templates;
129   }
130 }