-
-
Notifications
You must be signed in to change notification settings - Fork 220
Add service migrations #629
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/DependencyInjection/CompilerPass/RegisterMigrationsPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass; | ||
|
|
||
| use Doctrine\DBAL\Connection; | ||
| use Psr\Log\LoggerInterface; | ||
| use Symfony\Component\DependencyInjection\Argument\BoundArgument; | ||
| use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| use Symfony\Component\DependencyInjection\TypedReference; | ||
|
|
||
| /** @internal */ | ||
| final class RegisterMigrationsPass implements CompilerPassInterface | ||
| { | ||
| public function process(ContainerBuilder $container): void | ||
| { | ||
| if (! $container->hasDefinition('doctrine.migrations.service_migrations_repository')) { | ||
| return; | ||
| } | ||
|
|
||
| $migrationRefs = []; | ||
|
|
||
| foreach ($container->findTaggedServiceIds('doctrine_migrations.migration', true) as $id => $attributes) { | ||
| $definition = $container->getDefinition($id); | ||
| $definition->setBindings([ | ||
| Connection::class => new BoundArgument(new Reference('doctrine.migrations.connection'), false), | ||
| LoggerInterface::class => new BoundArgument(new Reference('doctrine.migrations.logger'), false), | ||
| ]); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $migrationRefs[$id] = new TypedReference($id, $definition->getClass()); | ||
| } | ||
|
|
||
| $container->getDefinition('doctrine.migrations.service_migrations_repository') | ||
| ->replaceArgument(0, new ServiceLocatorArgument($migrationRefs)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\MigrationsBundle\MigrationsRepository; | ||
|
|
||
| use Doctrine\Migrations\AbstractMigration; | ||
| use Doctrine\Migrations\Exception\MigrationClassNotFound; | ||
| use Doctrine\Migrations\Metadata\AvailableMigration; | ||
| use Doctrine\Migrations\Metadata\AvailableMigrationsSet; | ||
| use Doctrine\Migrations\MigrationsRepository; | ||
| use Doctrine\Migrations\Version\Version; | ||
| use Symfony\Contracts\Service\ServiceProviderInterface; | ||
|
|
||
| /** @internal */ | ||
| final class ServiceMigrationsRepository implements MigrationsRepository | ||
| { | ||
| /** @var ServiceProviderInterface<AbstractMigration> */ | ||
| private $container; | ||
|
|
||
| /** @var array<string, AvailableMigration> */ | ||
| private $migrations = []; | ||
|
|
||
| /** @param ServiceProviderInterface<AbstractMigration> $container */ | ||
| public function __construct(ServiceProviderInterface $container) | ||
| { | ||
| $this->container = $container; | ||
| } | ||
|
|
||
| public function hasMigration(string $version): bool | ||
| { | ||
| return isset($this->migrations[$version]) || $this->container->has($version); | ||
| } | ||
|
|
||
| public function getMigration(Version $version): AvailableMigration | ||
| { | ||
| $this->loadMigrationFromContainer($version); | ||
|
|
||
| return $this->migrations[(string) $version]; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a non-sorted set of migrations. | ||
| */ | ||
| public function getMigrations(): AvailableMigrationsSet | ||
| { | ||
| foreach ($this->container->getProvidedServices() as $id) { | ||
| $this->loadMigrationFromContainer(new Version($id)); | ||
| } | ||
|
|
||
| return new AvailableMigrationsSet($this->migrations); | ||
| } | ||
|
|
||
| private function loadMigrationFromContainer(Version $version): void | ||
| { | ||
| $id = (string) $version; | ||
|
|
||
| if (isset($this->migrations[$id])) { | ||
| return; | ||
| } | ||
|
|
||
| if (! $this->container->has($id)) { | ||
| throw MigrationClassNotFound::new($id); | ||
| } | ||
|
|
||
| $this->migrations[$id] = new AvailableMigration($version, $this->container->get($id)); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
tests/DependencyInjection/CompilerPass/RegisterMigrationsPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\MigrationsBundle\Tests\DependencyInjection\CompilerPass; | ||
|
|
||
| use Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass\RegisterMigrationsPass; | ||
| use Doctrine\Bundle\MigrationsBundle\MigrationsRepository\ServiceMigrationsRepository; | ||
| use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\Migrations\Migration001; | ||
| use Doctrine\DBAL\Connection; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Psr\Log\LoggerInterface; | ||
| use Symfony\Component\DependencyInjection\Argument\AbstractArgument; | ||
| use Symfony\Component\DependencyInjection\Argument\BoundArgument; | ||
| use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| use Symfony\Component\DependencyInjection\TypedReference; | ||
|
|
||
| class RegisterMigrationsPassTest extends TestCase | ||
| { | ||
| public function testProcessWhenServiceMigrationsRepositoryIsRegistered(): void | ||
| { | ||
| $container = new ContainerBuilder(); | ||
| $container->register('doctrine.migrations.service_migrations_repository', ServiceMigrationsRepository::class) | ||
| ->addArgument(new AbstractArgument()); | ||
| $container->register(Migration001::class, Migration001::class)->addTag('doctrine_migrations.migration'); | ||
|
|
||
| $pass = new RegisterMigrationsPass(); | ||
| $pass->process($container); | ||
|
|
||
| $argument = $container->getDefinition('doctrine.migrations.service_migrations_repository')->getArgument(0); | ||
| self::assertEquals( | ||
| new ServiceLocatorArgument([Migration001::class => new TypedReference(Migration001::class, Migration001::class)]), | ||
| $argument | ||
| ); | ||
|
|
||
| self::assertEquals([ | ||
| Connection::class => new BoundArgument(new Reference('doctrine.migrations.connection'), false), | ||
| LoggerInterface::class => new BoundArgument(new Reference('doctrine.migrations.logger'), false), | ||
| ], $container->getDefinition(Migration001::class)->getBindings()); | ||
| } | ||
|
|
||
| public function testProcessWhenServiceMigrationsRepositoryIsNotRegistered(): void | ||
| { | ||
| $container = new ContainerBuilder(); | ||
|
|
||
| $pass = new RegisterMigrationsPass(); | ||
| $pass->process($container); | ||
|
|
||
| self::assertFalse($container->hasDefinition('doctrine.migrations.service_migrations_repository')); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.