Skip to content

Commit 94676cc

Browse files
authored
Deprecate ContainerAwareMigrationFactory (#518)
1 parent 68643ac commit 94676cc

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

DependencyInjection/DoctrineMigrationsExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use RuntimeException;
1414
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Definition;
1819
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -23,6 +24,7 @@
2324
use function assert;
2425
use function explode;
2526
use function implode;
27+
use function interface_exists;
2628
use function is_array;
2729
use function sprintf;
2830
use function strlen;
@@ -130,6 +132,12 @@ public function load(array $configs, ContainerBuilder $container): void
130132

131133
$container->setParameter('doctrine.migrations.preferred_em', $config['em']);
132134
$container->setParameter('doctrine.migrations.preferred_connection', $config['connection']);
135+
136+
if (interface_exists(ContainerAwareInterface::class)) {
137+
return;
138+
}
139+
140+
$container->removeDefinition('doctrine.migrations.container_aware_migrations_factory');
133141
}
134142

135143
private function checkIfBundleRelativePath(string $path, ContainerBuilder $container): string

MigrationsFactory/ContainerAwareMigrationFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1010
use Symfony\Component\DependencyInjection\ContainerInterface;
1111

12+
/**
13+
* @deprecated This class is not compatible with Symfony >= 7
14+
*/
1215
class ContainerAwareMigrationFactory implements MigrationFactory
1316
{
1417
/**
@@ -32,6 +35,8 @@ public function createVersion(string $migrationClassName): AbstractMigration
3235
$migration = $this->migrationFactory->createVersion($migrationClassName);
3336

3437
if ($migration instanceof ContainerAwareInterface) {
38+
trigger_deprecation('doctrine/doctrine-migrations-bundle', '3.3', 'Migration "%s" implements "%s" to gain access to the application\'s service container. This method is deprecated and won\'t work with Symfony 7.', $migrationClassName, ContainerAwareInterface::class);
39+
3540
$migration->setContainer($this->container);
3641
}
3742

Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use InvalidArgumentException;
2323
use PHPUnit\Framework\TestCase;
2424
use RuntimeException;
25+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
2526
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2627
use Symfony\Component\Config\FileLocator;
2728
use Symfony\Component\DependencyInjection\Alias;
@@ -31,13 +32,17 @@
3132
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3233
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
3334
use Symfony\Component\DependencyInjection\Reference;
35+
use Symfony\Component\VarExporter\LazyGhostTrait;
3436

3537
use function assert;
3638
use function interface_exists;
3739
use function sys_get_temp_dir;
40+
use function trait_exists;
3841

3942
class DoctrineMigrationsExtensionTest extends TestCase
4043
{
44+
use ExpectDeprecationTrait;
45+
4146
public function testXmlConfigs(): void
4247
{
4348
$container = $this->getContainerBuilder();
@@ -170,6 +175,7 @@ public function compare(Version $a, Version $b): int
170175
self::assertSame($sorter, $di->getVersionComparator());
171176
}
172177

178+
/** @group legacy */
173179
public function testContainerAwareMigrations(): void
174180
{
175181
if (! interface_exists(ContainerAwareInterface::class)) {
@@ -186,6 +192,8 @@ public function testContainerAwareMigrations(): void
186192
$di = $container->get('doctrine.migrations.dependency_factory');
187193
self::assertInstanceOf(DependencyFactory::class, $di);
188194

195+
$this->expectDeprecation('Since doctrine/doctrine-migrations-bundle 3.3: Migration "Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\Migrations\ContainerAwareMigration" implements "Symfony\Component\DependencyInjection\ContainerAwareInterface" to gain access to the application\'s service container. This method is deprecated and won\'t work with Symfony 7.');
196+
189197
$migration = $di->getMigrationFactory()->createVersion(ContainerAwareMigration::class);
190198

191199
self::assertInstanceOf(ContainerAwareMigration::class, $migration);
@@ -276,7 +284,8 @@ public function testPrefersEntityManagerOverConnection(): void
276284
$config = [
277285
'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
278286
];
279-
$container = $this->getContainer($config, null, []);
287+
$ormConfig = trait_exists(LazyGhostTrait::class) ? ['enable_lazy_ghost_objects' => true] : [];
288+
$container = $this->getContainer($config, null, $ormConfig);
280289

281290
$container->compile();
282291

@@ -334,12 +343,17 @@ public function testCustomEntityManager(): void
334343
'em' => 'custom',
335344
'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
336345
];
337-
$container = $this->getContainer($config, null, [
346+
$ormConfig = [
338347
'entity_managers' => [
339348
'custom' => null,
340349
'acb' => null,
341350
],
342-
]);
351+
];
352+
if (trait_exists(LazyGhostTrait::class)) {
353+
$ormConfig['enable_lazy_ghost_objects'] = true;
354+
}
355+
356+
$container = $this->getContainer($config, null, $ormConfig);
343357

344358
$container->compile();
345359

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
"require": {
2323
"php": "^7.2|^8.0",
24+
"symfony/deprecation-contracts": "^2.1 || ^3",
2425
"symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0",
2526
"doctrine/doctrine-bundle": "^2.4",
2627
"doctrine/migrations": "^3.2"
@@ -37,6 +38,8 @@
3738
"doctrine/persistence": "^2.0 || ^3 ",
3839
"psalm/plugin-phpunit": "^0.18.4",
3940
"psalm/plugin-symfony": "^3 || ^5",
41+
"symfony/phpunit-bridge": "^6.3 || ^7",
42+
"symfony/var-exporter": "^5.4 || ^6 || ^7",
4043
"vimeo/psalm": "^4.30 || ^5.15"
4144
},
4245
"autoload": {

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
</exclude>
1818
</whitelist>
1919
</filter>
20+
21+
<listeners>
22+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
23+
</listeners>
2024
</phpunit>

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
errorLevel="4"
44
phpVersion="8.2"
55
findUnusedBaselineEntry="true"
6-
findUnusedCode="true"
6+
findUnusedCode="false"
77
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88
xmlns="https://getpsalm.org/schema/config"
99
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"

0 commit comments

Comments
 (0)