• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 11878724576

17 Nov 2024 12:12PM UTC coverage: 84.428% (+0.001%) from 84.427%
11878724576

push

github

web-flow
fix: code issues after merging develop (#9284)

* cs-fix

* fix services call

* apply rector rules

3 of 3 new or added lines in 2 files covered. (100.0%)

361 existing lines in 20 files now uncovered.

20597 of 24396 relevant lines covered (84.43%)

189.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.04
/system/Debug/Toolbar.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Debug;
15

16
use CodeIgniter\CodeIgniter;
17
use CodeIgniter\Debug\Toolbar\Collectors\BaseCollector;
18
use CodeIgniter\Debug\Toolbar\Collectors\Config;
19
use CodeIgniter\Debug\Toolbar\Collectors\History;
20
use CodeIgniter\Format\JSONFormatter;
21
use CodeIgniter\Format\XMLFormatter;
22
use CodeIgniter\HTTP\DownloadResponse;
23
use CodeIgniter\HTTP\Header;
24
use CodeIgniter\HTTP\IncomingRequest;
25
use CodeIgniter\HTTP\RequestInterface;
26
use CodeIgniter\HTTP\ResponseInterface;
27
use CodeIgniter\I18n\Time;
28
use Config\Toolbar as ToolbarConfig;
29
use Kint\Kint;
30

31
/**
32
 * Displays a toolbar with bits of stats to aid a developer in debugging.
33
 *
34
 * Inspiration: http://prophiler.fabfuel.de
35
 */
36
class Toolbar
37
{
38
    /**
39
     * Toolbar configuration settings.
40
     *
41
     * @var ToolbarConfig
42
     */
43
    protected $config;
44

45
    /**
46
     * Collectors to be used and displayed.
47
     *
48
     * @var list<BaseCollector>
49
     */
50
    protected $collectors = [];
51

52
    public function __construct(ToolbarConfig $config)
53
    {
54
        $this->config = $config;
86✔
55

56
        foreach ($config->collectors as $collector) {
86✔
57
            if (! class_exists($collector)) {
86✔
UNCOV
58
                log_message(
×
59
                    'critical',
×
60
                    'Toolbar collector does not exist (' . $collector . ').'
×
61
                    . ' Please check $collectors in the app/Config/Toolbar.php file.'
×
62
                );
×
63

UNCOV
64
                continue;
×
65
            }
66

67
            $this->collectors[] = new $collector();
86✔
68
        }
69
    }
70

71
    /**
72
     * Returns all the data required by Debug Bar
73
     *
74
     * @param float           $startTime App start time
75
     * @param IncomingRequest $request
76
     *
77
     * @return string JSON encoded data
78
     */
79
    public function run(float $startTime, float $totalTime, RequestInterface $request, ResponseInterface $response): string
80
    {
UNCOV
81
        $data = [];
×
82
        // Data items used within the view.
UNCOV
83
        $data['url']             = current_url();
×
84
        $data['method']          = $request->getMethod();
×
85
        $data['isAJAX']          = $request->isAJAX();
×
86
        $data['startTime']       = $startTime;
×
87
        $data['totalTime']       = $totalTime * 1000;
×
88
        $data['totalMemory']     = number_format(memory_get_peak_usage() / 1024 / 1024, 3);
×
89
        $data['segmentDuration'] = $this->roundTo($data['totalTime'] / 7);
×
90
        $data['segmentCount']    = (int) ceil($data['totalTime'] / $data['segmentDuration']);
×
91
        $data['CI_VERSION']      = CodeIgniter::CI_VERSION;
×
92
        $data['collectors']      = [];
×
93

UNCOV
94
        foreach ($this->collectors as $collector) {
×
95
            $data['collectors'][] = $collector->getAsArray();
×
96
        }
97

UNCOV
98
        foreach ($this->collectVarData() as $heading => $items) {
×
99
            $varData = [];
×
100

UNCOV
101
            if (is_array($items)) {
×
102
                foreach ($items as $key => $value) {
×
103
                    if (is_string($value)) {
×
104
                        $varData[esc($key)] = esc($value);
×
105
                    } else {
UNCOV
106
                        $oldKintMode       = Kint::$mode_default;
×
107
                        $oldKintCalledFrom = Kint::$display_called_from;
×
108

UNCOV
109
                        Kint::$mode_default        = Kint::MODE_RICH;
×
110
                        Kint::$display_called_from = false;
×
111

UNCOV
112
                        $kint = @Kint::dump($value);
×
113
                        $kint = substr($kint, strpos($kint, '</style>') + 8);
×
114

UNCOV
115
                        Kint::$mode_default        = $oldKintMode;
×
116
                        Kint::$display_called_from = $oldKintCalledFrom;
×
117

UNCOV
118
                        $varData[esc($key)] = $kint;
×
119
                    }
120
                }
121
            }
122

UNCOV
123
            $data['vars']['varData'][esc($heading)] = $varData;
×
124
        }
125

UNCOV
126
        if (isset($_SESSION)) {
×
127
            foreach ($_SESSION as $key => $value) {
×
128
                // Replace the binary data with string to avoid json_encode failure.
UNCOV
129
                if (is_string($value) && preg_match('~[^\x20-\x7E\t\r\n]~', $value)) {
×
130
                    $value = 'binary data';
×
131
                }
132

UNCOV
133
                $data['vars']['session'][esc($key)] = is_string($value) ? esc($value) : '<pre>' . esc(print_r($value, true)) . '</pre>';
×
134
            }
135
        }
136

UNCOV
137
        foreach ($request->getGet() as $name => $value) {
×
138
            $data['vars']['get'][esc($name)] = is_array($value) ? '<pre>' . esc(print_r($value, true)) . '</pre>' : esc($value);
×
139
        }
140

UNCOV
141
        foreach ($request->getPost() as $name => $value) {
×
142
            $data['vars']['post'][esc($name)] = is_array($value) ? '<pre>' . esc(print_r($value, true)) . '</pre>' : esc($value);
×
143
        }
144

UNCOV
145
        foreach ($request->headers() as $name => $value) {
×
146
            if ($value instanceof Header) {
×
147
                $data['vars']['headers'][esc($name)] = esc($value->getValueLine());
×
148
            } else {
UNCOV
149
                foreach ($value as $i => $header) {
×
150
                    $index = $i + 1;
×
151
                    $data['vars']['headers'][esc($name)] ??= '';
×
152
                    $data['vars']['headers'][esc($name)] .= ' (' . $index . ') '
×
153
                        . esc($header->getValueLine());
×
154
                }
155
            }
156
        }
157

UNCOV
158
        foreach ($request->getCookie() as $name => $value) {
×
159
            $data['vars']['cookies'][esc($name)] = esc($value);
×
160
        }
161

UNCOV
162
        $data['vars']['request'] = ($request->isSecure() ? 'HTTPS' : 'HTTP') . '/' . $request->getProtocolVersion();
×
163

UNCOV
164
        $data['vars']['response'] = [
×
165
            'statusCode'  => $response->getStatusCode(),
×
166
            'reason'      => esc($response->getReasonPhrase()),
×
167
            'contentType' => esc($response->getHeaderLine('content-type')),
×
168
            'headers'     => [],
×
169
        ];
×
170

UNCOV
171
        foreach ($response->headers() as $name => $value) {
×
172
            if ($value instanceof Header) {
×
173
                $data['vars']['response']['headers'][esc($name)] = esc($value->getValueLine());
×
174
            } else {
UNCOV
175
                foreach ($value as $i => $header) {
×
176
                    $index = $i + 1;
×
177
                    $data['vars']['response']['headers'][esc($name)] ??= '';
×
178
                    $data['vars']['response']['headers'][esc($name)] .= ' (' . $index . ') '
×
179
                        . esc($header->getValueLine());
×
180
                }
181
            }
182
        }
183

UNCOV
184
        $data['config'] = Config::display();
×
185

UNCOV
186
        $response->getCSP()->addImageSrc('data:');
×
187

UNCOV
188
        return json_encode($data);
×
189
    }
190

191
    /**
192
     * Called within the view to display the timeline itself.
193
     */
194
    protected function renderTimeline(array $collectors, float $startTime, int $segmentCount, int $segmentDuration, array &$styles): string
195
    {
UNCOV
196
        $rows       = $this->collectTimelineData($collectors);
×
197
        $styleCount = 0;
×
198

199
        // Use recursive render function
UNCOV
200
        return $this->renderTimelineRecursive($rows, $startTime, $segmentCount, $segmentDuration, $styles, $styleCount);
×
201
    }
202

203
    /**
204
     * Recursively renders timeline elements and their children.
205
     */
206
    protected function renderTimelineRecursive(array $rows, float $startTime, int $segmentCount, int $segmentDuration, array &$styles, int &$styleCount, int $level = 0, bool $isChild = false): string
207
    {
UNCOV
208
        $displayTime = $segmentCount * $segmentDuration;
×
209

UNCOV
210
        $output = '';
×
211

UNCOV
212
        foreach ($rows as $row) {
×
213
            $hasChildren = isset($row['children']) && ! empty($row['children']);
×
214
            $isQuery     = isset($row['query']) && ! empty($row['query']);
×
215

216
            // Open controller timeline by default
UNCOV
217
            $open = $row['name'] === 'Controller';
×
218

UNCOV
219
            if ($hasChildren || $isQuery) {
×
220
                $output .= '<tr class="timeline-parent' . ($open ? ' timeline-parent-open' : '') . '" id="timeline-' . $styleCount . '_parent" data-toggle="childrows" data-child="timeline-' . $styleCount . '">';
×
221
            } else {
UNCOV
222
                $output .= '<tr>';
×
223
            }
224

UNCOV
225
            $output .= '<td class="' . ($isChild ? 'debug-bar-width30' : '') . ' debug-bar-level-' . $level . '" >' . ($hasChildren || $isQuery ? '<nav></nav>' : '') . $row['name'] . '</td>';
×
226
            $output .= '<td class="' . ($isChild ? 'debug-bar-width10' : '') . '">' . $row['component'] . '</td>';
×
227
            $output .= '<td class="' . ($isChild ? 'debug-bar-width10 ' : '') . 'debug-bar-alignRight">' . number_format($row['duration'] * 1000, 2) . ' ms</td>';
×
228
            $output .= "<td class='debug-bar-noverflow' colspan='{$segmentCount}'>";
×
229

UNCOV
230
            $offset = ((((float) $row['start'] - $startTime) * 1000) / $displayTime) * 100;
×
231
            $length = (((float) $row['duration'] * 1000) / $displayTime) * 100;
×
232

UNCOV
233
            $styles['debug-bar-timeline-' . $styleCount] = "left: {$offset}%; width: {$length}%;";
×
234

UNCOV
235
            $output .= "<span class='timer debug-bar-timeline-{$styleCount}' title='" . number_format($length, 2) . "%'></span>";
×
236
            $output .= '</td>';
×
237
            $output .= '</tr>';
×
238

UNCOV
239
            $styleCount++;
×
240

241
            // Add children if any
UNCOV
242
            if ($hasChildren || $isQuery) {
×
243
                $output .= '<tr class="child-row ' . ($open ? '' : ' debug-bar-ndisplay') . '" id="timeline-' . ($styleCount - 1) . '_children" >';
×
244
                $output .= '<td colspan="' . ($segmentCount + 3) . '" class="child-container">';
×
245
                $output .= '<table class="timeline">';
×
246
                $output .= '<tbody>';
×
247

UNCOV
248
                if ($isQuery) {
×
249
                    // Output query string if query
UNCOV
250
                    $output .= '<tr>';
×
251
                    $output .= '<td class="query-container debug-bar-level-' . ($level + 1) . '" >' . $row['query'] . '</td>';
×
252
                    $output .= '</tr>';
×
253
                } else {
254
                    // Recursively render children
UNCOV
255
                    $output .= $this->renderTimelineRecursive($row['children'], $startTime, $segmentCount, $segmentDuration, $styles, $styleCount, $level + 1, true);
×
256
                }
257

UNCOV
258
                $output .= '</tbody>';
×
259
                $output .= '</table>';
×
260
                $output .= '</td>';
×
261
                $output .= '</tr>';
×
262
            }
263
        }
264

UNCOV
265
        return $output;
×
266
    }
267

268
    /**
269
     * Returns a sorted array of timeline data arrays from the collectors.
270
     *
271
     * @param array $collectors
272
     */
273
    protected function collectTimelineData($collectors): array
274
    {
UNCOV
275
        $data = [];
×
276

277
        // Collect it
UNCOV
278
        foreach ($collectors as $collector) {
×
279
            if (! $collector['hasTimelineData']) {
×
280
                continue;
×
281
            }
282

UNCOV
283
            $data = array_merge($data, $collector['timelineData']);
×
284
        }
285

286
        // Sort it
UNCOV
287
        $sortArray = [
×
288
            array_column($data, 'start'), SORT_NUMERIC, SORT_ASC,
×
289
            array_column($data, 'duration'), SORT_NUMERIC, SORT_DESC,
×
290
            &$data,
×
291
        ];
×
292

UNCOV
293
        array_multisort(...$sortArray);
×
294

295
        // Add end time to each element
UNCOV
296
        array_walk($data, static function (&$row): void {
×
297
            $row['end'] = $row['start'] + $row['duration'];
×
298
        });
×
299

300
        // Group it
UNCOV
301
        $data = $this->structureTimelineData($data);
×
302

UNCOV
303
        return $data;
×
304
    }
305

306
    /**
307
     * Arranges the already sorted timeline data into a parent => child structure.
308
     */
309
    protected function structureTimelineData(array $elements): array
310
    {
311
        // We define ourselves as the first element of the array
UNCOV
312
        $element = array_shift($elements);
×
313

314
        // If we have children behind us, collect and attach them to us
UNCOV
315
        while ($elements !== [] && $elements[array_key_first($elements)]['end'] <= $element['end']) {
×
316
            $element['children'][] = array_shift($elements);
×
317
        }
318

319
        // Make sure our children know whether they have children, too
UNCOV
320
        if (isset($element['children'])) {
×
321
            $element['children'] = $this->structureTimelineData($element['children']);
×
322
        }
323

324
        // If we have no younger siblings, we can return
UNCOV
325
        if ($elements === []) {
×
326
            return [$element];
×
327
        }
328

329
        // Make sure our younger siblings know their relatives, too
UNCOV
330
        return array_merge([$element], $this->structureTimelineData($elements));
×
331
    }
332

333
    /**
334
     * Returns an array of data from all of the modules
335
     * that should be displayed in the 'Vars' tab.
336
     */
337
    protected function collectVarData(): array
338
    {
UNCOV
339
        if (! ($this->config->collectVarData ?? true)) {
×
340
            return [];
×
341
        }
342

UNCOV
343
        $data = [];
×
344

UNCOV
345
        foreach ($this->collectors as $collector) {
×
346
            if (! $collector->hasVarData()) {
×
347
                continue;
×
348
            }
349

UNCOV
350
            $data = array_merge($data, $collector->getVarData());
×
351
        }
352

UNCOV
353
        return $data;
×
354
    }
355

356
    /**
357
     * Rounds a number to the nearest incremental value.
358
     */
359
    protected function roundTo(float $number, int $increments = 5): float
360
    {
UNCOV
361
        $increments = 1 / $increments;
×
362

UNCOV
363
        return ceil($number * $increments) / $increments;
×
364
    }
365

366
    /**
367
     * Prepare for debugging.
368
     *
369
     * @return void
370
     */
371
    public function prepare(?RequestInterface $request = null, ?ResponseInterface $response = null)
372
    {
373
        /**
374
         * @var IncomingRequest|null $request
375
         */
376
        if (CI_DEBUG && ! is_cli()) {
85✔
UNCOV
377
            $app = service('codeigniter');
×
378

UNCOV
379
            $request ??= service('request');
×
380
            /** @var ResponseInterface $response */
UNCOV
381
            $response ??= service('response');
×
382

383
            // Disable the toolbar for downloads
UNCOV
384
            if ($response instanceof DownloadResponse) {
×
385
                return;
×
386
            }
387

UNCOV
388
            $toolbar = service('toolbar', config(ToolbarConfig::class));
×
389
            $stats   = $app->getPerformanceStats();
×
390
            $data    = $toolbar->run(
×
391
                $stats['startTime'],
×
392
                $stats['totalTime'],
×
393
                $request,
×
394
                $response
×
395
            );
×
396

UNCOV
397
            helper('filesystem');
×
398

399
            // Updated to microtime() so we can get history
UNCOV
400
            $time = sprintf('%.6f', Time::now()->format('U.u'));
×
401

UNCOV
402
            if (! is_dir(WRITEPATH . 'debugbar')) {
×
403
                mkdir(WRITEPATH . 'debugbar', 0777);
×
404
            }
405

UNCOV
406
            write_file(WRITEPATH . 'debugbar/debugbar_' . $time . '.json', $data, 'w+');
×
407

UNCOV
408
            $format = $response->getHeaderLine('content-type');
×
409

410
            // Non-HTML formats should not include the debugbar
411
            // then we send headers saying where to find the debug data
412
            // for this response
UNCOV
413
            if ($request->isAJAX() || ! str_contains($format, 'html')) {
×
414
                $response->setHeader('Debugbar-Time', "{$time}")
×
415
                    ->setHeader('Debugbar-Link', site_url("?debugbar_time={$time}"));
×
416

UNCOV
417
                return;
×
418
            }
419

UNCOV
420
            $oldKintMode        = Kint::$mode_default;
×
421
            Kint::$mode_default = Kint::MODE_RICH;
×
422
            $kintScript         = @Kint::dump('');
×
423
            Kint::$mode_default = $oldKintMode;
×
424
            $kintScript         = substr($kintScript, 0, strpos($kintScript, '</style>') + 8);
×
425
            $kintScript         = ($kintScript === '0') ? '' : $kintScript;
×
426

UNCOV
427
            $script = PHP_EOL
×
428
                . '<script ' . csp_script_nonce() . ' id="debugbar_loader" '
×
429
                . 'data-time="' . $time . '" '
×
430
                . 'src="' . site_url() . '?debugbar"></script>'
×
431
                . '<script ' . csp_script_nonce() . ' id="debugbar_dynamic_script"></script>'
×
432
                . '<style ' . csp_style_nonce() . ' id="debugbar_dynamic_style"></style>'
×
433
                . $kintScript
×
434
                . PHP_EOL;
×
435

UNCOV
436
            if (str_contains((string) $response->getBody(), '<head>')) {
×
437
                $response->setBody(
×
438
                    preg_replace(
×
439
                        '/<head>/',
×
440
                        '<head>' . $script,
×
441
                        $response->getBody(),
×
442
                        1
×
443
                    )
×
444
                );
×
445

UNCOV
446
                return;
×
447
            }
448

UNCOV
449
            $response->appendBody($script);
×
450
        }
451
    }
452

453
    /**
454
     * Inject debug toolbar into the response.
455
     *
456
     * @codeCoverageIgnore
457
     *
458
     * @return void
459
     */
460
    public function respond()
461
    {
UNCOV
462
        if (ENVIRONMENT === 'testing') {
×
463
            return;
×
464
        }
465

UNCOV
466
        $request = service('request');
×
467

468
        // If the request contains '?debugbar then we're
469
        // simply returning the loading script
UNCOV
470
        if ($request->getGet('debugbar') !== null) {
×
471
            header('Content-Type: application/javascript');
×
472

UNCOV
473
            ob_start();
×
474
            include $this->config->viewsPath . 'toolbarloader.js';
×
475
            $output = ob_get_clean();
×
476
            $output = str_replace('{url}', rtrim(site_url(), '/'), $output);
×
477
            echo $output;
×
478

UNCOV
479
            exit;
×
480
        }
481

482
        // Otherwise, if it includes ?debugbar_time, then
483
        // we should return the entire debugbar.
UNCOV
484
        if ($request->getGet('debugbar_time')) {
×
485
            helper('security');
×
486

487
            // Negotiate the content-type to format the output
UNCOV
488
            $format = $request->negotiate('media', ['text/html', 'application/json', 'application/xml']);
×
489
            $format = explode('/', $format)[1];
×
490

UNCOV
491
            $filename = sanitize_filename('debugbar_' . $request->getGet('debugbar_time'));
×
492
            $filename = WRITEPATH . 'debugbar/' . $filename . '.json';
×
493

UNCOV
494
            if (is_file($filename)) {
×
495
                // Show the toolbar if it exists
UNCOV
496
                echo $this->format(file_get_contents($filename), $format);
×
497

UNCOV
498
                exit;
×
499
            }
500

501
            // Filename not found
UNCOV
502
            http_response_code(404);
×
503

UNCOV
504
            exit; // Exit here is needed to avoid loading the index page
×
505
        }
506
    }
507

508
    /**
509
     * Format output
510
     */
511
    protected function format(string $data, string $format = 'html'): string
512
    {
UNCOV
513
        $data = json_decode($data, true);
×
514

UNCOV
515
        if ($this->config->maxHistory !== 0 && preg_match('/\d+\.\d{6}/s', (string) service('request')->getGet('debugbar_time'), $debugbarTime)) {
×
516
            $history = new History();
×
517
            $history->setFiles(
×
518
                $debugbarTime[0],
×
519
                $this->config->maxHistory
×
520
            );
×
521

UNCOV
522
            $data['collectors'][] = $history->getAsArray();
×
523
        }
524

UNCOV
525
        $output = '';
×
526

527
        switch ($format) {
UNCOV
528
            case 'html':
×
529
                $data['styles'] = [];
×
530
                extract($data);
×
531
                $parser = service('parser', $this->config->viewsPath, null, false);
×
532
                ob_start();
×
533
                include $this->config->viewsPath . 'toolbar.tpl.php';
×
534
                $output = ob_get_clean();
×
535
                break;
×
536

UNCOV
537
            case 'json':
×
538
                $formatter = new JSONFormatter();
×
539
                $output    = $formatter->format($data);
×
540
                break;
×
541

UNCOV
542
            case 'xml':
×
543
                $formatter = new XMLFormatter();
×
544
                $output    = $formatter->format($data);
×
545
                break;
×
546
        }
547

UNCOV
548
        return $output;
×
549
    }
550
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc