Skip to content

Commit eefc67b

Browse files
committed
feat: debugbar logging requests using microtime
From #5643
1 parent 2a5322b commit eefc67b

7 files changed

Lines changed: 114 additions & 27 deletions

File tree

admin/css/debug-toolbar/toolbar.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@
474474
width: 70px;
475475
}
476476

477-
.debug-bar-width140p {
478-
width: 140px;
477+
.debug-bar-width190p {
478+
width: 190px;
479479
}
480480

481481
.debug-bar-width20e {

system/Debug/Toolbar.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(ToolbarConfig $config)
5454
foreach ($config->collectors as $collector) {
5555
if (! class_exists($collector)) {
5656
log_message('critical', 'Toolbar collector does not exists(' . $collector . ').' .
57-
'please check $collectors in the Config\Toolbar.php file.');
57+
'please check $collectors in the Config\Toolbar.php file.');
5858

5959
continue;
6060
}
@@ -378,8 +378,8 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
378378

379379
helper('filesystem');
380380

381-
// Updated to time() so we can get history
382-
$time = time();
381+
// Updated to microtime() so we can get history
382+
$time = sprintf('%.6f', microtime(true));
383383

384384
if (! is_dir(WRITEPATH . 'debugbar')) {
385385
mkdir(WRITEPATH . 'debugbar', 0777);
@@ -485,10 +485,10 @@ protected function format(string $data, string $format = 'html'): string
485485
{
486486
$data = json_decode($data, true);
487487

488-
if ($this->config->maxHistory !== 0) {
488+
if ($this->config->maxHistory !== 0 && preg_match('/\d+\.\d{6}/s', (string) Services::request()->getGet('debugbar_time'), $debugbarTime)) {
489489
$history = new History();
490490
$history->setFiles(
491-
(int) Services::request()->getGet('debugbar_time'),
491+
$debugbarTime[0],
492492
$this->config->maxHistory
493493
);
494494

system/Debug/Toolbar/Collectors/History.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace CodeIgniter\Debug\Toolbar\Collectors;
1313

14+
use DateTime;
15+
1416
/**
1517
* History collector
1618
*/
@@ -56,10 +58,10 @@ class History extends BaseCollector
5658
/**
5759
* Specify time limit & file count for debug history.
5860
*
59-
* @param int $current Current history time
60-
* @param int $limit Max history files
61+
* @param string $current Current history time
62+
* @param int $limit Max history files
6163
*/
62-
public function setFiles(int $current, int $limit = 20)
64+
public function setFiles(string $current, int $limit = 20)
6365
{
6466
$filenames = glob(WRITEPATH . 'debugbar/debugbar_*.json');
6567

@@ -81,13 +83,13 @@ public function setFiles(int $current, int $limit = 20)
8183

8284
$contents = @json_decode($contents);
8385
if (json_last_error() === JSON_ERROR_NONE) {
84-
preg_match_all('/\d+/', $filename, $time);
85-
$time = (int) end($time[0]);
86+
preg_match('/debugbar_(.*)\.json$/s', $filename, $time);
87+
$time = sprintf('%.6f', $time[1] ?? 0);
8688

8789
// Debugbar files shown in History Collector
8890
$files[] = [
8991
'time' => $time,
90-
'datetime' => date('Y-m-d H:i:s', $time),
92+
'datetime' => DateTime::createFromFormat('U.u', $time)->format('Y-m-d H:i:s.u'),
9193
'active' => $time === $current,
9294
'status' => $contents->vars->response->statusCode,
9395
'method' => $contents->method,

system/Debug/Toolbar/Views/_history.tpl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<table>
22
<thead>
3-
<tr>
4-
<th>Action</th>
5-
<th>Datetime</th>
6-
<th>Status</th>
7-
<th>Method</th>
8-
<th>URL</th>
9-
<th>Content-Type</th>
10-
<th>Is AJAX?</th>
11-
</tr>
3+
<tr>
4+
<th>Action</th>
5+
<th>Datetime</th>
6+
<th>Status</th>
7+
<th>Method</th>
8+
<th>URL</th>
9+
<th>Content-Type</th>
10+
<th>Is AJAX?</th>
11+
</tr>
1212
</thead>
1313
<tbody>
1414
{files}
1515
<tr data-active="{active}">
16-
<td class="debug-bar-width70p">
17-
<button class="ci-history-load" data-time="{time}">Load</button>
16+
<td class="debug-bar-width70p">
17+
<button class="ci-history-load" data-time="{time}">Load</button>
1818
</td>
19-
<td class="debug-bar-width140p">{datetime}</td>
19+
<td class="debug-bar-width190p">{datetime}</td>
2020
<td>{status}</td>
2121
<td>{method}</td>
2222
<td>{url}</td>

system/Debug/Toolbar/Views/toolbar.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,8 @@
773773
width: 70px;
774774
}
775775

776-
.debug-bar-width140p {
777-
width: 140px;
776+
.debug-bar-width190p {
777+
width: 190px;
778778
}
779779

780780
.debug-bar-width20e {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Debug\Toolbar\Collectors;
13+
14+
use CodeIgniter\Test\CIUnitTestCase;
15+
use DateTime;
16+
17+
/**
18+
* @internal
19+
*/
20+
final class HistoryTest extends CIUnitTestCase
21+
{
22+
private const STEP = 0.0001;
23+
24+
private float $time;
25+
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
$this->time = (float) sprintf('%.4f', microtime(true));
30+
}
31+
32+
private function createDummyDebugbarJson()
33+
{
34+
$time = $this->time;
35+
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json";
36+
37+
$dummyData = [
38+
'vars' => [
39+
'response' => [
40+
'statusCode' => 200,
41+
'contentType' => 'text/html; charset=UTF-8',
42+
],
43+
],
44+
'method' => 'get',
45+
'url' => 'localhost',
46+
'isAJAX' => false,
47+
];
48+
// create 20 dummy debugbar json files
49+
for ($i = 0; $i < 20; $i++) {
50+
$path = str_replace($time, sprintf('%.4f', $time - self::STEP), $path);
51+
file_put_contents($path, json_encode($dummyData));
52+
$time = sprintf('%.4f', $time - self::STEP);
53+
}
54+
}
55+
56+
protected function tearDown(): void
57+
{
58+
command('debugbar:clear');
59+
}
60+
61+
public function testSetFiles()
62+
{
63+
$time = $this->time;
64+
// test dir is now populated with json
65+
$this->createDummyDebugbarJson();
66+
67+
$activeRowTime = $time = sprintf('%.6f', $time - self::STEP);
68+
69+
$history = new History();
70+
$history->setFiles($time, 20);
71+
$this->assertArrayHasKey('files', $history->display());
72+
$this->assertNotEmpty($history->display()['files'], 'Dummy Debugbar data is empty');
73+
74+
foreach ($history->display()['files'] as $request) {
75+
$this->assertSame($request['time'], sprintf('%.6f', $time));
76+
$this->assertSame($request['datetime'], DateTime::createFromFormat('U.u', $time)->format('Y-m-d H:i:s.u'));
77+
$this->assertSame($request['active'], ($time === $activeRowTime));
78+
79+
$time = sprintf('%.6f', $time - self::STEP);
80+
}
81+
}
82+
}

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ BREAKING
2020
- The ``CodeIgniter\Autoloader\Autoloader::initialize()`` has changed the behavior to fix a bug. It used to use Composer classmap only when ``$modules->discoverInComposer`` is true. Now it always uses the Composer classmap if Composer is available.
2121
- The color code output by :ref:`CLI::color() <cli-library-color>` has been changed to fix a bug.
2222
- To prevent unexpected access from the web browser, if a controller is added to a cli route (``$routes->cli()``), all methods of that controller are no longer accessible via auto-routing.
23+
- There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method.
2324

2425
Enhancements
2526
************
@@ -67,6 +68,8 @@ Others
6768
- QueryBuilder raw SQL string support
6869
- Added the class ``CodeIgniter\Database\RawSql`` which expresses raw SQL strings.
6970
- :ref:`select() <query-builder-select-rawsql>`, :ref:`where() <query-builder-where-rawsql>`, :ref:`like() <query-builder-like-rawsql>` accept the ``CodeIgniter\Database\RawSql`` instance.
71+
- Debugbar enhancements
72+
- Debug toolbar is now using ``microtime()`` instead of ``time()``.
7073

7174
Changes
7275
*******

0 commit comments

Comments
 (0)