<?php
$config = [
'fetchAs' => PDO::FETCH_CLASS,
'use' => 'mysql',
'drivers' => [
'mysql' => [
'host' => '127.0.0.1', # skip resolving localhost go directly to 127.0.0.1 ( Optional )
'port' => 3306, # default mysql port ( Optional )
'database' => 'twitbot',
'username' => 'root',
'password' => 'toor',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci', # ( Optional )
'persist' => true, # use persistent connection ( true | false )
'prefix' => '', # ( Optional )
'socket' => '', # unix socket example /tmp/mysql.sock, leave blank if you are using host
]
]
];
class Manager{
public $manager;
public function __construct( array $config )
{
$this->manager = new ConnectionManager( $config );
}
public function getConnection()
{
return $this->manager->createConnection();
}
}
class ConnectionManager
{
private $config = [];
private $availableDrivers = [];
private $supportedDrivers = ['mysql'];
public function __construct( array $config )
{
$this->config = $config;
$this->availableDrivers = PDO::getAvailableDrivers();
}
public function createConnection()
{
if(empty( $this->config['use'] )) throw new Exception('Database driver must be set!', 1);
# check if PDO Driver was compiled with PHP
$driver = strtolower( $this->config['use'] );
if ( ! in_array( $driver, $this->availableDrivers, true )) throw new Exception('Missing PDO driver ' . $driver, 1);
if ( ! in_array( $driver, $this->supportedDrivers, true )) throw new Exception($driver . ' is not supported!', 1);
$reflection = new ReflectionClass(ucfirst($driver) . 'Connection');
return $reflection->newInstanceArgs([$this->config])->connect();
}
}
abstract class Connection
{
private $options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false
];
public $config = [];
public function __construct( array $config )
{
$this->config = $config;
$this->options[ PDO::ATTR_PERSISTENT ] = $this->getDriverKey('persist');
$this->options[ PDO::ATTR_DEFAULT_FETCH_MODE ] = $this->config['fetchAs'];
}
protected function establishConnection( $dsn = null )
{
return new PDO( $dsn, $this->getDriverKey('username'), $this->getDriverKey('password'), $this->options );
}
public function getDriverKey( $key = null )
{
$value = $this->config['drivers'][ $this->config['use'] ][ $key ];
return ( isset($value) && ! empty($value) ) ? $value : false;
}
public function setOptions( array $options )
{
$this->options = $options;
}
public function getOptions()
{
return $this->options;
}
}
class MysqlConnection extends Connection{
private $dsn;
public $connection;
public function __construct( array $config )
{
parent::__construct( $config );
$this->dsn = $this->makeDsn();
}
private function makeDsn(){
return ( $this->isUsingSocket( $this->config ) ) ? $this->useSocket() : $this->useHost();
}
private function isUsingSocket(){
if( empty($this->getDriverKey('socket')) && ! $this->getDriverKey('socket') ) return false;
return ( is_readable($this->getDriverKey('socket')) ) ? true : false; # check if the socket file path is readable
}
private function isUsingPort()
{
return ( is_numeric($this->getDriverKey('port')) ) ? ';port=' . $this->getDriverKey('port') : '';
}
private function useSocket()
{
return 'mysql:unix_socket=' . $this->getDriverKey('socket') . ';dbname=' . $this->getDriverKey('database') . ';charset=' . $this->getDriverKey('charset');
}
private function useHost()
{
return 'mysql:host=' . $this->getDriverKey('host') . $this->isUsingPort() . ';dbname=' . $this->getDriverKey('database') . ';charset=' . $this->getDriverKey('charset');
}
public function connect()
{
# check if collation is set
if( $this->getDriverKey('collation') ){
#$this->options[ PDO::MYSQL_ATTR_INIT_COMMAND ] = "SET NAMES '" . $this->getDriverKey('charset') . "' COLLATE '" . $this->getDriverKey('collation') . "'";
}
return $this->connection = $this->establishConnection( $this->dsn );
}
}
$manager = new Manager( $config );
var_dump($manager);
preferences:
23.41 ms | 409 KiB | 5 Q