Files
AeonDumpManager/src/Service/DumpBinaryLocator.php
T

49 lines
1.6 KiB
PHP

<?php declare(strict_types=1);
namespace AeonDumpManager\Service;
use AeonDumpManager\Service\Exception\DumpBinaryNotFoundException;
use Doctrine\DBAL\Connection;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Process\ExecutableFinder;
/**
* Picks mariadb-dump/mysqldump depending on what the configured DB connection
* actually is, mirroring Doctrine DBAL's own stripos($version, 'mariadb')
* version-string check (no shared platform base class to rely on instead).
*/
class DumpBinaryLocator
{
public function __construct(
private readonly Connection $connection,
private readonly SystemConfigService $systemConfigService,
private readonly ExecutableFinder $executableFinder,
) {
}
public function locate(): string
{
$configuredName = $this->systemConfigService->getString('AeonDumpManager.config.dumpBinaryName');
$binaryNames = $configuredName !== ''
? [$configuredName]
: ($this->isMariaDb() ? ['mariadb-dump', 'mysqldump'] : ['mysqldump']);
foreach ($binaryNames as $binaryName) {
$path = $this->executableFinder->find($binaryName);
if ($path !== null) {
return $path;
}
}
throw DumpBinaryNotFoundException::create($binaryNames);
}
private function isMariaDb(): bool
{
// Connection::getServerVersion() is private in the DBAL version this
// repo pins against — SELECT VERSION() is the stable public way to get
// the same string.
return MySqlVariant::isMariaDb((string) $this->connection->fetchOne('SELECT VERSION()'));
}
}