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

Introduce EnvironmentAwareMigrationInterface #530

Closed
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
9 changes: 9 additions & 0 deletions Migration/EnvironmentAwareMigrationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\Migration;

interface EnvironmentAwareMigrationInterface
{
public function setEnvironment(string $env): void;
}
15 changes: 15 additions & 0 deletions Migration/EnvironmentAwareMigrationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\Migration;

trait EnvironmentAwareMigrationTrait
{
/** @var string */
private $environment;

public function setEnvironment(string $env): void
{
$this->environment = $env;
}
}
39 changes: 39 additions & 0 deletions MigrationsFactory/EnvironmentAwareMigrationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\MigrationsFactory;

use Doctrine\Bundle\MigrationsBundle\Migration\EnvironmentAwareMigrationInterface;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Version\MigrationFactory;

class EnvironmentAwareMigrationFactory implements MigrationFactory
{
/**
* @var string
*/
private $environment;

/**
* @var MigrationFactory
*/
private $migrationFactory;

public function __construct(MigrationFactory $migrationFactory, string $environment)
{
$this->environment = $environment;
$this->migrationFactory = $migrationFactory;
}

public function createVersion(string $migrationClassName): AbstractMigration
{
$migration = $this->migrationFactory->createVersion($migrationClassName);

if ($migration instanceof EnvironmentAwareMigrationInterface) {
$migration->setEnvironment($this->environment);
}

return $migration;
}
}
8 changes: 8 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
<argument id="service_container" type="service"/>
</service>

<service id="doctrine.migrations.environment_aware_migrations_factory"
class="Doctrine\Bundle\MigrationsBundle\MigrationsFactory\EnvironmentAwareMigrationFactory"
decorates="doctrine.migrations.migrations_factory"
>
<argument id="doctrine.migrations.environment_aware_migrations_factory.inner" type="service"/>
<argument>%kernel.environment%</argument>
</service>

<service id="doctrine_migrations.diff_command" class="Doctrine\Migrations\Tools\Console\Command\DiffCommand">

<argument type="service" id="doctrine.migrations.dependency_factory"/>
Expand Down
48 changes: 48 additions & 0 deletions Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle;
use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\Migrations\ContainerAwareMigration;
use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\Migrations\EnvironmentAwareMigration;
use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\TestBundle\TestBundle;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\DependencyFactory;
Expand Down Expand Up @@ -200,6 +201,53 @@ public function testContainerAwareMigrations(): void
self::assertSame($container, $migration->getContainer());
}

/** @group legacy */
public function testEnvironmentAwareMigrations(): void
{
if (! interface_exists(ContainerAwareInterface::class)) {
self::markTestSkipped('This test requires Symfony < 7');
}

$config = [
'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
];
$container = $this->getContainer($config);

$container->compile();

$di = $container->get('doctrine.migrations.dependency_factory');
self::assertInstanceOf(DependencyFactory::class, $di);

$this->expectDeprecation('The "" service relies on the deprecated "Doctrine\Bundle\MigrationsBundle\MigrationsFactory\ContainerAwareMigrationFactory" class. It should either be deprecated or its implementation upgraded.');

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

self::assertInstanceOf(EnvironmentAwareMigration::class, $migration);
self::assertSame('test', $migration->getEnvironment());
}

public function testEnvironmentAwareMigrationsWithSymfony7(): void
{
if (interface_exists(ContainerAwareInterface::class)) {
self::markTestSkipped('This test requires Symfony > 7');
}

$config = [
'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
];
$container = $this->getContainer($config);

$container->compile();

$di = $container->get('doctrine.migrations.dependency_factory');
self::assertInstanceOf(DependencyFactory::class, $di);

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

self::assertInstanceOf(EnvironmentAwareMigration::class, $migration);
self::assertSame('test', $migration->getEnvironment());
}

public function testServicesAreLazy(): void
{
$config = [
Expand Down
24 changes: 24 additions & 0 deletions Tests/Fixtures/Migrations/EnvironmentAwareMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\Migrations;

use Doctrine\Bundle\MigrationsBundle\Migration\EnvironmentAwareMigrationInterface;
use Doctrine\Bundle\MigrationsBundle\Migration\EnvironmentAwareMigrationTrait;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

class EnvironmentAwareMigration extends AbstractMigration implements EnvironmentAwareMigrationInterface
{
use EnvironmentAwareMigrationTrait;

public function up(Schema $schema): void
{
}

public function getEnvironment(): string
{
return $this->environment;
}
}
Loading