ConfigureDependencyFactoryPass.php 4.8 KB

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