Passed
Pull Request — master (#182)
by Arman
06:15 queued 03:14
created

Debugger::addToStore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Debugger;
16
17
use DebugBar\DataCollector\RequestDataCollector;
18
use DebugBar\DataCollector\TimeDataCollector;
19
use DebugBar\DataCollector\MessagesCollector;
20
use DebugBar\DataCollector\PhpInfoCollector;
21
use DebugBar\DataCollector\MemoryCollector;
22
use DebugBar\JavascriptRenderer;
23
use DebugBar\DebugBarException;
24
use DebugBar\DebugBar;
25
26
/**
27
 * Class Debugger
28
 * @package Quantum\Debugger
29
 * @uses DebugBar
30
 */
31
class Debugger
32
{
33
34
    /**
35
     * Messages tab
36
     */
37
    const MESSAGES = 'messages';
38
39
    /**
40
     * Queries tab
41
     */
42
    const QUERIES = 'queries';
43
44
    /**
45
     * Routes tab
46
     */
47
    const ROUTES = 'routes';
48
49
    /**
50
     * Hooks tab
51
     */
52
    const HOOKS = 'hooks';
53
54
    /**
55
     * Mails tab
56
     */
57
    const MAILS = 'mails';
58
59
    /**
60
     * Store
61
     * @var array
62
     */
63
    private $store;
64
65
    /**
66
     * @var Debugger
67
     */
68
    private static $instance;
69
70
    /**
71
     * DebugBar instance
72
     * @var DebugBar
73
     */
74
    private $debugBar;
75
76
    /**
77
     * Assets url
78
     * @var string
79
     */
80
    private $assetsUrl = '/assets/DebugBar/Resources';
81
82
    /**
83
     * Custom CSS
84
     * @var string
85
     */
86
    private $customCss = 'custom_debugbar.css';
87
88
    /**
89
     * Debugger constructor.
90
     * @param DebuggerStore $store
91
     * @param DebugBar $debugBar
92
     * @param array $collectors
93
     * @throws DebugBarException
94
     */
95
    public function __construct(DebuggerStore $store, DebugBar $debugBar, array $collectors = [])
96
    {
97
        $this->store = $store;
0 ignored issues
show
Documentation Bug introduced by
It seems like $store of type Quantum\Debugger\DebuggerStore is incompatible with the declared type array of property $store.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
        $this->debugBar = $debugBar;
99
100
        foreach ($collectors as $collector) {
101
            $this->debugBar->addCollector($collector);
102
        }
103
    }
104
105
    /**
106
     * @param DebuggerStore|null $store
107
     * @param DebugBar|null $debugBar
108
     * @param array|null $collectors
109
     * @return Debugger
110
     * @throws DebugBarException
111
     */
112
    public static function getInstance(DebuggerStore $store = null, DebugBar $debugBar = null, ?array $collectors = []): Debugger
113
    {
114
        if (self::$instance === null) {
115
            $debugBar = $debugBar ?? new DebugBar();
116
            $store = $store ?? new DebuggerStore();
117
            $collectors = $collectors ?: self::getDefaultCollectors();
118
119
            self::$instance = new self($store, $debugBar, $collectors);
120
        }
121
122
        return self::$instance;
123
    }
124
125
    /**
126
     * Checks if debug bar enabled
127
     * @return bool
128
     */
129
    public function isEnabled(): bool
130
    {
131
        return filter_var(config()->get('debug'), FILTER_VALIDATE_BOOLEAN);
132
    }
133
134
    /**
135
     * @return void
136
     */
137
    public function initStore()
138
    {
139
        $this->store->init([
140
            Debugger::MESSAGES,
141
            Debugger::QUERIES,
142
            Debugger::ROUTES,
143
            Debugger::HOOKS,
144
            Debugger::MAILS
145
        ]);
146
    }
147
148
    /**
149
     * Adds data to the store cell
150
     * @param string $cell
151
     * @param string $level
152
     * @param mixed $data
153
     */
154
    public function addToStoreCell(string $cell, string $level, $data)
155
    {
156
        if (!empty($data)) {
157
            $this->store->set($cell, [$level => $data]);
158
        }
159
    }
160
161
    /**
162
     * @param string $cell
163
     * @return array
164
     */
165
    public function getStoreCell(string $cell): array
166
    {
167
        return $this->store->get($cell);
168
    }
169
170
    /**
171
     * Clears the store cell
172
     * @param string $cell
173
     */
174
    public function clearStoreCell(string $cell)
175
    {
176
        $this->store->delete($cell);
177
    }
178
179
    /**
180
     * @return void
181
     */
182
    public function resetStore()
183
    {
184
        $this->store->flush();
185
    }
186
187
    /**
188
     * Renders the debug bar
189
     * @return string
190
     * @throws DebugBarException
191
     */
192
    public function render(): string
193
    {
194
        foreach ([self::MESSAGES, self::QUERIES, self::ROUTES, self::HOOKS, self::MAILS] as $tab) {
195
            $this->createTab($tab);
196
        }
197
198
        $renderer = $this->getRenderer();
199
200
        return $renderer->renderHead() . $renderer->render();
201
    }
202
203
    /**
204
     * Creates a tab
205
     * @param string $type
206
     * @throws DebugBarException
207
     */
208
    protected function createTab(string $type)
209
    {
210
        if (!$this->debugBar->hasCollector($type)) {
211
            $this->debugBar->addCollector(new MessagesCollector($type));
212
        }
213
214
        $messages = $this->store->get($type);
215
216
        if (count($messages)) {
217
            foreach ($messages as $message) {
218
                $fn = key($message);
219
                $this->debugBar[$type]->$fn($message[$fn]);
220
            }
221
        }
222
    }
223
224
    /**
225
     * Gets the renderer
226
     * @return JavascriptRenderer
227
     */
228
    protected function getRenderer(): JavascriptRenderer
229
    {
230
        return $this->debugBar
231
            ->getJavascriptRenderer()
232
            ->setBaseUrl(base_url() . $this->assetsUrl)
233
            ->addAssets([$this->customCss], []);
234
    }
235
236
    /**
237
     * @return array
238
     */
239
    protected static function getDefaultCollectors(): array
240
    {
241
        return [
242
            new PhpInfoCollector(),
243
            new MessagesCollector(),
244
            new RequestDataCollector(),
245
            new TimeDataCollector(),
246
            new MemoryCollector(),
247
        ];
248
    }
249
250
}
251