Completed
Push — develop ( 782b4e...557e80 )
by Adrien
43:38
created

Logger::getWriteDebugLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;
4
5
class Logger
6
{
7
    /**
8
     * Flag to determine whether a debug log should be generated by the calculation engine
9
     *        If true, then a debug log will be generated
10
     *        If false, then a debug log will not be generated.
11
     *
12
     * @var bool
13
     */
14
    private $writeDebugLog = false;
15
16
    /**
17
     * Flag to determine whether a debug log should be echoed by the calculation engine
18
     *        If true, then a debug log will be echoed
19
     *        If false, then a debug log will not be echoed
20
     * A debug log can only be echoed if it is generated.
21
     *
22
     * @var bool
23
     */
24
    private $echoDebugLog = false;
25
26
    /**
27
     * The debug log generated by the calculation engine.
28
     *
29
     * @var string[]
30
     */
31
    private $debugLog = [];
32
33
    /**
34
     * The calculation engine cell reference stack.
35
     *
36
     * @var CyclicReferenceStack
37
     */
38
    private $cellStack;
39
40
    /**
41
     * Instantiate a Calculation engine logger.
42
     *
43
     * @param CyclicReferenceStack $stack
44
     */
45 134
    public function __construct(CyclicReferenceStack $stack)
46
    {
47 134
        $this->cellStack = $stack;
48 134
    }
49
50
    /**
51
     * Enable/Disable Calculation engine logging.
52
     *
53
     * @param bool $pValue
54
     */
55 60
    public function setWriteDebugLog($pValue)
56
    {
57 60
        $this->writeDebugLog = $pValue;
58 60
    }
59
60
    /**
61
     * Return whether calculation engine logging is enabled or disabled.
62
     *
63
     * @return bool
64
     */
65 143
    public function getWriteDebugLog()
66
    {
67 143
        return $this->writeDebugLog;
68
    }
69
70
    /**
71
     * Enable/Disable echoing of debug log information.
72
     *
73
     * @param bool $pValue
74
     */
75
    public function setEchoDebugLog($pValue)
76
    {
77
        $this->echoDebugLog = $pValue;
78
    }
79
80
    /**
81
     * Return whether echoing of debug log information is enabled or disabled.
82
     *
83
     * @return bool
84
     */
85
    public function getEchoDebugLog()
86
    {
87
        return $this->echoDebugLog;
88
    }
89
90
    /**
91
     * Write an entry to the calculation engine debug log.
92
     */
93 116
    public function writeDebugLog(...$args)
94
    {
95
        //    Only write the debug log if logging is enabled
96 116
        if ($this->writeDebugLog) {
97
            $message = implode($args);
98
            $cellReference = implode(' -> ', $this->cellStack->showStack());
99
            if ($this->echoDebugLog) {
100
                echo $cellReference,
101
                    ($this->cellStack->count() > 0 ? ' => ' : ''),
102
                    $message,
103
                    PHP_EOL;
104
            }
105
            $this->debugLog[] = $cellReference .
106
                ($this->cellStack->count() > 0 ? ' => ' : '') .
107
                $message;
108
        }
109 116
    }
110
111
    /**
112
     * Clear the calculation engine debug log.
113
     */
114 47
    public function clearLog()
115
    {
116 47
        $this->debugLog = [];
117 47
    }
118
119
    /**
120
     * Return the calculation engine debug log.
121
     *
122
     * @return string[]
123
     */
124
    public function getLog()
125
    {
126
        return $this->debugLog;
127
    }
128
}
129