public static function getInstance($alias = 'standard', $type = null, $user = null, $pass = null, array $option = null)
{
try {
try {
if (!array_key_exists($alias, self::$handle)) {
// INI file containing the initial settings of the adapters the database
$ini_file = self::getSettings();
// Checks if the INI file exists
if (file_exists($ini_file)) {
$_dir_file = dirname($ini_file);
$_ini_file = basename($ini_file);
// Checks whether the file is readable
if (is_readable($ini_file)) {
// Interprets the file containing the initial settings
$datafile = self::parse_ini_file_advanced($ini_file);
// Initial settings for database adapters
if (isset($datafile['PDO4YOU_ADAPTER'])) {
// Captures all the names of the keys of an array
$Keys = function ($array) {
$keys = array();
foreach ($array as $key => $value) {
$slice_item = array_slice($value, 0, 1);
$first_item = array_shift($slice_item);
if (is_array($first_item)) {
foreach ($value as $key2 => $value2) {
$keys[] = $key . '.' . $key2;
}
} else {
$keys[] = $key;
}
}
return $keys;
};
// List with the names of adapters available
$adapters = $Keys($datafile['PDO4YOU_ADAPTER']);
} else {
exit('The settings for existing databases, were not configured in the <strong>' . $_ini_file . '</strong>.');
}
} else {
exit('The <strong>' . $_ini_file . '</strong> file can not be read in the directory:<br /> ' . $_dir_file);
}
}
// Checks the selected adapter
if (isset($adapters)) {
$adapter = $alias == 'standard' ? static::PDO4YOU_ADAPTER : $alias;
if (empty($adapter)) {
return;
}
if ($adapter == 'vcap') {
$json = json_decode(getenv("VCAP_SERVICES"), true);
$data = $datafile['PDO4YOU_ADAPTER']['vcap'];
$part = preg_split('~[|]~', $data['vcap']);
$conf = $json[$part[0]][$part[1]]['credentials'];
$type = isset($data['type']) ? $data['type'] : null;
$host = isset($conf['hostname']) ? $conf['hostname'] : null;
$port = isset($conf['port']) ? $conf['port'] : null;
$user = isset($conf['username']) ? $conf['username'] : null;
$pass = isset($conf['password']) ? $conf['password'] : null;
$base = isset($conf['name']) ? $conf['name'] : null;
} else {
$part = preg_split('~[.]~', preg_replace('~[\\s]{1,}~', null, $adapter));
$conf = count($part) == 2 ? @$datafile['PDO4YOU_ADAPTER'][$part[0]][$part[1]] : @$datafile['PDO4YOU_ADAPTER'][$part[0]];
// Checks if the selected adapter in the instance exists
if (in_array($adapter, $adapters)) {
$type = isset($conf['type']) ? $conf['type'] : null;
$host = isset($conf['host']) ? $conf['host'] : null;
$port = isset($conf['port']) ? $conf['port'] : null;
$user = isset($conf['user']) ? $conf['user'] : null;
$pass = isset($conf['pass']) ? $conf['pass'] : null;
$base = isset($conf['base']) ? $conf['base'] : null;
}
}
}
// Checks the type of adapter and mounts the DNS
$type = strtolower($type);
switch ($type) {
case 'maria':
$driver = 'mysql:' . (!empty($base) ? 'dbname=' . $base . ';' : null) . 'host=' . $host . ';port=' . $port . ';';
break;
case 'mysql':
case 'pgsql':
case 'cubrid':
$driver = $type . ':' . (!empty($base) ? 'dbname=' . $base . ';' : null) . 'host=' . $host . ';port=' . $port . ';';
break;
case 'mssql':
case 'dblib':
case 'sybase':
$driver = $type . ':' . (!empty($base) ? 'dbname=' . $base . ';' : null) . 'host=' . $host . ';';
break;
case 'sqlsrv':
$driver = 'sqlsrv:' . (!empty($base) ? 'database=' . $base . ';' : null) . 'server=' . $host . ';';
break;
case 'oracle':
$driver = 'oci:' . (!empty($base) ? 'dbname=' . $base : null);
break;
case 'sqlite':
$driver = 'sqlite:' . (!empty($base) ? $base : null);
break;
default:
$driver = $type;
}
// Initializes the Singleton connection
self::singleton($alias, $driver, $user, $pass, $option);
}
} catch (\PDOException $e) {
$error = self::getErrorInfo($e);
if ($error['state'] == '42000') {
throw new \PDOException(self::$exception['no-database']);
} else {
throw $e;
}
}
} catch (\PDOException $e) {
self::stackTrace($e);
}
return self::$instance;
}