Skip to content

Commit b103cb0

Browse files
Fix Doctrine deprecations
1 parent cc05652 commit b103cb0

File tree

5 files changed

+99
-8
lines changed

5 files changed

+99
-8
lines changed

Store/DoctrineDbalPostgreSqlStore.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Doctrine\DBAL\Configuration;
1415
use Doctrine\DBAL\Connection;
1516
use Doctrine\DBAL\DriverManager;
1617
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
18+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
19+
use Doctrine\DBAL\Tools\DsnParser;
20+
use Doctrine\ORM\ORMSetup;
1721
use Symfony\Component\Lock\BlockingSharedLockStoreInterface;
1822
use Symfony\Component\Lock\BlockingStoreInterface;
1923
use Symfony\Component\Lock\Exception\InvalidArgumentException;
@@ -51,7 +55,28 @@ public function __construct($connOrUrl)
5155
if (!class_exists(DriverManager::class)) {
5256
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
5357
}
54-
$this->conn = DriverManager::getConnection(['url' => $this->filterDsn($connOrUrl)]);
58+
if (class_exists(DsnParser::class)) {
59+
$params = (new DsnParser([
60+
'db2' => 'ibm_db2',
61+
'mssql' => 'pdo_sqlsrv',
62+
'mysql' => 'pdo_mysql',
63+
'mysql2' => 'pdo_mysql',
64+
'postgres' => 'pdo_pgsql',
65+
'postgresql' => 'pdo_pgsql',
66+
'pgsql' => 'pdo_pgsql',
67+
'sqlite' => 'pdo_sqlite',
68+
'sqlite3' => 'pdo_sqlite',
69+
]))->parse($this->filterDsn($connOrUrl));
70+
} else {
71+
$params = ['url' => $this->filterDsn($connOrUrl)];
72+
}
73+
74+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration() : new Configuration();
75+
if (class_exists(DefaultSchemaManagerFactory::class)) {
76+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
77+
}
78+
79+
$this->conn = DriverManager::getConnection($params, $config);
5580
} else {
5681
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be "%s" or string, "%s" given.', Connection::class, __METHOD__, get_debug_type($connOrUrl)));
5782
}

Store/DoctrineDbalStore.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Doctrine\DBAL\Configuration;
1415
use Doctrine\DBAL\Connection;
1516
use Doctrine\DBAL\DriverManager;
1617
use Doctrine\DBAL\Exception as DBALException;
1718
use Doctrine\DBAL\Exception\TableNotFoundException;
1819
use Doctrine\DBAL\ParameterType;
20+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
1921
use Doctrine\DBAL\Schema\Schema;
22+
use Doctrine\DBAL\Tools\DsnParser;
23+
use Doctrine\ORM\ORMSetup;
2024
use Symfony\Component\Lock\Exception\InvalidArgumentException;
2125
use Symfony\Component\Lock\Exception\InvalidTtlException;
2226
use Symfony\Component\Lock\Exception\LockConflictedException;
@@ -68,7 +72,28 @@ public function __construct($connOrUrl, array $options = [], float $gcProbabilit
6872
if (!class_exists(DriverManager::class)) {
6973
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
7074
}
71-
$this->conn = DriverManager::getConnection(['url' => $connOrUrl]);
75+
if (class_exists(DsnParser::class)) {
76+
$params = (new DsnParser([
77+
'db2' => 'ibm_db2',
78+
'mssql' => 'pdo_sqlsrv',
79+
'mysql' => 'pdo_mysql',
80+
'mysql2' => 'pdo_mysql',
81+
'postgres' => 'pdo_pgsql',
82+
'postgresql' => 'pdo_pgsql',
83+
'pgsql' => 'pdo_pgsql',
84+
'sqlite' => 'pdo_sqlite',
85+
'sqlite3' => 'pdo_sqlite',
86+
]))->parse($connOrUrl);
87+
} else {
88+
$params = ['url' => $connOrUrl];
89+
}
90+
91+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration() : new Configuration();
92+
if (class_exists(DefaultSchemaManagerFactory::class)) {
93+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
94+
}
95+
96+
$this->conn = DriverManager::getConnection($params, $config);
7297
} else {
7398
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be "%s" or string, "%s" given.', Connection::class, __METHOD__, get_debug_type($connOrUrl)));
7499
}

Tests/Store/DoctrineDbalPostgreSqlStoreTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Doctrine\DBAL\Configuration;
1415
use Doctrine\DBAL\Connection;
1516
use Doctrine\DBAL\DriverManager;
1617
use Doctrine\DBAL\Exception as DBALException;
18+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
19+
use Doctrine\DBAL\Tools\DsnParser;
20+
use Doctrine\ORM\ORMSetup;
1721
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1822
use Symfony\Component\Lock\Exception\LockConflictedException;
1923
use Symfony\Component\Lock\Key;
@@ -38,7 +42,7 @@ public function createPostgreSqlConnection(): Connection
3842
$this->markTestSkipped('Missing POSTGRES_HOST env variable');
3943
}
4044

41-
return DriverManager::getConnection(['url' => 'pgsql://postgres:password@'.getenv('POSTGRES_HOST')]);
45+
return self::getDbalConnection('pdo-pgsql://postgres:password@'.getenv('POSTGRES_HOST'));
4246
}
4347

4448
/**
@@ -68,7 +72,7 @@ public function testInvalidDriver($connOrDsn)
6872
public static function getInvalidDrivers()
6973
{
7074
yield ['sqlite:///tmp/foo.db'];
71-
yield [DriverManager::getConnection(['url' => 'sqlite:///tmp/foo.db'])];
75+
yield [self::getDbalConnection('sqlite:///tmp/foo.db')];
7276
}
7377

7478
public function testSaveAfterConflict()
@@ -168,4 +172,15 @@ public function testWaitAndSaveReadAfterConflictReleasesLockFromInternalStore()
168172

169173
$this->assertTrue($store2->exists($store2Key));
170174
}
175+
176+
private static function getDbalConnection(string $dsn): Connection
177+
{
178+
$params = class_exists(DsnParser::class) ? (new DsnParser(['sqlite' => 'pdo_sqlite']))->parse($dsn) : ['url' => $dsn];
179+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration(true) : new Configuration();
180+
if (class_exists(DefaultSchemaManagerFactory::class)) {
181+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
182+
}
183+
184+
return DriverManager::getConnection($params, $config);
185+
}
171186
}

Tests/Store/DoctrineDbalStoreTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Doctrine\DBAL\Configuration;
1415
use Doctrine\DBAL\Connection;
1516
use Doctrine\DBAL\DriverManager;
1617
use Doctrine\DBAL\Exception\TableNotFoundException;
1718
use Doctrine\DBAL\Platforms\AbstractPlatform;
19+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
20+
use Doctrine\ORM\ORMSetup;
1821
use Symfony\Component\Lock\Key;
1922
use Symfony\Component\Lock\PersistingStoreInterface;
2023
use Symfony\Component\Lock\Store\DoctrineDbalStore;
@@ -36,7 +39,12 @@ public static function setUpBeforeClass(): void
3639
{
3740
self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_lock');
3841

39-
$store = new DoctrineDbalStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]));
42+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration(true) : new Configuration();
43+
if (class_exists(DefaultSchemaManagerFactory::class)) {
44+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
45+
}
46+
47+
$store = new DoctrineDbalStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config));
4048
$store->createTable();
4149
}
4250

@@ -58,7 +66,12 @@ protected function getClockDelay()
5866
*/
5967
public function getStore(): PersistingStoreInterface
6068
{
61-
return new DoctrineDbalStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]));
69+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration(true) : new Configuration();
70+
if (class_exists(DefaultSchemaManagerFactory::class)) {
71+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
72+
}
73+
74+
return new DoctrineDbalStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config));
6275
}
6376

6477
public function testAbortAfterExpiration()

Tests/Store/PdoDbalStoreTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Doctrine\DBAL\Configuration;
1415
use Doctrine\DBAL\Connection;
1516
use Doctrine\DBAL\DriverManager;
17+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
1618
use Doctrine\DBAL\Schema\Schema;
19+
use Doctrine\ORM\ORMSetup;
1720
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1821
use Symfony\Component\Lock\Key;
1922
use Symfony\Component\Lock\PersistingStoreInterface;
@@ -37,7 +40,12 @@ public static function setUpBeforeClass(): void
3740
{
3841
self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_lock');
3942

40-
$store = new PdoStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]));
43+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration(true) : new Configuration();
44+
if (class_exists(DefaultSchemaManagerFactory::class)) {
45+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
46+
}
47+
48+
$store = new PdoStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config));
4149
$store->createTable();
4250
}
4351

@@ -61,7 +69,12 @@ public function getStore(): PersistingStoreInterface
6169
{
6270
$this->expectDeprecation('Since symfony/lock 5.4: Usage of a DBAL Connection with "Symfony\Component\Lock\Store\PdoStore" is deprecated and will be removed in symfony 6.0. Use "Symfony\Component\Lock\Store\DoctrineDbalStore" instead.');
6371

64-
return new PdoStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]));
72+
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration(true) : new Configuration();
73+
if (class_exists(DefaultSchemaManagerFactory::class)) {
74+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
75+
}
76+
77+
return new PdoStore(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config));
6578
}
6679

6780
public function testAbortAfterExpiration()

0 commit comments

Comments
 (0)