1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the DebugTemplate class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Bundle\EzPublishDebugBundle\Twig; |
10
|
|
|
|
11
|
|
|
use Twig_Template; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Meant to be used as a Twig base template class. |
15
|
|
|
* |
16
|
|
|
* Wraps the display method to: |
17
|
|
|
* - Inject debug info into template to be able to see in the markup which one is used |
18
|
|
|
*/ |
19
|
|
|
class DebugTemplate extends Twig_Template |
20
|
|
|
{ |
21
|
|
|
public function display(array $context, array $blocks = array()) |
22
|
|
|
{ |
23
|
|
|
// Bufferize to be able to insert template name as HTML comments if applicable. |
24
|
|
|
// Layout template name will only appear at the end, to avoid potential quirks with old browsers |
25
|
|
|
// when comments appear before doctype declaration. |
26
|
|
|
ob_start(); |
27
|
|
|
parent::display($context, $blocks); |
28
|
|
|
$templateResult = ob_get_clean(); |
29
|
|
|
|
30
|
|
|
$templateName = $this->getTemplateName(); |
31
|
|
|
// Check if template name ends with "html.twig", indicating this is an HTML template. |
32
|
|
|
$isHtmlTemplate = substr($templateName, -strlen('html.twig')) === 'html.twig'; |
33
|
|
|
|
34
|
|
|
// Display start template comment, if applicable. |
35
|
|
|
if ($isHtmlTemplate) { |
36
|
|
|
if (stripos(trim($templateResult), '<!doctype') !== false) { |
37
|
|
|
$templateResult = preg_replace( |
38
|
|
|
'#(<!doctype[^>]+>)#im', |
39
|
|
|
"$1\n<!-- START " . $templateName . ' -->', |
40
|
|
|
$templateResult |
41
|
|
|
); |
42
|
|
|
} else { |
43
|
|
|
echo "\n<!-- START $templateName -->\n"; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Display stop template comment after result, if applicable. |
48
|
|
|
if ($isHtmlTemplate) { |
49
|
|
|
$bodyPos = stripos($templateResult, '</body>'); |
50
|
|
|
if ($bodyPos !== false) { |
51
|
|
|
// Add layout template name before </body>, to avoid display quirks in some browsers. |
52
|
|
|
echo substr($templateResult, 0, $bodyPos) |
53
|
|
|
. "\n<!-- STOP $templateName -->\n" |
54
|
|
|
. substr($templateResult, $bodyPos); |
55
|
|
|
} else { |
56
|
|
|
echo $templateResult; |
57
|
|
|
echo "\n<!-- STOP $templateName -->\n"; |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
echo $templateResult; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getTemplateName() |
68
|
|
|
{ |
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getSource() |
76
|
|
|
{ |
77
|
|
|
return ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
protected function doDisplay(array $context, array $blocks = array()) |
84
|
|
|
{ |
85
|
|
|
return ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getDebugInfo() |
92
|
|
|
{ |
93
|
|
|
return array(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|