Completed
Push — develop ( 9c40cd...a29501 )
by Peter
01:44
created

Debugger::__construct()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
rs 8.4266
cc 7
nc 12
nop 1
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDebug/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDebug\Service;
11
12
use Tracy\Debugger as Tracy;
13
use WebinoDebug\Debugger\PanelInterface;
14
use WebinoDebug\Debugger\DebuggerBarInterface;
15
use WebinoDebug\Debugger\DebuggerDump;
16
use WebinoDebug\Debugger\DebuggerInterface;
17
use WebinoDebug\Debugger\DebuggerTimer;
18
use WebinoDebug\Options\DebuggerOptions;
19
use Zend\Stdlib\ArrayUtils;
20
21
/**
22
 * Class Debugger
23
 */
24
class Debugger implements
25
    DebuggerInterface,
26
    DebuggerBarInterface
27
{
28
    /**
29
     * @var DebuggerOptions
30
     */
31
    private $options;
32
33
    /**
34
     * @var array
35
     */
36
    protected $barPanels = [];
37
38
    /**
39
     * @var \WebinoDebug\Debugger\DebuggerTimer
40
     */
41
    protected $timerPrototype;
42
43
    /**
44
     * @param array|DebuggerOptions $options
45
     */
46
    public function __construct($options = null)
47
    {
48
        $_options = ($options instanceof DebuggerOptions)
49
            ? $options
50
            : new DebuggerOptions((array) $options);
51
52
        $this->options = $_options;
53
        if ($_options->isDisabled() || Tracy::isEnabled()) {
54
            return;
55
        }
56
57
        Tracy::$customCssFiles = $options->getCssFiles();
0 ignored issues
show
Bug introduced by
It seems like $options is not always an object, but can also be of type array|null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
58
        Tracy::$customJsFiles  = $options->getJsFiles();
59
60
        $showBar = $_options->showBar();
61
        if ($showBar) {
62
63
            $options->hasBarNoLogo()
64
                and Tracy::$customCssFiles[] = __DIR__ . '/../../../data/assets/Debugger/no-logo.css';
65
66
            $options->hasBarNoClose()
67
                and Tracy::$customCssFiles[] = __DIR__ . '/../../../data/assets/Debugger/no-close.css';
68
        }
69
70
        Tracy::$showBar    = $showBar;
71
        Tracy::$strictMode = $_options->isStrict();
72
        Tracy::$maxDepth   = $_options->getMaxDepth();
73
        Tracy::$maxLength  = $_options->getMaxLength();
74
75
        Tracy::enable(
76
            $_options->getMode(),
77
            $_options->getLog(),
78
            $_options->getEmail()
79
        );
80
    }
81
82
    /**
83
     * @return DebuggerOptions
84
     */
85
    public function getOptions()
86
    {
87
        return $this->options;
88
    }
89
90
    /**
91
     * @return PanelInterface[]
92
     */
93
    public function getBarPanels()
94
    {
95
        return $this->barPanels;
96
    }
97
98
    /**
99
     * @param string $id
100
     * @return PanelInterface|null
101
     */
102
    public function getBarPanel($id)
103
    {
104
        /** @var PanelInterface $panel */
105
        $panel = Tracy::getBar()->getPanel($id);
106
        return $panel;
107
    }
108
109
    /**
110
     * Set debugger bar panel
111
     *
112
     * @param object|PanelInterface|null $panel Panel object
113
     * @param string $id Panel id
114
     * @return $this
115
     */
116
    public function setBarPanel(PanelInterface $panel = null, $id = null)
117
    {
118
        $panel and Tracy::getBar()->addPanel($this->barPanels[$id] = $panel, $id);
119
        return $this;
120
    }
121
122
    /**
123
     * Set debugger bar info
124
     *
125
     * @param string $label Info label
126
     * @param string|int $value Info value
127
     * @return $this
128
     */
129
    public function setBarInfo(string $label, $value)
130
    {
131
        /** @var \Tracy\DefaultBarPanel $panel */
132
        $panel = $this->getBarPanel('Tracy:info');
133
        $panel and $panel->data = ArrayUtils::merge((array) $panel->data, [$label => (string) $value]);
134
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function dump($subject)
141
    {
142
        return (new DebuggerDump($subject));
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function timer($name = null)
149
    {
150
        return $this->createDebuggerTimer()->setName($name)->start();
151
    }
152
153
    /**
154
     * @return DebuggerTimer
155
     */
156
    private function createDebuggerTimer()
157
    {
158
        if (!$this->timerPrototype) {
159
            /** @var \WebinoDebug\Debugger\TimerPanel $timerPanel */
160
            $timerPanel = $this->getBarPanel('WebinoDebug:timer');
161
            $this->timerPrototype = new DebuggerTimer($timerPanel);
162
        }
163
        return clone $this->timerPrototype;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     * @deprecated use dump() instead, now it returns stringable object
169
     */
170
    public function dumpStr($subject)
171
    {
172
        return $this->dump($subject);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     * @deprecated use dump() instead, now it returns object
178
     */
179
    public function barDump($subject, $title = null, array $options = null)
180
    {
181
        return $this->dump($subject)->bar($title);
182
    }
183
}
184