/
TemplateManager.php
126 lines (104 loc) · 3.37 KB
/
TemplateManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\WebProfilerBundle\Profiler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Bundle\TwigBundle\TwigEngine;
/**
* Profiler Templates Manager
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Artur Wielogórski <wodor@wodor.net>
*/
class TemplateManager {
/**
* @var \Symfony\Bundle\TwigBundle\TwigEngine
*/
protected $templating;
/**
* @var \Twig_Environment
*/
protected $twig;
/**
* @var array
*/
protected $templates;
/**
* @var \Symfony\Component\HttpKernel\Profiler\Profiler
*/
protected $profiler;
/**
* @param \Symfony\Bundle\TwigBundle\TwigEngine $templating
* @param \Twig_Environment $twig
* @param array $templates
*/
public function __construct(Profiler $profiler, TwigEngine $templating, \Twig_Environment $twig, array $templates)
{
$this->profiler = $profiler;
$this->templating = $templating;
$this->twig = $twig;
$this->templates = $templates;
}
/**
* @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
* @param $panel
* @return mixed
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function getName(Profile $profile, $panel)
{
$templates = $this->getNames($profile);
if (!isset($templates[$panel])) {
throw new NotFoundHttpException(sprintf('Panel "%s" is not registered in profiler or is not present in viewed profile.', $panel));
}
return $templates[$panel];
}
/**
* @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
* @return array
*/
public function getTemplates(Profile $profile)
{
$templates = $this->getNames($profile);
foreach ($templates as $name => $template) {
$templates[$name] = $this->twig->loadTemplate($template);
}
return $templates;
}
/**
* Gets template names of templates that are
* present in the viewed profile
* @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
* @return array
* @throws \UnexpectedValueException
*/
protected function getNames(Profile $profile)
{
$templates = array();
foreach ($this->templates as $arguments) {
if (null === $arguments) {
continue;
}
list($name, $template) = $arguments;
if (!$this->profiler->has($name) || !$profile->hasCollector($name)) {
continue;
}
if ('.html.twig' === substr($template, -10)) {
$template = substr($template, 0, -10);
}
if (!$this->templating->exists($template.'.html.twig')) {
throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
}
$templates[$name] = $template.'.html.twig';
}
return $templates;
}
}