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