Completed
Push — 6.7 ( 435624...83fe1a )
by André
30:08 queued 17:07
created

DebugTemplate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B display() 0 42 5
A getTemplateName() 0 4 1
A getSource() 0 4 1
A doDisplay() 0 4 1
A getDebugInfo() 0 4 1
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