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 Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Twig_Template; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Meant to be used as a Twig base template class. |
16
|
|
|
* |
17
|
|
|
* Wraps the display method to: |
18
|
|
|
* - Inject debug info into template to be able to see in the markup which one is used |
19
|
|
|
*/ |
20
|
|
|
class DebugTemplate extends Twig_Template |
21
|
|
|
{ |
22
|
|
|
private $fileSystem; |
23
|
|
|
|
24
|
|
|
public function display(array $context, array $blocks = array()) |
25
|
|
|
{ |
26
|
|
|
$this->fileSystem = $this->fileSystem ?: new Filesystem(); |
27
|
|
|
|
28
|
|
|
// Bufferize to be able to insert template name as HTML comments if applicable. |
29
|
|
|
// Layout template name will only appear at the end, to avoid potential quirks with old browsers |
30
|
|
|
// when comments appear before doctype declaration. |
31
|
|
|
ob_start(); |
32
|
|
|
parent::display($context, $blocks); |
33
|
|
|
$templateResult = ob_get_clean(); |
34
|
|
|
|
35
|
|
|
$templateName = trim($this->fileSystem->makePathRelative($this->getSourceContext()->getPath(), dirname(getcwd())), '/'); |
36
|
|
|
// Check if template name ends with "html.twig", indicating this is an HTML template. |
37
|
|
|
$isHtmlTemplate = substr($templateName, -strlen('html.twig')) === 'html.twig'; |
38
|
|
|
$templateName = $isHtmlTemplate ? $templateName . ' (' . $this->getSourceContext()->getName() . ')' : $templateName; |
39
|
|
|
|
40
|
|
|
// Display start template comment, if applicable. |
41
|
|
|
if ($isHtmlTemplate) { |
42
|
|
|
if (stripos(trim($templateResult), '<!doctype') !== false) { |
43
|
|
|
$templateResult = preg_replace( |
44
|
|
|
'#(<!doctype[^>]+>)#im', |
45
|
|
|
"$1\n<!-- START " . $templateName . ' -->', |
46
|
|
|
$templateResult |
47
|
|
|
); |
48
|
|
|
} else { |
49
|
|
|
echo "\n<!-- START $templateName -->\n"; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Display stop template comment after result, if applicable. |
54
|
|
|
if ($isHtmlTemplate) { |
55
|
|
|
$bodyPos = stripos($templateResult, '</body>'); |
56
|
|
|
if ($bodyPos !== false) { |
57
|
|
|
// Add layout template name before </body>, to avoid display quirks in some browsers. |
58
|
|
|
echo substr($templateResult, 0, $bodyPos) |
59
|
|
|
. "\n<!-- STOP $templateName -->\n" |
60
|
|
|
. substr($templateResult, $bodyPos); |
61
|
|
|
} else { |
62
|
|
|
echo $templateResult; |
63
|
|
|
echo "\n<!-- STOP $templateName -->\n"; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
echo $templateResult; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getTemplateName() |
74
|
|
|
{ |
75
|
|
|
return ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getSource() |
82
|
|
|
{ |
83
|
|
|
return ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
protected function doDisplay(array $context, array $blocks = array()) |
90
|
|
|
{ |
91
|
|
|
return ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getDebugInfo() |
98
|
|
|
{ |
99
|
|
|
return array(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|