<commit_before><?php namespace CodeIgniter\Database;

use CodeIgniter\Config\BaseConfig;
use Config\Database;

class Config extends BaseConfig
{
	/**
	 * Cache for instance of any connections that
	 * have been requested as a "shared" instance.
	 *
	 * @var array
	 */
	static protected $instances = [];

	/**
	 * The main instance used to manage all of
	 * our open database connections.
	 *
	 * @var \CodeIgniter\Database\Database
	 */
	static protected $factory;

	//--------------------------------------------------------------------

	/**
	 * Creates the default
	 *
	 * @param string $group     The name of the connection group to use.
	 * @param bool   $getShared Whether to return a shared instance of the connection.
	 *
	 * @return mixed
	 */
	public static function connect(string $group = null, $getShared = true)
	{
		if ($getShared && isset(self::$instances[$group]))
		{
			return self::$instances[$group];
		}

		self::ensureFactory();

		$config = new \Config\Database();

		if (empty($group))
		{
			$group = ENVIRONMENT == 'testing' ? 'tests' : $config->defaultGroup;
		}

		if (! isset($config->$group))
		{
			throw new \InvalidArgumentException($group.' is not a valid database connection group.');
		}

		$connection = self::$factory->load($config->$group, $group);

		self::$instances[$group] =& $connection;

		return $connection;
	}

	//--------------------------------------------------------------------

	/**
	 * Returns an array of all db connections currently made.
	 *<commit_msg>Allow custom connections to be made when creating a database. Fixes #62<commit_after><?php namespace CodeIgniter\Database;

use CodeIgniter\Config\BaseConfig;
use Config\Database;

class Config extends BaseConfig
{
	/**
	 * Cache for instance of any connections that
	 * have been requested as a "shared" instance.
	 *
	 * @var array
	 */
	static protected $instances = [];

	/**
	 * The main instance used to manage all of
	 * our open database connections.
	 *
	 * @var \CodeIgniter\Database\Database
	 */
	static protected $factory;

	//--------------------------------------------------------------------

	/**
	 * Creates the default
	 *
	 * @param string|array  $group     The name of the connection group to use,
	 *                                 or an array of configuration settings.
	 * @param bool          $getShared Whether to return a shared instance of the connection.
	 *
	 * @return mixed
	 */
	public static function connect($group = null, $getShared = true)
	{
		if (is_array($group))
		{
			$config = $group;
			$group = 'custom';
		}

		if ($getShared && isset(self::$instances[$group]))
		{
			return self::$instances[$group];
		}

		self::ensureFactory();

		$config = $config ?? new \Config\Database();

		if (empty($group))
		{
			$group = ENVIRONMENT == 'testing' ? 'tests' : $config->defaultGroup;
		}

		if (is_string($group) && ! isset($config->$group) && $group != 'custom')
		{
			throw new \InvalidArgumentException($group.' is not a valid database connection group.');
		}

		if (isset($config->$group))
		{
			$config = $config->$group;
		}

		$connection = self::$factory->load($config, $group);

		self::$instances[$group] =& $connection;

		return $connection;
	}

	//--------------------------------------------------------------------

	/**
	 * Returns an array of all db connections currently made.
	 *
{'fraction_non_alphanumeric': 0.20732797140303844, 'fraction_numerical': 0.0005957700327673518, 'mean_word_length': 2.3446215139442232, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '91262', 'n_tokens_mistral': 1014, 'n_tokens_neox': 907, 'n_words': 284}