* * @param string $name * * @return Connection */ protected function makeConnection($name) { $config = $this->getConfig($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); } $driver = $config['driver']; // 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])) { return call_user_func($this->extensions[$driver], $config, $name); } return $this->factory->make($config, $name); } /** * Prepare the database connection instance. * * @param Connection $connection * * @return Connection */ protected function prepare($connection) { return $connection; } /** * Get the configuration for a connection. * * @param string $name * * @return array * * @throws \InvalidArgumentException */ protected function getConfig($name) { $name = $name ?: $this->getDefaultConnection(); // To get the database connection configuration, we will just pull each of the // connection configurations and get the configurations for the given name. // If the configuration doesn't exist, we'll throw an exception and bail. $connections = $this->app['config']['database.connections']; if (is_null($config = array_get($connections, $name))) { throw new \InvalidArgumentException("Database [$name] not configured."); } return $config; } /** * Get the default connection name. * * @return string */ public function getDefaultConnection() { return $this->app['config']['database.default-connection']; } /** * Set the default connection name. * * @param string $name */ public function setDefaultConnection($name) { $this->app['config']['database.default-connection'] = $name; } /** * Register an extension connection resolver. * * @param string $name * @param callable $resolver */ public function extend($name, $resolver) { $this->extensions[$name] = $resolver; } /** * Return all of the created connections. * * @return \Concrete\Core\Database\Connection\Connection[] */ public function getConnections() { return $this->connections; } /** * Dynamically pass methods to the default connection. * * @param string $method * @param array $parameters * * @return mixed */ public function __call($method, $parameters) { return call_user_func_array(array($this->connection(), $method), $parameters); } /** * @return \Concrete\Core\Database\Connection\ConnectionFactory */ public function getFactory() { return $this->factory; } public function getConnection($name = null) { return $this->connection($name); } public function getConnectionNames() { return array_keys($this->connections); } public function getDefaultConnectionName() { return $this->getDefaultConnection(); } }