Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8d8f99

Browse files
Merge branch '5.4' into 6.2
* 5.4: [Cache] Fix tests [DependencyInjection] Allow casting env var processors to cast null [Cache] Fix DBAL deprecations Fix broken symlink tests Windows PHP 7.4+ [Serializer] Fix discriminator map not working with `AbstractNormalizer::OBJECT_TO_POPULATE`
2 parents 1ce7ed8 + eb8f63d commit b8d8f99

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

Adapter/DoctrineDbalAdapter.php

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

1212
namespace Symfony\Component\Cache\Adapter;
1313

14+
use Doctrine\DBAL\ArrayParameterType;
15+
use Doctrine\DBAL\Configuration;
1416
use Doctrine\DBAL\Connection;
1517
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1618
use Doctrine\DBAL\DriverManager;
1719
use Doctrine\DBAL\Exception as DBALException;
1820
use Doctrine\DBAL\Exception\TableNotFoundException;
1921
use Doctrine\DBAL\ParameterType;
22+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
2023
use Doctrine\DBAL\Schema\Schema;
24+
use Doctrine\DBAL\Tools\DsnParser;
2125
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2226
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
2327
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
@@ -66,7 +70,28 @@ public function __construct(Connection|string $connOrDsn, string $namespace = ''
6670
if (!class_exists(DriverManager::class)) {
6771
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrDsn));
6872
}
69-
$this->conn = DriverManager::getConnection(['url' => $connOrDsn]);
73+
if (class_exists(DsnParser::class)) {
74+
$params = (new DsnParser([
75+
'db2' => 'ibm_db2',
76+
'mssql' => 'pdo_sqlsrv',
77+
'mysql' => 'pdo_mysql',
78+
'mysql2' => 'pdo_mysql',
79+
'postgres' => 'pdo_pgsql',
80+
'postgresql' => 'pdo_pgsql',
81+
'pgsql' => 'pdo_pgsql',
82+
'sqlite' => 'pdo_sqlite',
83+
'sqlite3' => 'pdo_sqlite',
84+
]))->parse($connOrDsn);
85+
} else {
86+
$params = ['url' => $connOrDsn];
87+
}
88+
89+
$config = new Configuration();
90+
if (class_exists(DefaultSchemaManagerFactory::class)) {
91+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
92+
}
93+
94+
$this->conn = DriverManager::getConnection($params, $config);
7095
}
7196

7297
$this->table = $options['db_table'] ?? $this->table;
@@ -143,7 +168,7 @@ protected function doFetch(array $ids): iterable
143168
$ids,
144169
], [
145170
ParameterType::INTEGER,
146-
Connection::PARAM_STR_ARRAY,
171+
class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY,
147172
])->iterateNumeric();
148173

149174
foreach ($result as $row) {
@@ -161,7 +186,7 @@ protected function doFetch(array $ids): iterable
161186
$expired,
162187
], [
163188
ParameterType::INTEGER,
164-
Connection::PARAM_STR_ARRAY,
189+
class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY,
165190
]);
166191
}
167192
}
@@ -204,7 +229,7 @@ protected function doDelete(array $ids): bool
204229
{
205230
$sql = "DELETE FROM $this->table WHERE $this->idCol IN (?)";
206231
try {
207-
$this->conn->executeStatement($sql, [array_values($ids)], [Connection::PARAM_STR_ARRAY]);
232+
$this->conn->executeStatement($sql, [array_values($ids)], [class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY]);
208233
} catch (TableNotFoundException) {
209234
}
210235

@@ -260,35 +285,42 @@ protected function doSave(array $values, int $lifetime): array|bool
260285
$stmt = $this->conn->prepare($sql);
261286
}
262287

263-
// $id and $data are defined later in the loop. Binding is done by reference, values are read on execution.
264288
if ('sqlsrv' === $platformName || 'oci' === $platformName) {
265-
$stmt->bindParam(1, $id);
266-
$stmt->bindParam(2, $id);
267-
$stmt->bindParam(3, $data, ParameterType::LARGE_OBJECT);
289+
$bind = static function ($id, $data) use ($stmt) {
290+
$stmt->bindValue(1, $id);
291+
$stmt->bindValue(2, $id);
292+
$stmt->bindValue(3, $data, ParameterType::LARGE_OBJECT);
293+
$stmt->bindValue(6, $data, ParameterType::LARGE_OBJECT);
294+
};
268295
$stmt->bindValue(4, $lifetime, ParameterType::INTEGER);
269296
$stmt->bindValue(5, $now, ParameterType::INTEGER);
270-
$stmt->bindParam(6, $data, ParameterType::LARGE_OBJECT);
271297
$stmt->bindValue(7, $lifetime, ParameterType::INTEGER);
272298
$stmt->bindValue(8, $now, ParameterType::INTEGER);
273299
} elseif (null !== $platformName) {
274-
$stmt->bindParam(1, $id);
275-
$stmt->bindParam(2, $data, ParameterType::LARGE_OBJECT);
300+
$bind = static function ($id, $data) use ($stmt) {
301+
$stmt->bindValue(1, $id);
302+
$stmt->bindValue(2, $data, ParameterType::LARGE_OBJECT);
303+
};
276304
$stmt->bindValue(3, $lifetime, ParameterType::INTEGER);
277305
$stmt->bindValue(4, $now, ParameterType::INTEGER);
278306
} else {
279-
$stmt->bindParam(1, $data, ParameterType::LARGE_OBJECT);
280307
$stmt->bindValue(2, $lifetime, ParameterType::INTEGER);
281308
$stmt->bindValue(3, $now, ParameterType::INTEGER);
282-
$stmt->bindParam(4, $id);
283309

284310
$insertStmt = $this->conn->prepare($insertSql);
285-
$insertStmt->bindParam(1, $id);
286-
$insertStmt->bindParam(2, $data, ParameterType::LARGE_OBJECT);
287311
$insertStmt->bindValue(3, $lifetime, ParameterType::INTEGER);
288312
$insertStmt->bindValue(4, $now, ParameterType::INTEGER);
313+
314+
$bind = static function ($id, $data) use ($stmt, $insertStmt) {
315+
$stmt->bindValue(1, $data, ParameterType::LARGE_OBJECT);
316+
$stmt->bindValue(4, $id);
317+
$insertStmt->bindValue(1, $id);
318+
$insertStmt->bindValue(2, $data, ParameterType::LARGE_OBJECT);
319+
};
289320
}
290321

291322
foreach ($values as $id => $data) {
323+
$bind($id, $data);
292324
try {
293325
$rowCount = $stmt->executeStatement();
294326
} catch (TableNotFoundException) {

Tests/Adapter/DoctrineDbalAdapterTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
1717
use Doctrine\DBAL\Driver\Middleware;
1818
use Doctrine\DBAL\DriverManager;
19+
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
1920
use Doctrine\DBAL\Schema\Schema;
2021
use PHPUnit\Framework\SkippedTestSuiteError;
2122
use Psr\Cache\CacheItemPoolInterface;
@@ -45,12 +46,12 @@ public static function tearDownAfterClass(): void
4546

4647
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
4748
{
48-
return new DoctrineDbalAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
49+
return new DoctrineDbalAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig()), '', $defaultLifetime);
4950
}
5051

5152
public function testConfigureSchemaDecoratedDbalDriver()
5253
{
53-
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
54+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig());
5455
if (!interface_exists(Middleware::class)) {
5556
$this->markTestSkipped('doctrine/dbal v2 does not support custom drivers using middleware');
5657
}
@@ -60,7 +61,7 @@ public function testConfigureSchemaDecoratedDbalDriver()
6061
->method('wrap')
6162
->willReturn(new DriverWrapper($connection->getDriver()));
6263

63-
$config = new Configuration();
64+
$config = $this->getDbalConfig();
6465
$config->setMiddlewares([$middleware]);
6566

6667
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $config);
@@ -75,7 +76,7 @@ public function testConfigureSchemaDecoratedDbalDriver()
7576

7677
public function testConfigureSchema()
7778
{
78-
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
79+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig());
7980
$schema = new Schema();
8081

8182
$adapter = new DoctrineDbalAdapter($connection);
@@ -95,7 +96,7 @@ public function testConfigureSchemaDifferentDbalConnection()
9596

9697
public function testConfigureSchemaTableExists()
9798
{
98-
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]);
99+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile], $this->getDbalConfig());
99100
$schema = new Schema();
100101
$schema->createTable('cache_items');
101102

@@ -154,4 +155,14 @@ private function createConnectionMock()
154155

155156
return $connection;
156157
}
158+
159+
private function getDbalConfig()
160+
{
161+
$config = new Configuration();
162+
if (class_exists(DefaultSchemaManagerFactory::class)) {
163+
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
164+
}
165+
166+
return $config;
167+
}
157168
}

0 commit comments

Comments
 (0)