Skip to content

Commit 3794477

Browse files
committed
Add Activity class
1 parent 194a0af commit 3794477

6 files changed

Lines changed: 149 additions & 175 deletions

File tree

‎src/Activity.php‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of Aplus Framework Debug Library.
4+
*
5+
* (c) Natan Felles <natanfelles@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Framework\Debug;
11+
12+
/**
13+
* Class Activity.
14+
*
15+
* @since 5
16+
*/
17+
class Activity
18+
{
19+
public string $collection;
20+
public string $collector;
21+
public string $class;
22+
public string $description;
23+
public float $start;
24+
public float $end;
25+
public float $width;
26+
public float $left;
27+
28+
public function __construct(
29+
string $collector,
30+
string $class,
31+
string $description,
32+
float $start,
33+
float $end
34+
) {
35+
$this->collector = $collector;
36+
$this->class = $class;
37+
$this->description = $description;
38+
$this->start = $start;
39+
$this->end = $end;
40+
}
41+
42+
public function total() : float
43+
{
44+
return $this->end - $this->start;
45+
}
46+
47+
public function title() : string
48+
{
49+
$title = $this->width . '%';
50+
$title .= ' · From ' . $this->left . '% to ' . $this->left + $this->width . '%';
51+
$title .= ' · From ' . \round($this->start, 6) . ' to ' . \round($this->end, 6) . ' second';
52+
return $title;
53+
}
54+
55+
/**
56+
* Set the Activity "left" and "width" representing the CSS `margin-left`
57+
* and `width` properties used to create the time bar.
58+
*
59+
* @param float $min The minimum time of all collected activities
60+
* @param float $max The maximum time of all collected activities
61+
*
62+
* @return static
63+
*/
64+
public function setTimeBarValues(float $min, float $max) : static
65+
{
66+
$total = $max - $min;
67+
if ($total > 0) {
68+
$this->left = \round(($this->start - $min) * 100 / $total, 3);
69+
$this->width = \round($this->total() * 100 / $total, 3);
70+
return $this;
71+
}
72+
$this->left = .0;
73+
$this->width = .0;
74+
return $this;
75+
}
76+
}

‎src/Collection.php‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,16 @@ public function hasCollectors() : bool
9292
}
9393

9494
/**
95-
* @return array<int,array<int,array{
96-
* collection: string,
97-
* collector: string,
98-
* class: string,
99-
* description: string,
100-
* start: float,
101-
* end: float,
102-
* }>>
95+
* @return array<int,array<int,Activity>>
10396
*/
10497
public function getActivities() : array
10598
{
10699
$result = [];
107100
foreach ($this->getCollectors() as $collector) {
108101
$activities = $collector->getActivities();
109102
if ($activities) {
110-
foreach ($activities as &$activity) {
111-
$activity = \array_merge([
112-
'collection' => $this->getName(),
113-
], $activity);
103+
foreach ($activities as $activity) {
104+
$activity->collection = $this->getName();
114105
}
115106
unset($activity);
116107
$result[] = $activities;

‎src/Collector.php‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ abstract class Collector
2222
*/
2323
protected array $data = [];
2424
/**
25-
* @var array<int,array{
26-
* collector: string,
27-
* class: string,
28-
* description: string,
29-
* start: float,
30-
* end: float,
31-
* }>
25+
* @var array<int,Activity>
3226
*/
3327
protected array $activities = [];
3428

@@ -72,13 +66,7 @@ public function hasData() : bool
7266
}
7367

7468
/**
75-
* @return array<int,array{
76-
* collector: string,
77-
* class: string,
78-
* description: string,
79-
* start: float,
80-
* end: float,
81-
* }>
69+
* @return array<int,Activity>
8270
*/
8371
public function getActivities() : array
8472
{

‎src/Debugger.php‎

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,7 @@ public function getOptions() : array
165165
* min: float,
166166
* max: float,
167167
* total: float,
168-
* collected: array<int,array{
169-
* collection: string,
170-
* collector: string,
171-
* class: string,
172-
* description: string,
173-
* start: float,
174-
* end: float,
175-
* total: float,
176-
* left: float,
177-
* width: float,
178-
* }>,
168+
* collected: array<int,Activity>,
179169
* }
180170
*/
181171
public function getActivities() : array
@@ -190,12 +180,12 @@ public function getActivities() : array
190180
$max = .0;
191181
if ($collected) {
192182
\usort($collected, static function ($c1, $c2) {
193-
return $c1['start'] <=> $c2['start'];
183+
return $c1->start <=> $c2->start;
194184
});
195185
$min = \min(\array_column($collected, 'start'));
196186
$max = \max(\array_column($collected, 'end'));
197-
foreach ($collected as &$activity) {
198-
$activity = $this->addActivityValues($activity, $min, $max);
187+
foreach ($collected as $activity) {
188+
$activity->setTimeBarValues($min, $max);
199189
}
200190
unset($activity);
201191
}
@@ -207,51 +197,6 @@ public function getActivities() : array
207197
];
208198
}
209199

210-
/**
211-
* Updates the $activity variable.
212-
*
213-
* Adds the "total" key representing the total execution time.
214-
*
215-
* Adds the "left" and "width" keys representing the CSS `margin-left` and
216-
* `width` properties used to create the time bar.
217-
*
218-
* @param array{
219-
* collection: string,
220-
* collector: string,
221-
* class: string,
222-
* description: string,
223-
* start: float,
224-
* end: float,
225-
* } $activity Current activity
226-
* @param float $min The minimum time of the collected activities
227-
* @param float $max The maximum time of collected activities
228-
*
229-
* @return array{
230-
* collection: string,
231-
* collector: string,
232-
* class: string,
233-
* description: string,
234-
* start: float,
235-
* end: float,
236-
* total: float,
237-
* left: float,
238-
* width: float,
239-
* }
240-
*/
241-
protected function addActivityValues(array $activity, float $min, float $max) : array
242-
{
243-
$total = $max - $min;
244-
$activity['total'] = $activity['end'] - $activity['start'];
245-
if ($total > 0) {
246-
$activity['left'] = \round(($activity['start'] - $min) * 100 / $total, 3);
247-
$activity['width'] = \round($activity['total'] * 100 / $total, 3);
248-
return $activity;
249-
}
250-
$activity['left'] = .0;
251-
$activity['width'] = .0;
252-
return $activity;
253-
}
254-
255200
/**
256201
* Set the path of the debugbar view.
257202
*

‎src/Views/debugbar/debugbar.php‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Framework\Debug\Activity;
34
use Framework\Debug\Debugger as D;
45

56
/**
@@ -8,17 +9,7 @@
89
* min: float,
910
* max: float,
1011
* total: float,
11-
* collected: array<int,array{
12-
* collection: string,
13-
* collector: string,
14-
* class: string,
15-
* description: string,
16-
* start: float,
17-
* end: float,
18-
* total: float,
19-
* left: float,
20-
* width: float,
21-
* }>,
12+
* collected: array<int,Activity>,
2213
* } $activities
2314
* @var array<string,mixed> $options
2415
*/
@@ -89,16 +80,13 @@ class_exists('Aplus')
8980
<?php foreach ($activities['collected'] as $index => $collected): ?>
9081
<tr>
9182
<td><?= $index + 1 ?></td>
92-
<td><?= D::esc($collected['collection']) ?></td>
93-
<td title="<?= D::esc($collected['class']) ?>"><?= D::esc($collected['collector']) ?></td>
94-
<td><?= D::esc($collected['description']) ?></td>
95-
<td><?= D::roundSecondsToMilliseconds($collected['total']) ?></td>
83+
<td><?= D::esc($collected->collection) ?></td>
84+
<td title="<?= D::esc($collected->class) ?>"><?= D::esc($collected->collector) ?></td>
85+
<td><?= D::esc($collected->description) ?></td>
86+
<td><?= D::roundSecondsToMilliseconds($collected->total()) ?></td>
9687
<td>
97-
<span style="width: <?= $collected['width'] ?>%; margin-left: <?= $collected['left']?>%"
98-
title="<?= $collected['width']
99-
?>% · From <?= $collected['left'] ?>% to <?= $collected['left'] + $collected['width']
100-
?>% · From <?= round($collected['start'], 6) ?> to <?= round($collected['end'], 6)
101-
?> second">
88+
<span style="width: <?= $collected->width ?>%; margin-left: <?= $collected->left ?>%"
89+
title="<?= $collected->title() ?>">
10290
</span>
10391
</td>
10492
</tr>

0 commit comments

Comments
 (0)