Skip to content

Commit 5299938

Browse files
committed
Update debug panel to show host info
1 parent 8d6bf87 commit 5299938

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

src/Debug/DatabaseCollector.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ public function getActivities() : array
7272
public function getContents() : string
7373
{
7474
\ob_start();
75-
if ( ! isset($this->serverInfo)) {
75+
if ( ! isset($this->database)) {
7676
echo '<p>This collector has not been added to a Database instance.</p>';
7777
return \ob_get_clean(); // @phpstan-ignore-line
78-
} ?>
79-
<p><strong>Server Info:</strong> <?= $this->getServerInfo() ?></p>
80-
<?php
78+
}
79+
echo $this->showHeader();
8180
if ( ! $this->hasData()) {
8281
echo '<p>Did not run statements.</p>';
8382
return \ob_get_clean(); // @phpstan-ignore-line
@@ -111,4 +110,34 @@ public function getContents() : string
111110
<?php
112111
return \ob_get_clean(); // @phpstan-ignore-line
113112
}
113+
114+
protected function showHeader() : string
115+
{
116+
$config = $this->database->getConfig();
117+
\ob_start();
118+
?>
119+
<p title="<?= 'Connected to ' . \htmlentities($this->getHostInfo()) ?>">
120+
<strong>Host:</strong> <?= $config['host'] ?? 'localhost' ?>
121+
</p>
122+
<?php
123+
if (\str_contains($this->getHostInfo(), 'TCP/IP')) {
124+
if (isset($config['port'])) {
125+
?>
126+
<p><strong>Port:</strong> <?= \htmlentities((string) $config['port']) ?></p>
127+
<?php
128+
}
129+
} elseif (isset($config['socket'])) { ?>
130+
<p><strong>Socket:</strong> <?= \htmlentities($config['socket']) ?></p>
131+
<?php
132+
}
133+
?>
134+
<p><strong>Server Info:</strong> <?= \htmlentities($this->getServerInfo()) ?></p>
135+
<?php
136+
return \ob_get_clean(); // @phpstan-ignore-line
137+
}
138+
139+
protected function getHostInfo() : string
140+
{
141+
return $this->database->getConnection()->host_info;
142+
}
114143
}

tests/Debug/DatabaseCollectorTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,44 @@ public function testActivities() : void
8080
\array_keys($this->collector->getActivities()[0])
8181
);
8282
}
83+
84+
protected function makeDatabaseWithSocket() : Database
85+
{
86+
return new Database([
87+
'username' => \getenv('DB_USERNAME'),
88+
'password' => \getenv('DB_PASSWORD'),
89+
'schema' => \getenv('DB_SCHEMA'),
90+
'host' => \getenv('DB_HOST'),
91+
'port' => \getenv('DB_PORT'),
92+
'socket' => '/var/run/mysqld/mysqld.sock',
93+
]);
94+
}
95+
96+
public function testPort() : void
97+
{
98+
$collector = new class() extends DatabaseCollector {
99+
protected function getHostInfo() : string
100+
{
101+
return '127.0.0.1 via TCP/IP';
102+
}
103+
};
104+
$database = $this->makeDatabaseWithSocket();
105+
$database->setDebugCollector($collector);
106+
self::assertStringNotContainsString('Socket:', $collector->getContents());
107+
self::assertStringContainsString('Port:', $collector->getContents());
108+
}
109+
110+
public function testSocket() : void
111+
{
112+
$collector = new class() extends DatabaseCollector {
113+
protected function getHostInfo() : string
114+
{
115+
return 'Localhost via UNIX socket';
116+
}
117+
};
118+
$database = $this->makeDatabaseWithSocket();
119+
$database->setDebugCollector($collector);
120+
self::assertStringContainsString('Socket:', $collector->getContents());
121+
self::assertStringNotContainsString('Port:', $collector->getContents());
122+
}
83123
}

0 commit comments

Comments
 (0)