Returns existing connection by config name or instantiate new one.
Вазвращает существующее подключение по имени из конига или создаёт новое.

@param string $name connection name defined in config

@return AbstractDb\DriverInterface

@throws DbException
773375
public static function connection($name = 'default')
    {
        $name = $name == 'default' ? self::$config['connection']['default'] : $name;

        return isset(self::$connection[$name])
            ? self::$connection[$name]
            : self::$connection[$name] = self::createForConnection($name);
    }
1153140
public function getConnection($name)
    {
        if (!$this->hasConnection($name)) {
            throw new \InvalidArgumentException(sprintf('The connection "%s" does not exist.', $name));
        }

        return $this->connections[$name];
    }
1251032
public function getConnection($name = ''): QueryBuilderInterface
	{
		// If the parameter is a string, use it as an array index
		if (is_scalar($name) && isset($this->connections[$name]))
		{
			return $this->connections[$name];
		}
		else if (empty($name) && ! empty($this->connections)) // Otherwise, return the last one
		{
			return end($this->connections);
		}

		// You should actually connect before trying to get a connection...
		throw new InvalidArgumentException('The specified connection does not exist');
	}
1135557
protected function makeConnection($name)
    {
        $config = $this->configuration($name);

        // First we will check by the connection name to see if an extension has been
        // registered specifically for that connection. If it has we will call the
        // Closure and pass it the config allowing it to resolve the connection.
//        if (isset($this->extensions[$name])) {
//            return call_user_func($this->extensions[$name], $config, $name);
//        }

        // Next we will check to see if an extension has been registered for a driver
        // and will call the Closure if so, which allows us to have a more generic
        // resolver for the drivers themselves which applies to all connections.
//        if (isset($this->extensions[$driver = $config['driver']])) {
//            return call_user_func($this->extensions[$driver], $config, $name);
//        }
        return $this->factory->make($config, $name);
    }
1229918
public function getConnectionConfig($name = null) {
		if (is_null($name)) {
			$name = $this->defaultConfigName;
		}

		if (isset($this->connectionConfigs[$name])) {
			return $this->connectionConfigs[$name];
		}

		return null;
	}
241872
public static function connect($name = 'default') {
        if ($name instanceof DatabaseConnection) {
            return $name;
        }
        if (is_null($name)) {
            $name = 'default';
        }
        if (defined('ARTISAN_TASK_PID')) {
            $pid = '@' . @posix_getpid();
        } else {
            $pid = '';
        }
        $config = $pname = false;
        if (is_array($name)) {
            $tmpname = implode('_', $name) . $pid;
            if (isset (self::$dbs [ $tmpname ])) {
                return self::$dbs [ $tmpname ];
            }
            $config = $name;
            $pname  = $tmpname;
        } else if (is_string($name)) {
            $pname = $name . $pid;
            if (isset (self::$dbs [ $pname ])) {
                return self::$dbs [ $pname ];
            }
            $config = App::cfgLoader()->loadDatabaseConfig($name);
        } else if ($name instanceof DatabaseConfiguration) {
            $config = $name;
            $pname  = $config->__toString() . $pid;
        }
        if ($config) {
            $dialect = DatabaseDialect::getDialect($config);
            if ($dialect) {
                $db                   = new DatabaseConnection($dialect, $name);
                $db->name             = $pname;
                self::$dbs [ $pname ] = $db;

                return $db;
            }
        }
        throw new DialectException('cannot connect the database server!');
    }
1082535
public function connection( $name = null )
	{
		list( $name, $type ) = $this->parseConnectionName( $name );

		// If we haven't created this connection, we'll create it based on the config
		// provided in the application. Once we've created the connections we will
		// set the "fetch mode" for PDO which determines the query return types.
		if ( !isset( $this->connections[$name] ) )
		{
			$connection = $this->makeConnection( $name );

			$this->setPdoForType( $connection, $type );

			$this->connections[$name] = $this->prepare( $connection );
		}

		return $this->connections[$name];
	}
592318
public function connection($name = null)
    {
        $name = $name ?: $this->getDefaultDriver();

        // If the connection has not been resolved yet we will resolve it now as all
        // of the connections are resolved when they are actually needed so we do
        // not make any unnecessary connection to the various queue end-points.
        if ( ! isset($this->connections[$name])) {
            $this->connections[$name] = $this->resolve($name);

            $this->connections[$name]->setContainer($this->app);

            $this->connections[$name]->setEncrypter($this->app['encrypter']);
        }

        return $this->connections[$name];
    }
122104
protected function getConnectionByName($name)
    {
        if (array_key_exists($name, $this->connectionByName)) {
            return $this->connectionByName[$name];
        }

        $event = clone $this->getManagerRegistryResourceEventPrototype();
        $event->setName(ManagerRegistryResourceEvent::RESOLVE_CONNECTION_EVENT);
        $event->setResourceName($name);
        $event->setTarget($this);


        $resultsEvent = $this->getEventManager()->trigger($event, function ($connection) {
            return is_object($connection);
        });

        $connection = $resultsEvent->last();

        if (!is_object($connection)) {
            $errMsg = sprintf('Connection %s not found', $name);
            throw new Exception\ConnectionNotFoundException($errMsg);
        }

        $this->connectionByName[$name] = $connection;

        return $this->connectionByName[$name];
    }
55618
public function getConnection($name = null)
    {
        $this->serviceContext = static::CONNECTION_CONTEXT;
        $result = parent::getConnection($name);
        $this->serviceContext = null;
        return $result;
    }
656667
function connection($name)
    {

        // Check if connection is already loaded.

        if ($this->isLoaded($name)) {

            $this->connection = $this->connections[$name];

            return $this->newQuery($name);

        }

        // Create a new connection.

        if (array_key_exists($name, $this->config["connections"])) {

            $config = $this->config["connections"][$name];

            // Instantiate a new ClientBuilder
            $clientBuilder = ClientBuilder::create();

            $clientBuilder->setHosts($config["servers"]);

            $clientBuilder = self::configureLogging($clientBuilder,$config);

            if (!empty($config['handler'])) {
                $clientBuilder->setHandler($config['handler']);
            }

            // Build the client object
            $connection = $clientBuilder->build();

            $this->connection = $connection;

            $this->connections[$name] = $connection;

            return $this->newQuery($name);
        }

        $this->app->abort(500, "Invalid elasticsearch connection driver `" . $name . "`");
    }
356555
public function get($name)
    {
        // get from cache
        if (isset($this->pool[$name])) {
            return $this->pool[$name];
        }
        
        // check if connection exists
        if (!isset($this->configuration[$name])) {
            throw new Exception('Connection with name ' . $name . ' not found');
        }
        
        // check if dsn exists
        if (!isset($this->configuration[$name]['dsn'])) {
            $this->configuration[$name]['dsn'] = null;
        }
        
        // check if connect options exists
        if (empty($this->configuration[$name]['connectOptions'])) {
            $this->configuration[$name]['connectOptions'] = null;
        }
        
        // init client
        $client = new Client(
            $this->configuration[$name]['dsn'],
            $this->configuration[$name]['connectOptions']
        );
        
        if (isset($this->configuration[$name]['mapping'])) {
            $client->map($this->configuration[$name]['mapping']);
        }
        
        if (isset($this->configuration[$name]['defaultDatabase'])) {
            $client->useDatabase($this->configuration[$name]['defaultDatabase']);
        }
        
        $this->pool[$name] = $client;
        
        return $client;
    }
1067907
public function getExporter($name, DatabaseDriver $db = null)
	{
		// Derive the class name from the driver.
		$class = '\\Joomla\\Database\\' . ucfirst(strtolower($name)) . '\\' . ucfirst(strtolower($name)) . 'Exporter';

		// Make sure we have an exporter class for this driver.
		if (!class_exists($class))
		{
			// If it doesn't exist we are at an impasse so throw an exception.
			throw new \RuntimeException('Database Exporter not found.');
		}

		/* @var  $o  DatabaseExporter */
		$o = new $class;

		if ($db instanceof DatabaseDriver)
		{
			$o->setDbo($db);
		}

		return $o;
	}
1300756
public static function get($name = 'default')
    {
        if (!isset(self::$connections[$name])) {
            return null;
        }

        $connector = self::$connections[$name];

        return $connector->connection();
    }
298039
protected function getConnector($name)
    {
        $config = isset($this->drivers[$name]) ? $this->drivers[$name] : false;

        if ($config === false) {
            throw new InvalidArgumentException(
                "Unregistered cache driver name '$name'"
            );
        }

        if (!isset($this->connectors[$name])) {
            if (!is_subclass_of($config['connector'], Connector::class)) {
                throw new InvalidArgumentException(
                    $config['connector'] . ' must implement of ' . Connector::class
                );
            }

            $connector = new $config['connector']($config);

            $this->connectors[$name] = $connector;
        }

        return $this->connectors[$name];
    }
859211
public static function connection($name)
    {
        $schema = static::$app['db']->connection($name)->getSchemaBuilder();
        $schema->blueprintResolver(function ($table, $callback) {
            return new Blueprint($table, $callback);
        });

        return $schema;
    }
1090623
public function getPDO($name = null)
    {
        if (null === $name) {
            $name = $this->current ?: 'default';
        }
        if (!isset($this->connections[$name])) {
            $config = $this->configs[$name];
            $this->connections[$name] = new \PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
        }

        return $this->connections[$name];
    }