Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate ContainerAwareMigrationFactory #518

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, Psalm starts to detect classes used by DoctrineMigrationsExtensionTest as "unused" after my change. My motivation to investigate that is rather limited, I must admit. 😓

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