Skip to content

Commit a419fda

Browse files
authored
Merge pull request #1979 from greg0ire/remove-dbal-3-test
Remove type and method existence based checks
2 parents d9e7e39 + d9c647f commit a419fda

File tree

7 files changed

+84
-224
lines changed

7 files changed

+84
-224
lines changed

UPGRADE-3.0.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ Auto mapping used any route parameter that matches with a field name of the Enti
6262

6363
If you were relying on this functionality, you will need to update your code to use explicit mapped route parameters instead.
6464

65+
ConnectionFactory::createConnection() signature change
66+
------------------------------------------------------
67+
68+
The signature of `ConnectionFactory::createConnection()` changed.
69+
You should use stop passing an event manager argument.
70+
71+
```diff
72+
- $connectionFactory->createConnection($params, $config, $eventManager, $mappingTypes)
73+
+ $connectionFactory->createConnection($params, $config, $mappingTypes)
74+
```
75+
6576
Types
6677
-----
6778

src/ConnectionFactory.php

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Doctrine\Bundle\DoctrineBundle;
44

5-
use Doctrine\Common\EventManager;
65
use Doctrine\DBAL\Configuration;
76
use Doctrine\DBAL\Connection;
87
use Doctrine\DBAL\Connection\StaticServerVersionProvider;
@@ -17,15 +16,10 @@
1716
use Doctrine\DBAL\Platforms\AbstractPlatform;
1817
use Doctrine\DBAL\Tools\DsnParser;
1918
use Doctrine\DBAL\Types\Type;
20-
use Doctrine\Deprecations\Deprecation;
21-
use InvalidArgumentException;
2219

2320
use function array_merge;
2421
use function class_exists;
25-
use function func_num_args;
26-
use function is_array;
2722
use function is_subclass_of;
28-
use function method_exists;
2923
use function trigger_deprecation;
3024

3125
use const PHP_EOL;
@@ -62,52 +56,17 @@ public function __construct(
6256
/**
6357
* Create a connection by name.
6458
*
65-
* @param mixed[] $params
66-
* @param EventManager|array<string, string>|null $eventManagerOrMappingTypes
67-
* @param array<string, string> $deprecatedMappingTypes
59+
* @param mixed[] $params
60+
* @param array<string, string> $mappingTypes
6861
* @phpstan-param Params $params
6962
*
7063
* @return Connection
71-
*
72-
* @no-named-arguments
7364
*/
7465
public function createConnection(
7566
array $params,
7667
Configuration|null $config = null,
77-
EventManager|array|null $eventManagerOrMappingTypes = [],
78-
array $deprecatedMappingTypes = [],
68+
array $mappingTypes = [],
7969
) {
80-
if (! method_exists(Connection::class, 'getEventManager') && $eventManagerOrMappingTypes instanceof EventManager) {
81-
throw new InvalidArgumentException('Passing an EventManager instance is not supported with DBAL > 3');
82-
}
83-
84-
if (is_array($eventManagerOrMappingTypes) && func_num_args() === 4) {
85-
throw new InvalidArgumentException('Passing mapping types both as 3rd and 4th argument makes no sense.');
86-
}
87-
88-
if ($eventManagerOrMappingTypes instanceof EventManager) {
89-
// DBAL 3
90-
$eventManager = $eventManagerOrMappingTypes;
91-
$mappingTypes = $deprecatedMappingTypes;
92-
} elseif (is_array($eventManagerOrMappingTypes)) {
93-
// Future signature
94-
$eventManager = null;
95-
$mappingTypes = $eventManagerOrMappingTypes;
96-
} else {
97-
// Legacy signature
98-
if (! method_exists(Connection::class, 'getEventManager')) {
99-
Deprecation::trigger(
100-
'doctrine/doctrine-bundle',
101-
'https://github.com/doctrine/DoctrineBundle/pull/1976',
102-
'Passing mapping types as 4th argument to %s is deprecated when using DBAL 4 and will not be supported in version 3.0 of the bundle. Pass them as 3rd argument instead.',
103-
__METHOD__,
104-
);
105-
}
106-
107-
$eventManager = null;
108-
$mappingTypes = $deprecatedMappingTypes;
109-
}
110-
11170
if (! $this->initialized) {
11271
$this->initializeTypes();
11372
}
@@ -151,16 +110,12 @@ public function createConnection(
151110
$params['wrapperClass'] = null;
152111
}
153112

154-
$connection = DriverManager::getConnection(...array_merge([$params, $config], $eventManager ? [$eventManager] : []));
113+
$connection = DriverManager::getConnection($params, $config);
155114
$params = $this->addDatabaseSuffix(array_merge($connection->getParams(), $overriddenOptions));
156115
$driver = $connection->getDriver();
157-
/** @phpstan-ignore arguments.count (DBAL < 4.x doesn't accept an argument) */
158-
$platform = $driver->getDatabasePlatform(
159-
...(class_exists(StaticServerVersionProvider::class)
160-
? [new StaticServerVersionProvider($params['serverVersion'] ?? $params['primary']['serverVersion'] ?? '')]
161-
: []
162-
),
163-
);
116+
$platform = $driver->getDatabasePlatform(new StaticServerVersionProvider(
117+
$params['serverVersion'] ?? $params['primary']['serverVersion'] ?? '',
118+
));
164119

165120
if (! isset($params['charset'])) {
166121
if ($platform instanceof AbstractMySQLPlatform) {
@@ -179,9 +134,9 @@ public function createConnection(
179134
$wrapperClass = Connection::class;
180135
}
181136

182-
$connection = new $wrapperClass($params, $driver, $config, $eventManager);
137+
$connection = new $wrapperClass($params, $driver, $config);
183138
} else {
184-
$connection = DriverManager::getConnection(...array_merge([$params, $config], $eventManager ? [$eventManager] : []));
139+
$connection = DriverManager::getConnection($params, $config);
185140
}
186141

187142
if (! empty($mappingTypes)) {
@@ -209,10 +164,7 @@ private function getDatabasePlatform(Connection $connection): AbstractPlatform
209164
try {
210165
return $connection->getDatabasePlatform();
211166
} catch (DriverException $driverException) {
212-
$class = class_exists(DBALException::class) ? DBALException::class : ConnectionException::class;
213-
214-
/* @phpstan-ignore new.interface */
215-
throw new $class(
167+
throw new ConnectionException(
216168
'An exception occurred while establishing a connection to figure out your platform version.' . PHP_EOL .
217169
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
218170
'For further information have a look at:' . PHP_EOL .
@@ -305,11 +257,7 @@ private function parseDatabaseUrl(array $params): array
305257
// If a schemeless connection URL is given, we require a default driver or default custom driver
306258
// as connection parameter.
307259
if (! isset($params['driverClass']) && ! isset($params['driver'])) {
308-
if (class_exists(DriverRequired::class)) {
309-
throw DriverRequired::new($params['url']);
310-
}
311-
312-
throw DBALException::driverRequired($params['url']);
260+
throw DriverRequired::new($params['url']);
313261
}
314262

315263
unset($params['url']);

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use function is_int;
3232
use function is_string;
3333
use function key;
34-
use function method_exists;
3534
use function reset;
3635
use function sprintf;
3736
use function strlen;
@@ -496,7 +495,7 @@ private function addOrmSection(ArrayNodeDefinition $node): void
496495
->end()
497496
->end()
498497
->booleanNode('enable_lazy_ghost_objects')
499-
->defaultValue(! method_exists(ProxyFactory::class, 'resetUninitializedProxy'))
498+
->defaultValue(true)
500499
->info('Enables the new implementation of proxies based on lazy ghosts instead of using the legacy implementation')
501500
->end()
502501
->booleanNode('enable_native_lazy_objects')

src/DependencyInjection/DoctrineExtension.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
use Doctrine\ORM\Mapping\MappedSuperclass;
3131
use Doctrine\ORM\ORMSetup;
3232
use Doctrine\ORM\Proxy\Autoloader;
33-
use Doctrine\ORM\Proxy\ProxyFactory;
34-
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
3533
use Doctrine\ORM\UnitOfWork;
3634
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
3735
use Doctrine\Persistence\Mapping\Driver\PHPDriver;
@@ -299,12 +297,11 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
299297
$def = $container
300298
->setDefinition($connectionId, new ChildDefinition('doctrine.dbal.connection'))
301299
->setPublic(true)
302-
->setArguments(array_merge(
303-
[$options, new Reference(sprintf('doctrine.dbal.%s_connection.configuration', $name))],
304-
// event manager must only be passed for DBAL < 4
305-
method_exists(Connection::class, 'getEventManager') ? [new Reference(sprintf('doctrine.dbal.%s_connection.event_manager', $name))] : [],
306-
[$connection['mapping_types']],
307-
));
300+
->setArguments([
301+
$options,
302+
new Reference(sprintf('doctrine.dbal.%s_connection.configuration', $name)),
303+
$connection['mapping_types'],
304+
]);
308305

309306
$container
310307
->registerAliasForArgument($connectionId, Connection::class, sprintf('%s.connection', $name))
@@ -316,13 +313,8 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
316313
}
317314

318315
if (isset($connection['use_savepoints'])) {
319-
// DBAL >= 4 always has savepoints enabled. So we only need to call "setNestTransactionsWithSavepoints" for DBAL < 4
320-
if (method_exists(Connection::class, 'getEventManager')) {
321-
if ($connection['use_savepoints']) {
322-
$def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]);
323-
}
324-
} elseif (! $connection['use_savepoints']) {
325-
throw new LogicException('The "use_savepoints" option can only be set to "true" and should ideally not be set when using DBAL >= 4');
316+
if (! $connection['use_savepoints']) {
317+
throw new LogicException('The "use_savepoints" option can only be set to "true" and should ideally not be set');
326318
}
327319
}
328320

@@ -510,10 +502,6 @@ protected function ormLoad(array $config, ContainerBuilder $container)
510502
// Symfony 7.3 and higher expose type alias support in the EntityValueResolver
511503
$valueResolverDefinition->setArgument(3, $config['resolve_target_entities']);
512504

513-
if (! class_exists(ClassMetadataExporter::class)) {
514-
$container->removeDefinition('doctrine.mapping_import_command');
515-
}
516-
517505
$entityManagers = [];
518506
foreach (array_keys($config['entity_managers']) as $name) {
519507
$entityManagers[$name] = sprintf('doctrine.orm.%s_entity_manager', $name);
@@ -528,19 +516,17 @@ protected function ormLoad(array $config, ContainerBuilder $container)
528516

529517
$container->setParameter('doctrine.default_entity_manager', $config['default_entity_manager']);
530518

531-
if ($config['enable_lazy_ghost_objects'] ?? false) {
532-
if (! class_exists(ProxyHelper::class)) {
533-
throw new LogicException(
534-
'Lazy ghost objects cannot be enabled because the "symfony/var-exporter" library'
535-
. ' is not installed. Please run "composer require symfony/var-exporter".',
536-
);
537-
}
538-
} elseif (! method_exists(ProxyFactory::class, 'resetUninitializedProxy')) {
519+
if (! ($config['enable_lazy_ghost_objects'] ?? false)) {
539520
throw new LogicException(
540521
'Lazy ghost objects cannot be disabled for ORM 3.',
541522
);
542-
} else {
543-
trigger_deprecation('doctrine/doctrine-bundle', '2.11', 'Not setting "doctrine.orm.enable_lazy_ghost_objects" to true is deprecated.');
523+
}
524+
525+
if (! class_exists(ProxyHelper::class)) {
526+
throw new LogicException(
527+
'Lazy ghost objects cannot be enabled because the "symfony/var-exporter" library'
528+
. ' is not installed. Please run "composer require symfony/var-exporter".',
529+
);
544530
}
545531

546532
if ($config['enable_native_lazy_objects'] ?? false) {

tests/ConnectionFactoryTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
use Doctrine\DBAL\Driver;
99
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
1010
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
11-
use InvalidArgumentException;
1211

1312
use function array_intersect_key;
14-
use function method_exists;
1513

1614
class ConnectionFactoryTest extends TestCase
1715
{
@@ -139,34 +137,6 @@ public function testDbnameSuffixForReplicas(): void
139137
$this->assertSame('primary_test', $parsedParams['primary']['dbname']);
140138
$this->assertSame('replica_test', $parsedParams['replica']['replica1']['dbname']);
141139
}
142-
143-
public function testItThrowsWhenPassingMappingTypesTwice(): void
144-
{
145-
$this->expectException(InvalidArgumentException::class);
146-
147-
(new ConnectionFactory())->createConnection(['driver' => 'pdo_sqlite'], null, [], []);
148-
}
149-
150-
/** @group legacy */
151-
public function testPassingMappingTypesAsFourthArgumentIsDeprecatedWithDbal4(): void
152-
{
153-
if (method_exists(Connection::class, 'getEventManager')) {
154-
$this->markTestSkipped('DBAL 3 does not trigger the deprecation.');
155-
}
156-
157-
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/DoctrineBundle/pull/1976');
158-
(new ConnectionFactory())->createConnection(['driver' => 'pdo_sqlite'], null, null, []);
159-
}
160-
161-
public function testPassingMappingTypesAsFourthArgumentIsFineWithDbal3(): void
162-
{
163-
if (! method_exists(Connection::class, 'getEventManager')) {
164-
$this->markTestSkipped('DBAL 4 triggers the deprecation.');
165-
}
166-
167-
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/DoctrineBundle/pull/1976');
168-
(new ConnectionFactory())->createConnection(['driver' => 'pdo_sqlite'], null, null, []);
169-
}
170140
}
171141

172142
class FakeConnection extends Connection

0 commit comments

Comments
 (0)