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

aplus-framework / cache / 16306742406

15 Jul 2025 11:44PM UTC coverage: 95.194%. Remained the same
16306742406

push

github

natanfelles
Add since tag

515 of 541 relevant lines covered (95.19%)

55.5 hits per line

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

100.0
/src/Debug/CacheCollector.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Cache 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\Cache\Debug;
11

12
use Framework\Cache\ApcuCache;
13
use Framework\Cache\FilesCache;
14
use Framework\Cache\MemcachedCache;
15
use Framework\Cache\RedisCache;
16
use Framework\Debug\Collector;
17
use Framework\Debug\Debugger;
18

19
/**
20
 * Class CacheCollector.
21
 *
22
 * @package cache
23
 */
24
class CacheCollector extends Collector
25
{
26
    /**
27
     * @var array<string,mixed>
28
     */
29
    protected array $info;
30

31
    /**
32
     * @param array<string,mixed> $info
33
     *
34
     * @return static
35
     */
36
    public function setInfo(array $info) : static
37
    {
38
        $this->info = $info;
84✔
39
        return $this;
84✔
40
    }
41

42
    public function getActivities() : array
43
    {
44
        $activities = [];
21✔
45
        foreach ($this->getData() as $index => $data) {
21✔
46
            $activities[] = [
21✔
47
                'collector' => $this->getName(),
21✔
48
                'class' => static::class,
21✔
49
                'description' => 'Run command ' . ($index + 1),
21✔
50
                'start' => $data['start'],
21✔
51
                'end' => $data['end'],
21✔
52
            ];
21✔
53
        }
54
        return $activities;
21✔
55
    }
56

57
    public function getContents() : string
58
    {
59
        if (empty($this->info)) {
63✔
60
            return '<p>This collector has not been added to a Cache instance.</p>';
21✔
61
        }
62
        \ob_start(); ?>
42✔
63
        <p><strong>Handler:</strong>
42✔
64
            <?= \htmlentities($this->getHandler()) ?>
42✔
65
        </p>
42✔
66
        <?php
42✔
67
        if (isset($this->info['prefix'])) : ?>
42✔
68
            <p><strong>Keys Prefix:</strong>
42✔
69
                <?= \htmlentities($this->info['prefix']) ?>
42✔
70
            </p>
42✔
71
        <?php
42✔
72
        endif ?>
73
        <p><strong>Serializer:</strong>
42✔
74
            <?= \htmlentities($this->getSerializer()) ?>
42✔
75
        </p>
42✔
76
        <h1>Commands</h1>
42✔
77
        <?php
42✔
78
        echo $this->renderCommands();
42✔
79
        return \ob_get_clean(); // @phpstan-ignore-line
42✔
80
    }
81

82
    protected function getSerializer() : string
83
    {
84
        return $this->info['serializer'];
42✔
85
    }
86

87
    protected function renderCommands() : string
88
    {
89
        if (!$this->hasData()) {
42✔
90
            return '<p>No command was run.</p>';
21✔
91
        }
92
        $count = \count($this->getData());
21✔
93
        \ob_start(); ?>
21✔
94
        <p>Ran <?= $count ?> command<?= $count === 1 ? '' : 's' ?>
21✔
95
            in <?= $this->getCommandsTime() ?> ms:
21✔
96
        </p>
97
        <table>
98
            <thead>
99
            <tr>
100
                <th>#</th>
101
                <th>Command</th>
102
                <th>Status</th>
103
                <th>Key</th>
104
                <th>Value Type</th>
105
                <th title="Time To Live in seconds">TTL</th>
106
                <th>Expires At</th>
107
                <th title="Milliseconds">Time</th>
108
            </tr>
109
            </thead>
110
            <tbody>
111
            <?php foreach ($this->getData() as $index => $data): ?>
21✔
112
                <tr>
21✔
113
                    <td><?= $index + 1 ?></td>
21✔
114
                    <td><?= \htmlentities($data['command']) ?></td>
21✔
115
                    <td class="text-<?= $data['status'] === 'OK' ? 'success' : 'error' ?>">
21✔
116
                        <?= \htmlentities($data['status']) ?>
21✔
117
                    </td>
21✔
118
                    <td><?= \htmlentities($data['key'] ?? '') ?></td>
21✔
119
                    <td>
120
                        <?php if (isset($data['value'])): ?>
21✔
121
                            <pre><code class="language-php"><?= \htmlentities($data['value']) ?></code></pre>
21✔
122
                        <?php endif ?>
123
                    </td>
21✔
124
                    <td><?= \htmlentities((string) ($data['ttl'] ?? '')) ?></td>
21✔
125
                    <td><?php
126
                        if (isset($data['ttl'])) {
21✔
127
                            $ttl = $data['start'] + $data['ttl'];
21✔
128
                            echo \date('Y-m-d H:i:s', (int) $ttl);
21✔
129
                        } ?></td>
21✔
130
                    <td><?= Debugger::roundSecondsToMilliseconds($data['end'] - $data['start']) ?></td>
21✔
131
                </tr>
132
            <?php endforeach ?>
133
            </tbody>
21✔
134
        </table>
21✔
135
        <?php
21✔
136
        return \ob_get_clean(); // @phpstan-ignore-line
21✔
137
    }
138

139
    protected function getCommandsTime() : float
140
    {
141
        $time = .0;
21✔
142
        foreach ($this->getData() as $data) {
21✔
143
            $total = $data['end'] - $data['start'];
21✔
144
            $time += $total;
21✔
145
        }
146
        return Debugger::roundSecondsToMilliseconds($time);
21✔
147
    }
148

149
    protected function getHandler() : string
150
    {
151
        foreach ([
63✔
152
            'apcu' => ApcuCache::class,
63✔
153
            'files' => FilesCache::class,
63✔
154
            'memcached' => MemcachedCache::class,
63✔
155
            'redis' => RedisCache::class,
63✔
156
        ] as $name => $class) {
63✔
157
            if ($this->info['class'] === $class) {
63✔
158
                return $name;
63✔
159
            }
160
        }
161
        return $this->info['class'];
21✔
162
    }
163
}
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

© 2026 Coveralls, Inc