39 lines
1.4 KiB
PHP
39 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
use SensitiveParameter;
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
|
|
|
/** The test button and business readers share one driver implementation. */
|
|
class UpstreamDatabaseProbe
|
|
{
|
|
public function probe(string $driver, #[SensitiveParameter] array $connection, string $label): void
|
|
{
|
|
if ($driver === 'sqlsrv') {
|
|
UpstreamOdbcConnection::probeConfiguration($connection, $label);
|
|
return;
|
|
}
|
|
if (! in_array($driver, ['pgsql', 'mysql'], true)) {
|
|
throw new ServiceUnavailableHttpException(30, '不支持此数据库类型。');
|
|
}
|
|
try {
|
|
$pdo = $this->connect($driver, $connection);
|
|
$statement = $pdo->query('SELECT 1');
|
|
if ($statement === false || (int) $statement->fetchColumn() !== 1) {
|
|
throw new ServiceUnavailableHttpException(30, '只读测试未返回预期结果。');
|
|
}
|
|
$statement->closeCursor();
|
|
} catch (PDOException) {
|
|
throw new ServiceUnavailableHttpException(30, '上游只读连接失败,请检查账号、权限、网络和 TLS 配置。');
|
|
}
|
|
}
|
|
|
|
protected function connect(string $driver, #[SensitiveParameter] array $connection): PDO
|
|
{
|
|
return UpstreamConnection::openNative($driver, $connection);
|
|
}
|
|
}
|