Skip to content

Commit

Permalink
Deprecate ContainerAwareMigrationFactory (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Nov 13, 2023
1 parent 68643ac commit 94676cc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
8 changes: 8 additions & 0 deletions DependencyInjection/DoctrineMigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RuntimeException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand All @@ -23,6 +24,7 @@
use function assert;
use function explode;
use function implode;
use function interface_exists;
use function is_array;
use function sprintf;
use function strlen;
Expand Down Expand Up @@ -130,6 +132,12 @@ public function load(array $configs, ContainerBuilder $container): void

$container->setParameter('doctrine.migrations.preferred_em', $config['em']);
$container->setParameter('doctrine.migrations.preferred_connection', $config['connection']);

if (interface_exists(ContainerAwareInterface::class)) {
return;
}

$container->removeDefinition('doctrine.migrations.container_aware_migrations_factory');
}

private function checkIfBundleRelativePath(string $path, ContainerBuilder $container): string
Expand Down
5 changes: 5 additions & 0 deletions MigrationsFactory/ContainerAwareMigrationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* @deprecated This class is not compatible with Symfony >= 7
*/
class ContainerAwareMigrationFactory implements MigrationFactory
{
/**
Expand All @@ -32,6 +35,8 @@ public function createVersion(string $migrationClassName): AbstractMigration
$migration = $this->migrationFactory->createVersion($migrationClassName);

if ($migration instanceof ContainerAwareInterface) {
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);

$migration->setContainer($this->container);
}

Expand Down
20 changes: 17 additions & 3 deletions Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
Expand All @@ -31,13 +32,17 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function assert;
use function interface_exists;
use function sys_get_temp_dir;
use function trait_exists;

class DoctrineMigrationsExtensionTest extends TestCase
{
use ExpectDeprecationTrait;

public function testXmlConfigs(): void
{
$container = $this->getContainerBuilder();
Expand Down Expand Up @@ -170,6 +175,7 @@ public function compare(Version $a, Version $b): int
self::assertSame($sorter, $di->getVersionComparator());
}

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

$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.');

$migration = $di->getMigrationFactory()->createVersion(ContainerAwareMigration::class);

self::assertInstanceOf(ContainerAwareMigration::class, $migration);
Expand Down Expand Up @@ -276,7 +284,8 @@ public function testPrefersEntityManagerOverConnection(): void
$config = [
'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
];
$container = $this->getContainer($config, null, []);
$ormConfig = trait_exists(LazyGhostTrait::class) ? ['enable_lazy_ghost_objects' => true] : [];
$container = $this->getContainer($config, null, $ormConfig);

$container->compile();

Expand Down Expand Up @@ -334,12 +343,17 @@ public function testCustomEntityManager(): void
'em' => 'custom',
'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
];
$container = $this->getContainer($config, null, [
$ormConfig = [
'entity_managers' => [
'custom' => null,
'acb' => null,
],
]);
];
if (trait_exists(LazyGhostTrait::class)) {
$ormConfig['enable_lazy_ghost_objects'] = true;
}

$container = $this->getContainer($config, null, $ormConfig);

$container->compile();

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
"require": {
"php": "^7.2|^8.0",
"symfony/deprecation-contracts": "^2.1 || ^3",
"symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0",
"doctrine/doctrine-bundle": "^2.4",
"doctrine/migrations": "^3.2"
Expand All @@ -37,6 +38,8 @@
"doctrine/persistence": "^2.0 || ^3 ",
"psalm/plugin-phpunit": "^0.18.4",
"psalm/plugin-symfony": "^3 || ^5",
"symfony/phpunit-bridge": "^6.3 || ^7",
"symfony/var-exporter": "^5.4 || ^6 || ^7",
"vimeo/psalm": "^4.30 || ^5.15"
},
"autoload": {
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
errorLevel="4"
phpVersion="8.2"
findUnusedBaselineEntry="true"
findUnusedCode="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down

0 comments on commit 94676cc

Please sign in to comment.