Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
DatabaseCollector
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
6 / 6
16
100.00% covered (success)
100.00%
1 / 1
 setDatabase
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setServerInfo
n/a
0 / 0
n/a
0 / 0
1
 getServerInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActivities
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getContents
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
6
 showHeader
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 getHostInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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 */
10namespace Framework\Database\Debug;
11
12use Framework\Database\Database;
13use Framework\Debug\Collector;
14
15/**
16 * Class DatabaseCollector.
17 *
18 * @package database
19 */
20class DatabaseCollector extends Collector
21{
22    protected Database $database;
23    /**
24     * @var string
25     *
26     * @deprecated Use {@see Database::getConnection()}
27     */
28    protected string $serverInfo;
29
30    public function setDatabase(Database $database) : static
31    {
32        $this->database = $database;
33        return $this;
34    }
35
36    /**
37     * @param string $serverInfo
38     *
39     * @deprecated Use {@see Database::getConnection()}
40     *
41     * @codeCoverageIgnore
42     */
43    public function setServerInfo(string $serverInfo) : void
44    {
45        \trigger_error(
46            'Method ' . __METHOD__ . ' is deprecated',
47            \E_USER_DEPRECATED
48        );
49        $this->serverInfo = $serverInfo;
50    }
51
52    public function getServerInfo() : string
53    {
54        return $this->database->getConnection()->server_info;
55    }
56
57    public function getActivities() : array
58    {
59        $activities = [];
60        foreach ($this->getData() as $index => $data) {
61            $activities[] = [
62                'collector' => $this->getName(),
63                'class' => static::class,
64                'description' => 'Run statement ' . ($index + 1),
65                'start' => $data['start'],
66                'end' => $data['end'],
67            ];
68        }
69        return $activities;
70    }
71
72    public function getContents() : string
73    {
74        \ob_start();
75        if ( ! isset($this->database)) {
76            echo '<p>This collector has not been added to a Database instance.</p>';
77            return \ob_get_clean(); // @phpstan-ignore-line
78        }
79        echo $this->showHeader();
80        if ( ! $this->hasData()) {
81            echo '<p>Did not run statements.</p>';
82            return \ob_get_clean(); // @phpstan-ignore-line
83        }
84        $count = \count($this->getData()); ?>
85        <p>Ran <?= $count ?> statement<?= $count === 1 ? '' : 's' ?>:</p>
86        <table>
87            <thead>
88            <tr>
89                <th>#</th>
90                <th title="Seconds">Time</th>
91                <th>Statement</th>
92                <th title="Affected rows or Rows in set">Rows</th>
93            </tr>
94            </thead>
95            <tbody>
96            <?php foreach ($this->getData() as $index => $item): ?>
97                <tr>
98                    <td><?= $index + 1 ?></td>
99                    <td><?= \round($item['end'] - $item['start'], 6) ?></td>
100                    <td>
101                        <pre><code class="language-sql"><?=
102                                \htmlentities($item['statement'])
103                ?></code></pre>
104                    </td>
105                    <td<?= isset($item['description'])
106                        ? ' title="' . \htmlentities($item['description']) . '"'
107                        : ''?>><?= \htmlentities((string) $item['rows']) ?></td>
108                </tr>
109            <?php endforeach ?>
110            </tbody>
111        </table>
112        <?php
113        return \ob_get_clean(); // @phpstan-ignore-line
114    }
115
116    protected function showHeader() : string
117    {
118        $config = $this->database->getConfig();
119        \ob_start();
120        ?>
121        <p title="<?= 'Connected to ' . \htmlentities($this->getHostInfo()) ?>">
122            <strong>Host:</strong> <?= $config['host'] ?? 'localhost' ?>
123        </p>
124        <?php
125        if (\str_contains($this->getHostInfo(), 'TCP/IP')) {
126            if (isset($config['port'])) {
127                ?>
128                <p><strong>Port:</strong> <?= \htmlentities((string) $config['port']) ?></p>
129                <?php
130            }
131        } elseif (isset($config['socket'])) { ?>
132            <p><strong>Socket:</strong> <?= \htmlentities($config['socket']) ?></p>
133            <?php
134        }
135        ?>
136        <p><strong>Server Info:</strong> <?= \htmlentities($this->getServerInfo()) ?></p>
137        <?php
138        return \ob_get_clean(); // @phpstan-ignore-line
139    }
140
141    protected function getHostInfo() : string
142    {
143        return $this->database->getConnection()->host_info;
144    }
145}