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

aplus-framework / database / 14372198759

27 Jan 2025 09:09PM UTC coverage: 99.02%. Remained the same
14372198759

push

github

natanfelles
Merge branch 'development'

2426 of 2450 relevant lines covered (99.02%)

10.85 hits per line

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

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

12
use Framework\Database\Database;
13
use Framework\Debug\Collector;
14
use Framework\Debug\Debugger;
15

16
/**
17
 * Class DatabaseCollector.
18
 *
19
 * @package database
20
 */
21
class DatabaseCollector extends Collector
22
{
23
    protected Database $database;
24

25
    public function setDatabase(Database $database) : static
26
    {
27
        $this->database = $database;
7✔
28
        return $this;
7✔
29
    }
30

31
    public function getServerInfo() : string
32
    {
33
        return $this->database->getConnection()->server_info;
5✔
34
    }
35

36
    public function getActivities() : array
37
    {
38
        $activities = [];
1✔
39
        foreach ($this->getData() as $index => $data) {
1✔
40
            $activities[] = [
1✔
41
                'collector' => $this->getName(),
1✔
42
                'class' => static::class,
1✔
43
                'description' => 'Run statement ' . ($index + 1),
1✔
44
                'start' => $data['start'],
1✔
45
                'end' => $data['end'],
1✔
46
            ];
1✔
47
        }
48
        return $activities;
1✔
49
    }
50

51
    public function getContents() : string
52
    {
53
        \ob_start();
6✔
54
        if (!isset($this->database)) {
6✔
55
            echo '<p>This collector has not been added to a Database instance.</p>';
1✔
56
            return \ob_get_clean(); // @phpstan-ignore-line
1✔
57
        }
58
        echo $this->showHeader();
5✔
59
        if (!$this->hasData()) {
5✔
60
            echo '<p>Did not run statements.</p>';
3✔
61
            return \ob_get_clean(); // @phpstan-ignore-line
3✔
62
        }
63
        $count = \count($this->getData()); ?>
2✔
64
        <p>Ran <?= $count ?> statement<?= $count === 1 ? '' : 's' ?>:</p>
2✔
65
        <table>
66
            <thead>
67
            <tr>
68
                <th>#</th>
69
                <th title="Milliseconds">Time</th>
70
                <th>Statement</th>
71
                <th title="Affected rows or Rows in set">Rows</th>
72
            </tr>
73
            </thead>
74
            <tbody>
75
            <?php foreach ($this->getData() as $index => $item): ?>
2✔
76
                <tr>
2✔
77
                    <td><?= $index + 1 ?></td>
2✔
78
                    <td><?= Debugger::roundSecondsToMilliseconds($item['end'] - $item['start']) ?></td>
2✔
79
                    <td>
80
                        <pre><code class="language-sql"><?=
2✔
81
                                \htmlentities($item['statement'])
2✔
82
                ?></code></pre>
2✔
83
                    </td>
84
                    <td<?= isset($item['description'])
2✔
85
                        ? ' title="' . \htmlentities($item['description']) . '"'
1✔
86
                        : ''?>><?= \htmlentities((string) $item['rows']) ?></td>
2✔
87
                </tr>
88
            <?php endforeach ?>
89
            </tbody>
2✔
90
        </table>
2✔
91
        <?php
2✔
92
        return \ob_get_clean(); // @phpstan-ignore-line
2✔
93
    }
94

95
    protected function showHeader() : string
96
    {
97
        $config = $this->database->getConfig();
5✔
98
        \ob_start();
5✔
99
        ?>
100
        <p title="<?= 'Connected to ' . \htmlentities($this->getHostInfo()) ?>">
5✔
101
            <strong>Host:</strong> <?= $config['host'] ?? 'localhost' ?>
5✔
102
        </p>
5✔
103
        <?php
104
        if (\str_contains($this->getHostInfo(), 'TCP/IP')) {
5✔
105
            if (isset($config['port'])) {
4✔
106
                ?>
107
                <p><strong>Port:</strong> <?= \htmlentities((string) $config['port']) ?></p>
4✔
108
                <?php
109
            }
110
        } elseif (isset($config['socket'])) { ?>
1✔
111
            <p><strong>Socket:</strong> <?= \htmlentities($config['socket']) ?></p>
1✔
112
            <?php
113
        }
114
        ?>
115
        <p><strong>Server Info:</strong> <?= \htmlentities($this->getServerInfo()) ?></p>
5✔
116
        <?php
117
        return \ob_get_clean(); // @phpstan-ignore-line
5✔
118
    }
119

120
    protected function getHostInfo() : string
121
    {
122
        return $this->database->getConnection()->host_info;
3✔
123
    }
124
}
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