Completed
Push — master ( 2bd0d9...3d8325 )
by recca
05:52
created

DebuggerManager::renderBar()   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 1
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 Recca0120\LaravelTracy;
4
5
use Closure;
6
use Exception;
7
use Tracy\Bar;
8
use Tracy\Helpers;
9
use ErrorException;
10
use Tracy\Debugger;
11
use Tracy\BlueScreen;
12
use Illuminate\Support\Arr;
13
use Illuminate\Contracts\Routing\UrlGenerator;
14
15
class DebuggerManager
16
{
17
    /**
18
     * $config.
19
     *
20
     * @var array
21
     */
22
    protected $config;
23
24
    /**
25
     * $bar.
26
     *
27
     * @var \Tracy\Bar
28
     */
29
    protected $bar;
30
31
    /**
32
     * $blueScreen.
33
     *
34
     * @var \Tracy\BlueScreen
35
     */
36
    protected $blueScreen;
37
38
    /**
39
     * $urlGenerator.
40
     *
41
     * @var \Illuminate\Contracts\Routing\UrlGenerator
42
     */
43
    protected $urlGenerator;
44
45
    /**
46
     * __construct.
47
     *
48
     * @param array $config
49
     * @param \Tracy\Bar $bar
50
     * @param \Tracy\BlueScreen $blueScreen
51
     */
52 13
    public function __construct($config = [], Bar $bar = null, BlueScreen $blueScreen = null, Session $session = null)
53
    {
54 13
        $this->config = $config;
55 13
        $this->bar = $bar ?: Debugger::getBar();
56 13
        $this->blueScreen = $blueScreen ?: Debugger::getBlueScreen();
57 13
        $this->session = $session ?: new Session;
0 ignored issues
show
Bug introduced by
The property session does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58 13
    }
59
60
    /**
61
     * init.
62
     *
63
     * @param array $config
64
     * @return array
65
     */
66 3
    public static function init($config = [])
67
    {
68 3
        $config = array_merge([
69 3
            'accepts' => [],
70 3
            'appendTo' => 'body',
71
            'showBar' => false,
72 3
            'editor' => Debugger::$editor,
73 3
            'maxDepth' => Debugger::$maxDepth,
74 3
            'maxLength' => Debugger::$maxLength,
75
            'scream' => true,
76
            'showLocation' => true,
77
            'strictMode' => true,
78 3
            'currentTime' => $_SERVER['REQUEST_TIME_FLOAT'] ?: microtime(true),
79 3
            'editorMapping' => isset(Debugger::$editorMapping) === true ? Debugger::$editorMapping : [],
80 3
        ], $config);
81
82 3
        Debugger::$editor = $config['editor'];
83 3
        Debugger::$maxDepth = $config['maxDepth'];
84 3
        Debugger::$maxLength = $config['maxLength'];
85 3
        Debugger::$scream = $config['scream'];
86 3
        Debugger::$showLocation = $config['showLocation'];
87 3
        Debugger::$strictMode = $config['strictMode'];
88 3
        Debugger::$time = $config['currentTime'];
89
90 3
        if (isset(Debugger::$editorMapping) === true) {
91 3
            Debugger::$editorMapping = $config['editorMapping'];
92
        }
93
94 3
        return $config;
95
    }
96
97
    /**
98
     * enabled.
99
     *
100
     * @return bool
101
     */
102 1
    public function enabled()
103
    {
104 1
        return Arr::get($this->config, 'enabled', true) === true;
105
    }
106
107
    /**
108
     * showBar.
109
     *
110
     * @return bool
111
     */
112 1
    public function showBar()
113
    {
114 1
        return Arr::get($this->config, 'showBar', true) === true;
115
    }
116
117
    /**
118
     * accepts.
119
     *
120
     * @return array
121
     */
122 1
    public function accepts()
123
    {
124 1
        return Arr::get($this->config, 'accepts', []);
125
    }
126
127
    /**
128
     * setUrlGenerator.
129
     *
130
     * @param \Illuminate\Contracts\Routing\UrlGenerator $urlGenerator
131
     * @return $this
132
     */
133 2
    public function setUrlGenerator(UrlGenerator $urlGenerator)
134
    {
135 2
        $this->urlGenerator = $urlGenerator;
136
137 2
        return $this;
138
    }
139
140
    /**
141
     * dispatchAssets.
142
     *
143
     * @param string $type
144
     * @return array
145
     */
146 3
    public function dispatchAssets($type)
147
    {
148
        switch ($type) {
149 3
            case 'css':
150 2
            case 'js':
151
                $headers = [
152 2
                    'Content-Type' => $type === 'css' ? 'text/css; charset=utf-8' : 'text/javascript; charset=utf-8',
153 2
                    'Cache-Control' => 'max-age=86400',
154
                ];
155 2
                $content = $this->renderBuffer(function () {
156 2
                    return $this->bar->dispatchAssets();
157 2
                });
158 2
                break;
159
            default:
160
                $headers = [
161 1
                    'Content-Type' => 'text/javascript; charset=utf-8',
162
                ];
163 1
                $content = $this->dispatch();
164 1
                break;
165
        }
166
167
        return [
168 3
            array_merge($headers, [
169 3
                'Content-Length' => strlen($content),
170
            ]),
171 3
            $content,
172
        ];
173
    }
174
175
    /**
176
     * dispatch.
177
     *
178
     * @return string
179
     */
180 1
    public function dispatch()
181
    {
182 1
        if ($this->session->isStarted() === false) {
183 1
            $this->session->start();
184
        }
185
186 1
        return $this->renderBuffer(function () {
187 1
            return $this->bar->dispatchAssets();
188 1
        });
189
    }
190
191
    /**
192
     * shutdownHandler.
193
     *
194
     * @param string $content
195
     * @return string
196
     */
197 5
    public function shutdownHandler($content, $error = null)
198
    {
199 5
        $error = $error ?: error_get_last();
200 5
        if (in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE, E_RECOVERABLE_ERROR, E_USER_ERROR], true)) {
201 1
            return $this->exceptionHandler(
202 1
                Helpers::fixStack(
203 1
                    new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])
204
                )
205
            );
206
        }
207
208 4
        return $this->renderBar(
209 4
            $this->renderLoader($content)
210
        );
211
    }
212
213
    /**
214
     * exceptionHandler.
215
     *
216
     * @param \Exception $exception
217
     * @return string
218
     */
219
    public function exceptionHandler(Exception $exception)
220
    {
221 2
        return $this->renderBuffer(function () use ($exception) {
222 2
            Helpers::improveException($exception);
223 2
            $this->blueScreen->render($exception);
224 2
        });
225
    }
226
227
    /**
228
     * renderLoader.
229
     *
230
     * @param string $content
231
     * @return string
232
     */
233 4
    protected function renderLoader($content)
234
    {
235 4
        if ($this->session->isStarted() === false) {
236 1
            return $content;
237
        }
238
239 3
        return $this->render($content, 'renderLoader', 'head');
240
    }
241
242
    /**
243
     * renderBar.
244
     *
245
     * @param string $content
246
     * @return string
247
     */
248 4
    protected function renderBar($content)
249
    {
250 4
        return $this->render($content, 'render', Arr::get($this->config, 'appendTo', 'body'));
251
    }
252
253
    /**
254
     * render.
255
     *
256
     * @param string $content
257
     * @param string $method
258
     * @param string $appendTo
259
     * @return string
260
     */
261
    protected function render($content, $method, $appendTo = 'body')
262
    {
263 4
        $html = $this->renderBuffer(function () use ($method) {
264 4
            call_user_func([$this->bar, $method]);
265 4
        });
266
267 4
        $pos = strripos($content, '</'.$appendTo.'>');
268
269 4
        return $pos !== false
270 4
            ? substr($content, 0, $pos).$html.substr($content, $pos)
271 4
            : $content.$html;
272
    }
273
274
    /**
275
     * renderBuffer.
276
     *
277
     * @param \Closure $callback
278
     * @return string
279
     */
280 9
    protected function renderBuffer(Closure $callback)
281
    {
282 9
        ob_start();
283 9
        $callback();
284
285 9
        return $this->replacePath(ob_get_clean());
286
    }
287
288
    /**
289
     * replacePath.
290
     *
291
     * @param string $content
292
     * @return string
293
     */
294 9
    protected function replacePath($content)
295
    {
296 9
        $path = is_null($this->urlGenerator) === false
297 1
            ? $this->urlGenerator->route(Arr::get($this->config, 'route.as').'bar')
298 9
            : null;
299
300 9
        return empty($path) === false
301 1
            ? str_replace('?_tracy_bar', $path.'?_tracy_bar', $content)
302 9
            : $content;
303
    }
304
}
305