SingleConnectionProvider.php 1005 Bytes
Newer Older
1 2 3 4 5 6 7
<?php

namespace Doctrine\DBAL\Tools\Console\ConnectionProvider;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\ConnectionNotFound;
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use function sprintf;

class SingleConnectionProvider implements ConnectionProvider
{
    /** @var Connection */
    private $connection;

    /** @var string */
    private $defaultConnectionName;

    public function __construct(Connection $connection, string $defaultConnectionName = 'default')
    {
        $this->connection            = $connection;
        $this->defaultConnectionName = $defaultConnectionName;
    }

25
    public function getDefaultConnection(): Connection
26 27 28 29
    {
        return $this->connection;
    }

30
    public function getConnection(string $name): Connection
31 32 33 34 35 36 37 38
    {
        if ($name !== $this->defaultConnectionName) {
            throw new ConnectionNotFound(sprintf('Connection with name "%s" does not exist.', $name));
        }

        return $this->connection;
    }
}