Completed
Pull Request — master (#270)
by Marc
02:19
created

NodeVisualizer::outputLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TYPO3Fluid\Fluid\Utility;
4
5
/*
6
 * This file belongs to the package "TYPO3 Fluid".
7
 * See LICENSE.txt that was shipped with this package.
8
 */
9
10
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\BooleanNode;
11
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\EscapingNode;
12
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface;
13
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ObjectAccessorNode;
14
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
15
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
16
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
17
18
/**
19
 * Root node of every syntax tree.
20
 */
21
class NodeVisualizer
22
{
23
    /**
24
     * @param NodeInterface $node
25
     * @param $renderingContext
26
     * @param int $depth
27
     */
28
    public static function visualize($node, $renderingContext, $depth = 0)
29
    {
30
        if (stristr(get_class($node), 'Mock_')) {
31
            return;
32
        }
33 View Code Duplication
        if ($depth == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
            echo chr(10) . str_repeat('-', 120) . chr(10);
35
        }
36
37
        echo str_repeat(' ', $depth * 2);
38
        self::output(self::nodeType($node), 'cyan');
39
        self::output(' (escaping: "' . ($node->isEscapeOutputEnabled() ? 'enabled' : 'disabled') . '")', 'green');
40
41
        switch (true) {
42
            case $node instanceof ViewHelperNode:
43
                self::output(' (viewHelper: "' . $node->getViewHelperClassName() . '")', 'green');
44
                break;
45
            case $node instanceof ObjectAccessorNode:
46
                self::output(' (objectPath: "' . $node->getObjectPath() . '")', 'green');
47
                break;
48
            case $node instanceof BooleanNode:
49
                self::output(' (stack: "' . $node->reconcatenateExpression($node->getStack()) . '")', 'green');
50
                break;
51
        }
52
53
        self::outputLine();
54
55
        foreach ($node->getChildNodes() as $childNode) {
56
            self::visualize($childNode, $renderingContext, $depth + 1);
57
        }
58
59
        switch (true) {
60
            case $node instanceof EscapingNode:
61
                self::visualize($node->getNode(), $renderingContext, $depth + 1);
62
                break;
63
            case $node instanceof ViewHelperNode:
64
                foreach ($node->getArguments() as $name => $argumentNode) {
65
                    echo str_repeat(' ', ($depth + 1) * 2);
66
                    self::outputLine($name . ': ', 'red');
67
                    self::visualize($argumentNode, $renderingContext, $depth + 2);
68
                }
69
                break;
70
        }
71
72 View Code Duplication
        if ($depth == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            echo str_repeat('-', 120) . chr(10) . chr(10);
74
        }
75
    }
76
77
    public static function nodeType($node)
78
    {
79
        $className = get_class($node);
80
81
        return str_replace('TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\\', '', $className);
82
    }
83
84
    public static function output($text = null, $color = null, $background = null)
85
    {
86
        if ($text === null) {
87
            return;
88
        }
89
        $colors = [
90
            'black' => '0;30',
91
            'dark_gray' => '1;30',
92
            'blue' => '0;34',
93
            'light_blue' => '1;34',
94
            'green' => '0;32',
95
            'light_green' => '1;32',
96
            'cyan' => '0;36',
97
            'light_cyan' => '1;36',
98
            'red' => '0;31',
99
            'light_red' => '1;31',
100
            'purple' => '0;35',
101
            'light_purple' => '1;35',
102
            'brown' => '0;33',
103
            'yellow' => '1;33',
104
            'light_gray' => '0;37',
105
            'white' => '1;37',
106
        ];
107
        $backgrounds = [
108
            'black' => '40',
109
            'red' => '41',
110
            'green' => '42',
111
            'yellow' => '43',
112
            'blue' => '44',
113
            'magenta' => '45',
114
            'cyan' => '46',
115
            'light_gray' => '47',
116
        ];
117
        $output = '';
118
119
        if (isset($colors[$color])) {
120
            $output .= "\033[" . $colors[$color] . "m";
121
        }
122
        // Check if given background color found
123
        if (isset($backgrounds[$background])) {
124
            $output .= "\033[" . $backgrounds[$background] . "m";
125
        }
126
127
        $output .= $text . "\033[0m";
128
129
        echo $output;
130
    }
131
132
    public static function outputLine($text = null, $color = null, $background = null)
133
    {
134
        self::output($text, $color, $background);
135
        echo chr(10);
136
    }
137
}
138