Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Lampfire\CloutLib\DB;
- /**
- * Creates a PDO object based on a given configuration. Once the instance is constructed, the
- * configuration is immutable, though the internal PDO reference may change state.
- * @package Database
- * @author David Cloutman
- */
- class DBConnector implements PDOWrapperInterface {
- /**
- * Database type
- * @var string
- */
- protected $dbType;
- /**
- * Database username
- * @var string
- */
- protected $username;
- /**
- * Database password
- * @var [type]
- */
- protected $password;
- /**
- * Database name
- * @var string
- */
- protected $dbName;
- /**
- * Database domain or I.P address.
- * @var string
- */
- protected $hostName;
- /**
- * The associated PDO object.
- * @var null|\PDO
- */
- protected $pdo = null;
- /**
- * An array of allowed values for $dbType.
- */
- const SUPPORTED_DB_TYPES = ['pgsql', 'mysql'];
- /**
- * Create a new DBConnector instance.
- * @param string $dbType Database type. Either 'mysql' or 'pgsql'.
- * @param string $username The database user used to authenticate into the database server.
- * @param string $password The password used to authenticate into the database server.
- * @param string $dbName The name of the database.
- * @param string $hostname The domain name or IP address of the database server. Defaults to 'localhost'. An empty string will create a socket connection for Postgres (type='pgsql');
- * @param int $port The port of the database server. Defaults to usual value for $dbType.
- */
- function __construct($dbType, $username, $password, $dbName, $hostname = 'localhost', $port = null) {
- $this->dbType = $dbType;
- if(!$this->isValidDBType()) {
- throw new InvalidDBTypeException();
- }
- $this->username = $username;
- $this->password = $password;
- $this->dbName = $dbName;
- $this->hostName = $hostname;
- if (null === $port) {
- $this->port = $this->setDefaultPort();
- }
- }
- /**
- * Returns the type of database specified when the constructor was called. Valid values are 'mysql' or 'pgsql'.
- * @return string
- */
- public function getDBType() {
- return $dbType;
- }
- /**
- * Returns the database username.
- * @return string
- */
- public function getUsername() {
- return $this->username;
- }
- /**
- * Returns the database password.
- * @return string
- */
- public function getPassword() {
- return $this->password;
- }
- /**
- * Returns the database name.
- * @return string
- */
- public function getDbName() {
- return $this->dbName;
- }
- /**
- * Returns the database's hostname of i.p. address depending on which was configured.
- * @return string
- */
- public function getHostName() {
- return $this->hostName;
- }
- /**
- * Returns the PDO object generated by the connector or null if one is not associated with the connector.
- * @return \PDO|null
- */
- public function getPDO() {
- return $this->pdo;
- }
- /**
- * This internal function will set the $port property to the most common default for the given database type.
- */
- protected function setDefaultPort () {
- switch ($this->dbType) {
- case 'mysql':
- $this->port = 3306;
- break;
- case 'pgsql':
- $this->port = 5432;
- break;
- default:
- throw new InvalidDBTypeException();
- break;
- }
- }
- /**
- * Tests to see if the database type string supplied in the constructor matches supported databases.
- * Supported types: mysql, pgsql
- * @return boolean True if database type is supported.
- */
- protected function isValidDBType() {
- if (false !== array_search(self::SUPPORTED_DB_TYPES)) {
- return true;
- }
- return false;
- }
- /**
- * Creates a PDO connection string for the specified type of database.
- * @return string A connection string.
- * @throws \Exception Throws a base exception when $dbType is unsupported.
- */
- protected function generateConnectionString() {
- $connectionString = '';
- switch ($dbType) {
- case 'mysql':
- $connectionString = "mysql:host={$this->hostName};port={$this->port};dbname={$this->dbName}";
- break;
- case 'pgsql':
- $connectionString .= 'pgsql:';
- $connectionString .= (empty($this->hostName)) ? '' : "host={$this->hostName};"; // Skip empty hostname to establish socket connection.
- $connectionString .= "port={$this->port};dbname={$this->dbName};user={$this->username};password={$this->password}";
- break;
- default:
- throw new InvalidDBTypeException();
- }
- return $connectionString;
- }
- /**
- * Create a PDO connection. Note that you must call connect() explicitly to connect to the database. connect() is NOT called by the costructor.
- * @return \PDO|null|boolean Returns a PDO object connected to the specified database on success, boolean false on failure.
- *
- */
- public function connect() {
- if(null === $this->pdo) {
- try {
- $this->pdo = new \PDO('mysql:host=' . $this->hostName . ';dbname=' . $this->dbName, $this->username, $this->password);
- return $this->pdo;
- }
- catch (\PDOException $e) {
- error_log('Error Could not connect to database. PDOException: ' . $e->getMessage());
- return false;
- }
- }
- return $this->pdo;
- }
- /**
- * Closes the PDO if no other variables reference the instance. Subsequent calls to connect() will
- * create an new PDO object and a new connection. External variables referencing the PDO object will
- * cause the connection to persist.
- * @return null
- */
- public function close() {
- $this->pdo = null;
- }
- /**
- * Automatically calls close() when the object is destroyed.
- */
- public function __destruct(){
- $this->close();
- }
- }
- /**
- * Flags a class as having a PDO object as part of its composition.
- * @package Database
- */
- interface PDOWrapperInterface {
- /**
- * The extending contains a retrievable PDO object as part of its composition.
- * @return \PDO returns the encapsulated PDO object.
- */
- public function getPDO();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement