|
5 | 5 | namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass; |
6 | 6 |
|
7 | 7 | use Doctrine\Migrations\DependencyFactory; |
| 8 | +use InvalidArgumentException; |
| 9 | +use RuntimeException; |
8 | 10 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
9 | 11 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
10 | 12 | use Symfony\Component\DependencyInjection\Reference; |
11 | 13 |
|
| 14 | +use function array_keys; |
12 | 15 | use function assert; |
| 16 | +use function count; |
| 17 | +use function implode; |
| 18 | +use function is_array; |
13 | 19 | use function is_string; |
14 | 20 | use function sprintf; |
15 | 21 |
|
16 | 22 | class ConfigureDependencyFactoryPass implements CompilerPassInterface |
17 | 23 | { |
18 | 24 | public function process(ContainerBuilder $container): void |
19 | 25 | { |
| 26 | + if (! $container->has('doctrine')) { |
| 27 | + throw new RuntimeException('DoctrineMigrationsBundle requires DoctrineBundle to be enabled.'); |
| 28 | + } |
| 29 | + |
20 | 30 | $diDefinition = $container->getDefinition('doctrine.migrations.dependency_factory'); |
21 | | - $preferredEm = $container->getParameter('doctrine.migrations.preferred_em'); |
22 | | - if ($container->has('doctrine')) { |
23 | | - $loaderDefinition = $container->getDefinition('doctrine.migrations.registry_loader'); |
24 | | - $loaderDefinition->setArgument(0, new Reference('doctrine')); |
25 | | - if ($preferredEm !== null) { |
26 | | - $loaderDefinition->setArgument(1, $preferredEm); |
27 | | - } |
28 | 31 |
|
29 | | - $diDefinition->setFactory([DependencyFactory::class, 'fromEntityManager']); |
30 | | - $diDefinition->setArgument(1, new Reference('doctrine.migrations.registry_loader')); |
| 32 | + $preferredConnection = $container->getParameter('doctrine.migrations.preferred_connection'); |
| 33 | + assert(is_string($preferredConnection) || $preferredConnection === null); |
| 34 | + // explicitly use configured connection |
| 35 | + if ($preferredConnection !== null) { |
| 36 | + $this->validatePreferredConnection($container, $preferredConnection); |
| 37 | + |
| 38 | + $loaderDefinition = $container->getDefinition('doctrine.migrations.connection_registry_loader'); |
| 39 | + $loaderDefinition->setArgument(1, $preferredConnection); |
| 40 | + |
| 41 | + $diDefinition->setFactory([DependencyFactory::class, 'fromConnection']); |
| 42 | + $diDefinition->setArgument(1, new Reference('doctrine.migrations.connection_registry_loader')); |
31 | 43 |
|
32 | 44 | return; |
33 | 45 | } |
34 | 46 |
|
| 47 | + $preferredEm = $container->getParameter('doctrine.migrations.preferred_em'); |
35 | 48 | assert(is_string($preferredEm) || $preferredEm === null); |
36 | | - $emID = sprintf('doctrine.orm.%s_entity_manager', $preferredEm ?? 'default'); |
37 | | - if ($container->has($emID)) { |
38 | | - $container->getDefinition('doctrine.migrations.em_loader') |
39 | | - ->setArgument(0, new Reference($emID)); |
| 49 | + // explicitly use configured entity manager |
| 50 | + if ($preferredEm !== null) { |
| 51 | + $this->validatePreferredEm($container, $preferredEm); |
| 52 | + |
| 53 | + $loaderDefinition = $container->getDefinition('doctrine.migrations.entity_manager_registry_loader'); |
| 54 | + $loaderDefinition->setArgument(1, $preferredEm); |
40 | 55 |
|
41 | 56 | $diDefinition->setFactory([DependencyFactory::class, 'fromEntityManager']); |
42 | | - $diDefinition->setArgument(1, new Reference('doctrine.migrations.em_loader')); |
| 57 | + $diDefinition->setArgument(1, new Reference('doctrine.migrations.entity_manager_registry_loader')); |
43 | 58 |
|
44 | 59 | return; |
45 | 60 | } |
46 | 61 |
|
47 | | - $preferredConnection = $container->getParameter('doctrine.migrations.preferred_connection'); |
48 | | - assert(is_string($preferredConnection) || $preferredConnection === null); |
49 | | - $connectionId = sprintf('doctrine.dbal.%s_connection', $preferredConnection ?? 'default'); |
50 | | - $container->getDefinition('doctrine.migrations.connection_loader') |
51 | | - ->setArgument(0, new Reference($connectionId)); |
| 62 | + // try to use any/default entity manager |
| 63 | + if ( |
| 64 | + $container->hasParameter('doctrine.entity_managers') |
| 65 | + && is_array($container->getParameter('doctrine.entity_managers')) |
| 66 | + && count($container->getParameter('doctrine.entity_managers')) > 0 |
| 67 | + ) { |
| 68 | + $diDefinition->setFactory([DependencyFactory::class, 'fromEntityManager']); |
| 69 | + $diDefinition->setArgument(1, new Reference('doctrine.migrations.entity_manager_registry_loader')); |
| 70 | + |
| 71 | + return; |
| 72 | + } |
52 | 73 |
|
| 74 | + // fallback on any/default connection |
53 | 75 | $diDefinition->setFactory([DependencyFactory::class, 'fromConnection']); |
54 | | - $diDefinition->setArgument(1, new Reference('doctrine.migrations.connection_loader')); |
| 76 | + $diDefinition->setArgument(1, new Reference('doctrine.migrations.connection_registry_loader')); |
| 77 | + } |
| 78 | + |
| 79 | + private function validatePreferredConnection(ContainerBuilder $container, string $preferredConnection): void |
| 80 | + { |
| 81 | + /** |
| 82 | + * @var array<string, string> $allowedConnections |
| 83 | + */ |
| 84 | + $allowedConnections = $container->getParameter('doctrine.connections'); |
| 85 | + if (! isset($allowedConnections[$preferredConnection])) { |
| 86 | + throw new InvalidArgumentException(sprintf( |
| 87 | + 'The "%s" connection is not defined. Did you mean one of the following: %s', |
| 88 | + $preferredConnection, |
| 89 | + implode(', ', array_keys($allowedConnections)) |
| 90 | + )); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private function validatePreferredEm(ContainerBuilder $container, string $preferredEm): void |
| 95 | + { |
| 96 | + if ( |
| 97 | + ! $container->hasParameter('doctrine.entity_managers') |
| 98 | + || ! is_array($container->getParameter('doctrine.entity_managers')) |
| 99 | + || count($container->getParameter('doctrine.entity_managers')) === 0 |
| 100 | + ) { |
| 101 | + throw new InvalidArgumentException(sprintf( |
| 102 | + 'The "%s" entity manager is not defined. It seems that you do not have configured any entity manager in the DoctrineBundle.', |
| 103 | + $preferredEm |
| 104 | + )); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @var array<string, string> $allowedEms |
| 109 | + */ |
| 110 | + $allowedEms = $container->getParameter('doctrine.entity_managers'); |
| 111 | + if (! isset($allowedEms[$preferredEm])) { |
| 112 | + throw new InvalidArgumentException(sprintf( |
| 113 | + 'The "%s" entity manager is not defined. Did you mean one of the following: %s', |
| 114 | + $preferredEm, |
| 115 | + implode(', ', array_keys($allowedEms)) |
| 116 | + )); |
| 117 | + } |
55 | 118 | } |
56 | 119 | } |
0 commit comments