forked from lolautruche/EzCoreExtraBundle
/
DebugTemplate.php
124 lines (110 loc) · 3.68 KB
/
DebugTemplate.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
<?php
/*
* This file is part of the EzCoreExtraBundle package.
*
* (c) Jérôme Vieilledent <jerome@vieilledent.fr>
* (c) eZ Systems AS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Lolautruche\EzCoreExtraBundle\Templating\Twig;
use Twig_Template;
/**
* Meant to be used as a Twig base template class.
*
* Wraps the display method to:
* - Inject debug info into template to be able to see in the markup which one is used
*
* Mainly copy/paste of eZ\Bundle\EzPublishDebugBundle\Twig\DebugTemplate, courtesy of eZ Systems AS.
* Adds mapping between template name and path, to display actual used template source when using themes.
*/
class DebugTemplate extends Twig_Template
{
/**
* Map between template names and associated paths.
*
* @var array
*/
protected static $templatePathMap = [];
public static function addPathMapping($templateName, $path)
{
static::$templatePathMap[$templateName] = $path;
}
public static function getTemplatePath($templateName)
{
return isset(static::$templatePathMap[$templateName]) ? static::$templatePathMap[$templateName] : null;
}
public function display(array $context, array $blocks = array())
{
// Bufferize to be able to insert template name as HTML comments if applicable.
// Layout template name will only appear at the end, to avoid potential quirks with old browsers
// when comments appear before doctype declaration.
ob_start();
parent::display($context, $blocks);
$templateResult = ob_get_clean();
$templateName = $this->getTemplateName();
if ($templatePath = static::getTemplatePath($templateName)) {
$templateFullName = sprintf('%s (%s)', $templateName, $templatePath);
} else {
$templateFullName = $templateName;
}
// Check if template name ends with "html.twig", indicating this is an HTML template.
$isHtmlTemplate = substr($templateName, -strlen('html.twig')) === 'html.twig';
// Display start template comment, if applicable.
if ($isHtmlTemplate) {
if (stripos(trim($templateResult), '<!doctype') !== false) {
$templateResult = preg_replace(
'#(<!doctype[^>]+>)#im',
"$1\n<!-- START " . $templateFullName . ' -->',
$templateResult
);
} else {
echo "\n<!-- START $templateFullName -->\n";
}
}
// Display stop template comment after result, if applicable.
if ($isHtmlTemplate) {
$bodyPos = stripos($templateResult, '</body>');
if ($bodyPos !== false) {
// Add layout template name before </body>, to avoid display quirks in some browsers.
echo substr($templateResult, 0, $bodyPos)
. "\n<!-- STOP $templateFullName -->\n"
. substr($templateResult, $bodyPos);
} else {
echo $templateResult;
echo "\n<!-- STOP $templateFullName -->\n";
}
} else {
echo $templateResult;
}
}
/**
* {@inheritdoc}
*/
public function getTemplateName()
{
return '';
}
/**
* {@inheritdoc}
*/
public function getSource()
{
return '';
}
/**
* {@inheritdoc}
*/
protected function doDisplay(array $context, array $blocks = array())
{
return '';
}
/**
* {@inheritdoc}
*/
public function getDebugInfo()
{
return array();
}
}