Skip to content

Commit 95437a4

Browse files
authored
Merge pull request #5958 from kenjis/debugbar-request-microtime
feat: Debugbar request microtime
2 parents 8178bda + e04901a commit 95437a4

7 files changed

Lines changed: 133 additions & 28 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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ public function __construct(ToolbarConfig $config)
5353

5454
foreach ($config->collectors as $collector) {
5555
if (! class_exists($collector)) {
56-
log_message('critical', 'Toolbar collector does not exists(' . $collector . ').' .
57-
'please check $collectors in the Config\Toolbar.php file.');
56+
log_message(
57+
'critical',
58+
'Toolbar collector does not exist (' . $collector . ').'
59+
. ' Please check $collectors in the app/Config/Toolbar.php file.'
60+
);
5861

5962
continue;
6063
}
@@ -378,8 +381,8 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
378381

379382
helper('filesystem');
380383

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

384387
if (! is_dir(WRITEPATH . 'debugbar')) {
385388
mkdir(WRITEPATH . 'debugbar', 0777);
@@ -485,10 +488,10 @@ protected function format(string $data, string $format = 'html'): string
485488
{
486489
$data = json_decode($data, true);
487490

488-
if ($this->config->maxHistory !== 0) {
491+
if ($this->config->maxHistory !== 0 && preg_match('/\d+\.\d{6}/s', (string) Services::request()->getGet('debugbar_time'), $debugbarTime)) {
489492
$history = new History();
490493
$history->setFiles(
491-
(int) Services::request()->getGet('debugbar_time'),
494+
$debugbarTime[0],
492495
$this->config->maxHistory
493496
);
494497

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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 CodeIgniter\Test\Filters\CITestStreamFilter;
16+
use DateTime;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class HistoryTest extends CIUnitTestCase
22+
{
23+
private const STEP = 0.000001;
24+
25+
private float $time;
26+
private $streamFilter;
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
CITestStreamFilter::$buffer = '';
33+
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
34+
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
35+
36+
$this->time = (float) sprintf('%.6f', microtime(true));
37+
}
38+
39+
protected function tearDown(): void
40+
{
41+
command('debugbar:clear');
42+
43+
stream_filter_remove($this->streamFilter);
44+
}
45+
46+
private function createDummyDebugbarJson()
47+
{
48+
$time = $this->time;
49+
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json";
50+
51+
$dummyData = [
52+
'vars' => [
53+
'response' => [
54+
'statusCode' => 200,
55+
'contentType' => 'text/html; charset=UTF-8',
56+
],
57+
],
58+
'method' => 'get',
59+
'url' => 'localhost',
60+
'isAJAX' => false,
61+
];
62+
63+
// create 20 dummy debugbar json files
64+
for ($i = 0; $i < 20; $i++) {
65+
$path = str_replace($time, sprintf('%.6f', $time - self::STEP), $path);
66+
file_put_contents($path, json_encode($dummyData));
67+
$time = sprintf('%.6f', $time - self::STEP);
68+
}
69+
}
70+
71+
public function testSetFiles()
72+
{
73+
$time = $this->time;
74+
75+
// test dir is now populated with json
76+
$this->createDummyDebugbarJson();
77+
78+
$activeRowTime = $time = sprintf('%.6f', $time - self::STEP);
79+
80+
$history = new History();
81+
$history->setFiles($time, 20);
82+
83+
$this->assertArrayHasKey('files', $history->display());
84+
$this->assertNotEmpty($history->display()['files'], 'Dummy Debugbar data is empty');
85+
86+
foreach ($history->display()['files'] as $request) {
87+
$this->assertSame($request['time'], sprintf('%.6f', $time));
88+
$this->assertSame(
89+
$request['datetime'],
90+
DateTime::createFromFormat('U.u', $time)->format('Y-m-d H:i:s.u')
91+
);
92+
$this->assertSame($request['active'], ($time === $activeRowTime));
93+
94+
$time = sprintf('%.6f', $time - self::STEP);
95+
}
96+
}
97+
}

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)