Skip to content

Commit 71b67e6

Browse files
committed
Merge pull request #20 from Nyholm/decorator
Made the profiler page better
2 parents aa8a51f + f37fa87 commit 71b67e6

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/Cache/LoggingCachePool.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ public function getItem($key, array $tags = [])
6161
{
6262
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
6363
$result = $call->result;
64+
$call->isHit = $result->isHit();
6465

65-
if ($result->isHit()) {
66-
$call->result = sprintf('<DATA:%s>', gettype($result->get()));
66+
// Display the result in a good way depending on the data type
67+
if ($call->isHit) {
68+
$call->result = $this->getValueRepresentation($result->get());
6769
} else {
68-
$call->result = false;
70+
$call->result = null;
6971
}
7072

7173
$this->calls[] = $call;
@@ -91,11 +93,11 @@ public function deleteItem($key, array $tags = [])
9193

9294
public function save(CacheItemInterface $item)
9395
{
94-
$itemClone = clone $item;
95-
$itemClone->set(sprintf('<DATA:%s', gettype($item->get())));
96+
$key = $item->getKey();
97+
$value = $this->getValueRepresentation($item->get());
9698

9799
$call = $this->timeCall(__FUNCTION__, [$item]);
98-
$call->arguments = ['<CacheItem>', $itemClone];
100+
$call->arguments = ['<CacheItem>', $key, $value];
99101
$this->calls[] = $call;
100102

101103
return $call->result;
@@ -155,4 +157,27 @@ public function getCalls()
155157
{
156158
return $this->calls;
157159
}
160+
161+
/**
162+
* Get a string to represent the value.
163+
*
164+
* @param mixed $value
165+
*
166+
* @return string
167+
*/
168+
private function getValueRepresentation($value)
169+
{
170+
$type = gettype($value);
171+
if (in_array($type, ['boolean', 'integer', 'double', 'string', 'NULL'])) {
172+
$rep = $value;
173+
} elseif ($type === 'array') {
174+
$rep = json_encode($value);
175+
} elseif ($type === 'object') {
176+
$rep = get_class($value);
177+
} else {
178+
$rep = sprintf('<DATA:%s>', $type);
179+
}
180+
181+
return $rep;
182+
}
158183
}

src/DataCollector/CacheDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function calculateStatistics()
128128
$statistics[$name]['time'] += $call->time;
129129
if ($call->name === 'getItem') {
130130
$statistics[$name]['reads'] += 1;
131-
if ($call->result !== false) {
131+
if ($call->isHit) {
132132
$statistics[$name]['hits'] += 1;
133133
} else {
134134
$statistics[$name]['misses'] += 1;

src/Resources/views/Collector/cache.html.twig

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}
1111

12+
{% block head %}
13+
{{ parent() }}
14+
<style>
15+
.color-red {
16+
color:red;
17+
}
18+
</style>
19+
{% endblock head %}
20+
1221
{% block toolbar %}
1322
{% set icon %}
1423
<img width="20" height="28" alt="Cache"
@@ -91,8 +100,12 @@
91100
<li class="{{ i is odd ? 'odd' : 'even' }}">
92101
<div>
93102
<strong>Name</strong>: {{ call.name }}<br />
94-
<strong>Arguments</strong>: {{ call.arguments|json_encode() }}<br />
95-
<strong>Results</strong>: {{ call.result|json_encode() }}<br />
103+
<strong>Arguments</strong>: {{ call.arguments|json_encode|truncate(140) }}<br />
104+
<strong>Results</strong>: {% if call.result == false %}
105+
<span class="color-red">Miss</span>
106+
{% else %}
107+
{{ call.result|truncate(140) }}
108+
{% endif %}<br />
96109
</div>
97110
<small>
98111
<strong>Time</strong>: {{ '%0.2f'|format(call.time * 1000) }} ms

0 commit comments

Comments
 (0)