ConnectionRegistryConnection.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Configuration\Connection;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\Migrations\Configuration\Connection\Exception\InvalidConfiguration;
  6. use Doctrine\Persistence\ConnectionRegistry;
  7. final class ConnectionRegistryConnection implements ConnectionLoader
  8. {
  9. /** @var ConnectionRegistry */
  10. private $registry;
  11. /** @var string|null */
  12. private $defaultConnectionName;
  13. public static function withSimpleDefault(ConnectionRegistry $registry, ?string $connectionName = null): self
  14. {
  15. $that = new self();
  16. $that->registry = $registry;
  17. $that->defaultConnectionName = $connectionName;
  18. return $that;
  19. }
  20. private function __construct()
  21. {
  22. }
  23. public function getConnection(?string $name = null): Connection
  24. {
  25. $connection = $this->registry->getConnection($name ?? $this->defaultConnectionName);
  26. if (! $connection instanceof Connection) {
  27. throw InvalidConfiguration::invalidConnectionType($connection);
  28. }
  29. return $connection;
  30. }
  31. }