Skip to content

Commit

Permalink
fix: resolve Doctrine on L11
Browse files Browse the repository at this point in the history
  • Loading branch information
owenvoke committed Sep 20, 2024
1 parent 9f0986b commit 7eb10db
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"license": "MIT",
"require": {
"php": "^8.2",
"doctrine/dbal": "*",
"illuminate/console": "^10.0 || ^11.0",
"illuminate/database": "^10.0 | ^11.0",
"illuminate/support": "^10.0 || ^11.0",
"thecodingmachine/safe": "^2.5",
"worksome/foggy": "^0.6"
Expand Down
20 changes: 20 additions & 0 deletions src/Concerns/ConnectsToDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Worksome\FoggyLaravel\Concerns;

use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\PDO\Connection;

trait ConnectsToDatabase
{
public function connect(array $params): DriverConnection
{
if (! isset($params['pdo']) || ! $params['pdo'] instanceof \PDO) {
throw new \InvalidArgumentException('Laravel requires the "pdo" property to be set and be a PDO instance.');
}

return new Connection($params['pdo']);
}
}
29 changes: 28 additions & 1 deletion src/DatabaseDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace Worksome\FoggyLaravel;

use Doctrine\DBAL\Connection as DoctrineConnection;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use RuntimeException;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Worksome\Foggy\DumpProcess;
use Worksome\FoggyLaravel\Enums\SupportedDriver;

use function Safe\fopen;

Expand Down Expand Up @@ -38,12 +41,36 @@ public function handle(): void
};

$process = new DumpProcess(
DB::connection($this->option('connection'))->getDoctrineConnection(),
$this->getDoctrineConnection(),
$configFile,
$dumpOutput,
$consoleOutput
);

$process->run();
}

private function getDoctrineConnection(): DoctrineConnection
{
$connection = DB::connection($this->option('connection'));

if (method_exists($connection, 'getDoctrineConnection')) {
return $connection->getDoctrineConnection();
}

$driver = match ($driverName = $connection->getDriverName()) {
'mysql', 'mariadb' => SupportedDriver::MySQL,
'pgsql', 'postgres' => SupportedDriver::PostgreSQL,
'sqlite' => SupportedDriver::SQLite,
'sqlsrv' => SupportedDriver::SqlServer,
default => throw new RuntimeException("Unsupported database driver provided ({$driverName})."),
};

return new DoctrineConnection(array_filter([
'pdo' => $connection->getPdo(),
'dbname' => $connection->getDatabaseName(),
'driver' => $driver->driverName(),
'serverVersion' => $connection->getConfig('server_version'),
]), $driver->driver());
}
}
48 changes: 48 additions & 0 deletions src/Enums/SupportedDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Worksome\FoggyLaravel\Enums;

use Doctrine\DBAL\Driver;
use Worksome\FoggyLaravel\Concerns\ConnectsToDatabase;

enum SupportedDriver
{
case MySQL;
case PostgreSQL;
case SQLite;
case SqlServer;

public function driverName(): string
{
return match ($this) {
self::MySQL => 'pdo_mysql',
self::PostgreSQL => 'pdo_pgsql',
self::SQLite => 'pdo_sqlite',
self::SqlServer => 'pdo_sqlsrv',
};
}

public function driver(): Driver
{
return match ($this) {
self::MySQL => new class extends Driver\AbstractMySQLDriver
{
use ConnectsToDatabase;
},
self::PostgreSQL => new class extends Driver\AbstractPostgreSQLDriver
{
use ConnectsToDatabase;
},
self::SQLite => new class extends Driver\AbstractSQLiteDriver
{
use ConnectsToDatabase;
},
self::SqlServer => new class extends Driver\AbstractSQLServerDriver
{
use ConnectsToDatabase;
},
};
}
}

0 comments on commit 7eb10db

Please sign in to comment.