<commit_before><?php 

namespace Purekid\Mongodm;

/**
 * Mongodm - A PHP Mongodb ORM
 * This file is base on  mikelbring(https://github.com/mikelbring/Mongor)'s work and is inspired by https://github.com/Wouterrr/MangoDB
 *
 * @package  Mongodm
 * @author   <NAME> <<EMAIL>>
 * @link     http://blog.missyi.com
 */
class MongoDB 
{

	/**
	 * Database instances
	 *
	 * @var array
	 */
	public static $instances = array();

	/**
	 * Load instance
	 *
	 * @static
	 * @param string $name
	 * @param array|null $config
	 * @return MangoDB
	*/
	public static function instance($name = 'default', array $config = NULL)
	{
		if ( ! isset(self::$instances[$name]))
		{
			if ($config === NULL)
			{
				// Load the configuration for this database
				$config = self::config($name);
			}

			new MongoDB($name,$config);
		}

		return self::$instances[$name];
	}

	/**
	 * Instance name
	 *
	 * @var string
	 */
	protected $_name;

	/**
	 * Connected
	 *
	 * @var bool
	 */
	protected $_connected = FALSE;

	/**
	 * Raw server connection
	 *
	 * @var Mongo
	 */
	protected $_connection;

	/**
	 * Raw database connection
	 *
	 * @var Mongo Database
	 */
	protected $_db;

	/**
	 * Local config
	 *
	 * @var array
	 */
	protected $_config;

	/**
	 * @param  $name
	 * @param array $config
	 */
	protected function __construct($name, array $config)
	{
		$this->_name = $name;

		$this->_config = $config;
		/* Store the database instance */
		MongoDB::$instances[$name] = $this;
	}

	final public function __destruct()
	{
		$this->disconnect();
	}

	final public function __toString()
	{
		return $this->_name;
	}

	/**
	 * Connect to MongoDB, select database
	 *
	 * @return bool
	 */
	public function connect()
	{
		if ($this->_connection)
		{
			return;
		}

		/**
		 * Add required variables
		 * Clear the connection parameters for security
		 */
		$config = $this->_config['connection'] + array(
				'hostnames'  => 'localhost:27017'
		);

		unset($this->_config['connection']);

		/* Add Username & Password to server string */
		if (isset($config['username']) && isset($config['password']))
		{
			$config['hostnames'] = $config['username'] . ':' . $config['password'] . '@' . $config['hostnames'] . '/' . $config['database'];
		}

		/* Add required 'mongodb://' prefix */
		if (strpos($config['hostnames'], 'mongodb://') !== 0)
		{
			$config['hostnames'] = 'mongodb://' . $config['hostnames'];
		}

		if (!isset($options))
		{
			$options = array();
		}

		/* Create connection object, attempt to connect */
		$options['connect'] = false;
		$this->_connection = new \Mongo($config['hostnames'], $options);
		/* Try connect */
		try
		{
			$this->_connection->connect();
		}
		catch (\MongoConnectionException $e)
		{
			throw new \Exception('Unable to connect to MongoDB server at ' . $config['hostnames']);
		}

		if (!isset($config['database']))
		{
			throw new \Exception('No database specified in MangoDB Config');
		}
		$this->_db = $this->_connection->selectDB($config['database']);
		
		$this->_connected = $this->_connection->connected;
		return true;
	}

	public function getRef(array $ref){
		
		return $this->_connection->getDBRef($ref);<commit_msg>MongoClient instead of MongoDB class<commit_after><?php 

namespace Purekid\Mongodm;

/**
 * Mongodm - A PHP Mongodb ORM
 * This file is base on  mikelbring(https://github.com/mikelbring/Mongor)'s work and is inspired by https://github.com/Wouterrr/MangoDB
 *
 * @package  Mongodm
 * @author   <NAME> <<EMAIL>>
 * @link     http://blog.missyi.com
 */
class MongoDB 
{

	/**
	 * Database instances
	 *
	 * @var array
	 */
	public static $instances = array();

	/**
	 * Load instance
	 *
	 * @static
	 * @param string $name
	 * @param array|null $config
	 * @return MangoDB
	*/
	public static function instance($name = 'default', array $config = NULL)
	{
		if ( ! isset(self::$instances[$name]))
		{
			if ($config === NULL)
			{
				// Load the configuration for this database
				$config = self::config($name);
			}

			new MongoDB($name,$config);
		}

		return self::$instances[$name];
	}

	/**
	 * Instance name
	 *
	 * @var string
	 */
	protected $_name;

	/**
	 * Connected
	 *
	 * @var bool
	 */
	protected $_connected = FALSE;

	/**
	 * Raw server connection
	 *
	 * @var Mongo
	 */
	protected $_connection;

	/**
	 * Raw database connection
	 *
	 * @var Mongo Database
	 */
	protected $_db;

	/**
	 * Local config
	 *
	 * @var array
	 */
	protected $_config;

	/**
	 * @param  $name
	 * @param array $config
	 */
	protected function __construct($name, array $config)
	{
		$this->_name = $name;

		$this->_config = $config;
		/* Store the database instance */
		MongoDB::$instances[$name] = $this;
	}

	final public function __destruct()
	{
		$this->disconnect();
	}

	final public function __toString()
	{
		return $this->_name;
	}

	/**
	 * Connect to MongoDB, select database
	 *
	 * @return bool
	 */
	public function connect()
	{
		if ($this->_connection)
		{
			return;
		}

		/**
		 * Add required variables
		 * Clear the connection parameters for security
		 */
		$config = $this->_config['connection'] + array(
				'hostnames'  => 'localhost:27017'
		);

		unset($this->_config['connection']);

		/* Add Username & Password to server string */
		if (isset($config['username']) && isset($config['password']))
		{
			$config['hostnames'] = $config['username'] . ':' . $config['password'] . '@' . $config['hostnames'] . '/' . $config['database'];
		}

		/* Add required 'mongodb://' prefix */
		if (strpos($config['hostnames'], 'mongodb://') !== 0)
		{
			$config['hostnames'] = 'mongodb://' . $config['hostnames'];
		}

		if (!isset($options))
		{
			$options = array();
		}

		/* Create connection object, attempt to connect */
		$options['connect'] = false;
		$this->_connection = new \MongoClient($config['hostnames'], $options);
		/* Try connect */
		try
		{
			$this->_connection->connect();
		}
		catch (\MongoConnectionException $e)
		{
			throw new \Exception('Unable to connect to MongoDB server at ' . $config['hostnames']);
		}

		if (!isset($config['database']))
		{
			throw new \Exception('No database specified in MangoDB Config');
		}
		$this->_db = $this->_connection->selectDB($config['database']);
		
		$this->_connected = $this->_connection->connected;
		return true;
	}

	public function getRef(array $ref){
		
		return $this->_connection->getDBRef($ref);
{'fraction_non_alphanumeric': 0.17540544796095103, 'fraction_numerical': 0.001889466225791214, 'mean_word_length': 3.12735542560104, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646162', 'n_tokens_mistral': 2340, 'n_tokens_neox': 2016, 'n_words': 493}