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 df57901
Show file tree
Hide file tree
Showing 3 changed files with 47 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']);
}
}
26 changes: 25 additions & 1 deletion src/DatabaseDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Worksome\FoggyLaravel;

use Doctrine\DBAL\Connection as DoctrineConnection;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand Down Expand Up @@ -38,12 +40,34 @@ 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'));

$driverName = match ($connection->getDriverName()) {
'mysql' => 'pdo_mysql',
};

$driver = match ($driverName) {
'pdo_mysql' => new class extends AbstractMySQLDriver {
use Concerns\ConnectsToDatabase;
},
};

return new DoctrineConnection(array_filter([
'pdo' => $connection->getPdo(),
'dbname' => $connection->getDatabaseName(),
'driver' => $driverName,
'serverVersion' => $connection->getConfig('server_version'),
]), $driver);
}
}

0 comments on commit df57901

Please sign in to comment.