|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass; |
| 6 | + |
| 7 | +use Doctrine\Bundle\MigrationsBundle\AbstractServiceMigration; |
| 8 | +use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
| 9 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 10 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 12 | +use Symfony\Component\DependencyInjection\TypedReference; |
| 13 | + |
| 14 | +use function is_subclass_of; |
| 15 | + |
| 16 | +class RegisterMigrationsPass implements CompilerPassInterface |
| 17 | +{ |
| 18 | + public function process(ContainerBuilder $container): void |
| 19 | + { |
| 20 | + $migrationRefs = []; |
| 21 | + |
| 22 | + foreach ($container->findTaggedServiceIds('doctrine_migrations.migration', true) as $id => $attributes) { |
| 23 | + $class = $container->getDefinition($id)->getClass(); |
| 24 | + if (is_subclass_of($class, AbstractServiceMigration::class)) { |
| 25 | + $definition = new ChildDefinition('doctrine_migrations.abstract_migration'); |
| 26 | + $definition->setClass($class); |
| 27 | + $definition->addTag('container.service_subscriber'); |
| 28 | + |
| 29 | + $container->setDefinition($id, $definition); |
| 30 | + |
| 31 | + $migrationRefs[$id] = new TypedReference($id, $class); |
| 32 | + } else { |
| 33 | + $container->removeDefinition($id); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if ($migrationRefs !== []) { |
| 38 | + $container->getDefinition('doctrine.migrations.service_migrations_factory') |
| 39 | + ->replaceArgument(1, new ServiceLocatorArgument($migrationRefs)); |
| 40 | + } else { |
| 41 | + $container->removeDefinition('doctrine.migrations.service_migrations_factory'); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments