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

codeigniter4 / CodeIgniter4 / 20597166463

30 Dec 2025 01:04PM UTC coverage: 85.5% (+1.0%) from 84.522%
20597166463

Pull #9860

github

web-flow
Merge 014ed47f7 into 0d52f5abb
Pull Request #9860: feat: allow overriding namespaced views via `app/Views` directory

7 of 7 new or added lines in 1 file covered. (100.0%)

87 existing lines in 2 files now uncovered.

21811 of 25510 relevant lines covered (85.5%)

203.67 hits per line

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

39.22
/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;
95✔
55

56
        foreach ($config->collectors as $collector) {
95✔
57
            if (! class_exists($collector)) {
95✔
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

64
                continue;
×
65
            }
66

67
            $this->collectors[] = new $collector();
95✔
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
    {
81
        $data = [];
2✔
82
        // Data items used within the view.
83
        $data['url']             = current_url();
2✔
84
        $data['method']          = $request->getMethod();
2✔
85
        $data['isAJAX']          = $request->isAJAX();
2✔
86
        $data['startTime']       = $startTime;
2✔
87
        $data['totalTime']       = $totalTime * 1000;
2✔
88
        $data['totalMemory']     = number_format(memory_get_peak_usage() / 1024 / 1024, 3);
2✔
89
        $data['segmentDuration'] = $this->roundTo($data['totalTime'] / 7);
2✔
90
        $data['segmentCount']    = (int) ceil($data['totalTime'] / $data['segmentDuration']);
2✔
91
        $data['CI_VERSION']      = CodeIgniter::CI_VERSION;
2✔
92
        $data['collectors']      = [];
2✔
93

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

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

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

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

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

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

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

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

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

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

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

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

145
        foreach ($request->headers() as $name => $value) {
2✔
146
            if ($value instanceof Header) {
1✔
147
                $data['vars']['headers'][esc($name)] = esc($value->getValueLine());
1✔
148
            } else {
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

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

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

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

171
        foreach ($response->headers() as $name => $value) {
2✔
172
            if ($value instanceof Header) {
2✔
173
                $data['vars']['response']['headers'][esc($name)] = esc($value->getValueLine());
2✔
174
            } else {
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

184
        $data['config'] = Config::display();
2✔
185

186
        $response->getCSP()->addImageSrc('data:');
2✔
187

188
        return json_encode($data);
2✔
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
    {
196
        $rows       = $this->collectTimelineData($collectors);
×
197
        $styleCount = 0;
×
198

199
        // Use recursive render function
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
    {
208
        $displayTime = $segmentCount * $segmentDuration;
×
209

210
        $output = '';
×
211

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
217
            $open = $row['name'] === 'Controller';
×
218

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 {
222
                $output .= '<tr>';
×
223
            }
224

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

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

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

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

239
            $styleCount++;
×
240

241
            // Add children if any
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

248
                if ($isQuery) {
×
249
                    // Output query string if query
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
255
                    $output .= $this->renderTimelineRecursive($row['children'], $startTime, $segmentCount, $segmentDuration, $styles, $styleCount, $level + 1, true);
×
256
                }
257

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

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
    {
275
        $data = [];
×
276

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

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

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

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

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

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

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
312
        $element = array_shift($elements);
×
313

314
        // If we have children behind us, collect and attach them to us
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
320
        if (isset($element['children'])) {
×
321
            $element['children'] = $this->structureTimelineData($element['children']);
×
322
        }
323

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

329
        // Make sure our younger siblings know their relatives, too
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
    {
339
        if (! ($this->config->collectVarData ?? true)) {
2✔
340
            return [];
×
341
        }
342

343
        $data = [];
2✔
344

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

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

353
        return $data;
2✔
354
    }
355

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

363
        return ceil($number * $increments) / $increments;
2✔
364
    }
365

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

377
            $request ??= service('request');
2✔
378
            /** @var ResponseInterface $response */
379
            $response ??= service('response');
2✔
380

381
            // Disable the toolbar for downloads
382
            if ($response instanceof DownloadResponse) {
2✔
UNCOV
383
                return;
×
384
            }
385

386
            $toolbar = service('toolbar', $this->config);
2✔
387
            $stats   = $app->getPerformanceStats();
2✔
388
            $data    = $toolbar->run(
2✔
389
                $stats['startTime'],
2✔
390
                $stats['totalTime'],
2✔
391
                $request,
2✔
392
                $response,
2✔
393
            );
2✔
394

395
            helper('filesystem');
2✔
396

397
            // Updated to microtime() so we can get history
398
            $time = sprintf('%.6f', Time::now()->format('U.u'));
2✔
399

400
            if (! is_dir(WRITEPATH . 'debugbar')) {
2✔
UNCOV
401
                mkdir(WRITEPATH . 'debugbar', 0777);
×
402
            }
403

404
            write_file(WRITEPATH . 'debugbar/debugbar_' . $time . '.json', $data, 'w+');
2✔
405

406
            $format = $response->getHeaderLine('content-type');
2✔
407

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

415
                return;
1✔
416
            }
417

418
            $oldKintMode        = Kint::$mode_default;
1✔
419
            Kint::$mode_default = Kint::MODE_RICH;
1✔
420
            $kintScript         = @Kint::dump('');
1✔
421
            Kint::$mode_default = $oldKintMode;
1✔
422
            $kintScript         = substr($kintScript, 0, strpos($kintScript, '</style>') + 8);
1✔
423
            $kintScript         = ($kintScript === '0') ? '' : $kintScript;
1✔
424

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

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

444
                return;
×
445
            }
446

447
            $response->appendBody($script);
1✔
448
        }
449
    }
450

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

462
        $request = service('request');
×
463

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

UNCOV
469
            ob_start();
×
470
            include $this->config->viewsPath . 'toolbarloader.js';
×
471
            $output = ob_get_clean();
×
UNCOV
472
            $output = str_replace('{url}', rtrim(site_url(), '/'), $output);
×
473
            echo $output;
×
474

475
            exit;
×
476
        }
477

478
        // Otherwise, if it includes ?debugbar_time, then
479
        // we should return the entire debugbar.
UNCOV
480
        if ($request->getGet('debugbar_time')) {
×
UNCOV
481
            helper('security');
×
482

483
            // Negotiate the content-type to format the output
484
            $format = $request->negotiate('media', ['text/html', 'application/json', 'application/xml']);
×
485
            $format = explode('/', $format)[1];
×
486

UNCOV
487
            $filename = sanitize_filename('debugbar_' . $request->getGet('debugbar_time'));
×
488
            $filename = WRITEPATH . 'debugbar/' . $filename . '.json';
×
489

UNCOV
490
            if (is_file($filename)) {
×
491
                // Show the toolbar if it exists
492
                echo $this->format(file_get_contents($filename), $format);
×
493

494
                exit;
×
495
            }
496

497
            // Filename not found
498
            http_response_code(404);
×
499

UNCOV
500
            exit; // Exit here is needed to avoid loading the index page
×
501
        }
502
    }
503

504
    /**
505
     * Format output
506
     */
507
    protected function format(string $data, string $format = 'html'): string
508
    {
UNCOV
509
        $data = json_decode($data, true);
×
510

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

518
            $data['collectors'][] = $history->getAsArray();
×
519
        }
520

UNCOV
521
        $output = '';
×
522

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

533
            case 'json':
×
534
                $formatter = new JSONFormatter();
×
535
                $output    = $formatter->format($data);
×
UNCOV
536
                break;
×
537

538
            case 'xml':
×
539
                $formatter = new XMLFormatter();
×
540
                $output    = $formatter->format($data);
×
UNCOV
541
                break;
×
542
        }
543

544
        return $output;
×
545
    }
546

547
    /**
548
     * Determine if the toolbar should be disabled based on the request headers.
549
     *
550
     * This method allows checking both the presence of headers and their expected values.
551
     * Useful for AJAX, HTMX, Unpoly, Turbo, etc., where partial HTML responses are expected.
552
     *
553
     * @return bool True if any header condition matches; false otherwise.
554
     */
555
    private function shouldDisableToolbar(IncomingRequest $request): bool
556
    {
557
        // Fallback for older installations where the config option is missing (e.g. after upgrading from a previous version).
558
        $headers = $this->config->disableOnHeaders ?? ['X-Requested-With' => 'xmlhttprequest'];
2✔
559

560
        foreach ($headers as $headerName => $expectedValue) {
2✔
561
            if (! $request->hasHeader($headerName)) {
2✔
562
                continue; // header not present, skip
1✔
563
            }
564

565
            // If expectedValue is null, only presence is enough
566
            if ($expectedValue === null) {
1✔
UNCOV
567
                return true;
×
568
            }
569

570
            $headerValue = strtolower($request->getHeaderLine($headerName));
1✔
571

572
            if ($headerValue === strtolower($expectedValue)) {
1✔
573
                return true;
1✔
574
            }
575
        }
576

577
        return false;
1✔
578
    }
579
}
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