1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166:
<?php
namespace MvcCore\Model;
trait Connection {
public static function GetConnection ($connectionNameOrConfig = NULL, $strict = TRUE) {
$connectionName = (is_string($connectionNameOrConfig) || is_int($connectionNameOrConfig))
? $connectionNameOrConfig
: static::resolveConnectionName($connectionNameOrConfig, $strict);
if (isset(self::$connections[$connectionName]))
return self::$connections[$connectionName];
$cfg = static::GetConfig($connectionName);
if ($cfg === NULL) throw new \InvalidArgumentException(
"[".get_called_class()."] No connection found under given name/index: `{$connectionName}`."
);
$connection = static::connect($cfg);
self::$connections[$connectionName] = $connection;
return $connection;
}
protected static function resolveConnectionName ($connectionNameOrConfig = NULL, $strict = TRUE) {
if (is_array($connectionNameOrConfig) || $connectionNameOrConfig instanceof \stdClass) {
if (self::$configs === NULL) static::loadConfigs(FALSE, $strict);
$connectionName = static::SetConfig((array) $connectionNameOrConfig);
} else {
if (self::$configs === NULL) static::loadConfigs(TRUE, TRUE);
$connectionName = $connectionNameOrConfig;
if ($connectionName === NULL && isset(static::$connectionName))
$connectionName = static::$connectionName;
if ($connectionName === NULL && isset(self::$connectionName))
$connectionName = self::$connectionName;
if ($connectionName === NULL)
$connectionName = self::$defaultConnectionName;
}
if ($connectionName === NULL)
throw new \InvalidArgumentException(
"[".get_called_class()."] No connection name or connection config specified."
);
return $connectionName;
}
protected static function connect ($dbConfig) {
$sysCfgProps = (object) static::$sysConfigProperties;
$conArgsKey = isset(self::$connectionArguments[$dbConfig->{$sysCfgProps->driver}])
? $dbConfig->{$sysCfgProps->driver}
: 'default';
$conArgs = (object) self::$connectionArguments[$conArgsKey];
$connection = NULL;
if ($conArgs->fileDb) {
$appRoot = \MvcCore\Application::GetInstance()->GetRequest()->GetAppRoot();
if (strpos($appRoot, 'phar://') !== FALSE) {
$lastSlashPos = strrpos($appRoot, '/');
$appRoot = substr($appRoot, 7, $lastSlashPos - 7);
}
$dbConfig = (object) array_merge([], (array) $dbConfig);
$dbFileFullPath = realpath($appRoot . $dbConfig->{$sysCfgProps->database});
if ($dbFileFullPath === FALSE) throw new \InvalidArgumentException(
"[".get_called_class()."] Database file doesn't exists: `{$dbFileFullPath}`."
);
$dbConfig->{$sysCfgProps->database} = str_replace('\\', '/', realpath($dbFileFullPath));
}
$dsn = $conArgs->dsn;
$cfgArr = array_merge($conArgs->defaults, (array) $dbConfig);
$credentialsInDsn = (
mb_strpos($dsn, '{user}') !== FALSE &&
mb_strpos($dsn, '{password}') !== FALSE
);
foreach ($cfgArr as $key => $value) {
if (
is_numeric($key) ||
mb_strpos($key, '\\PDO::') === 0 ||
$key == 'options'
) continue;
if (isset($sysCfgProps->{$key})) {
$prop = $sysCfgProps->{$key};
$value = isset($dbConfig->{$prop})
? $dbConfig->{$prop}
: $value;
}
$dsn = str_replace('{'.$key.'}', $value, $dsn);
}
$connectionClass = isset($dbConfig->{$sysCfgProps->class})
? $dbConfig->{$sysCfgProps->class}
: self::$defaultConnectionClass;
$defaultOptions = self::$connectionArguments['default']['options'];
$rawOptions = isset($dbConfig->{$sysCfgProps->options})
? array_merge([], $defaultOptions, $conArgs->options, $dbConfig->{$sysCfgProps->options} ?: [])
: array_merge([], $defaultOptions, $conArgs->options);
$options = [];
foreach ($rawOptions as $optionKey => $optionValue) {
if (is_string($optionValue) && mb_strpos($optionValue, '\\PDO::') === 0)
if (defined($optionValue))
$optionValue = constant($optionValue);
if (is_string($optionKey) && mb_strpos($optionKey, '\\PDO::') === 0) {
if (defined($optionKey))
$options[constant($optionKey)] = $optionValue;
} else {
$options[$optionKey] = $optionValue;
}
}
if ($conArgs->auth && !$credentialsInDsn) {
$connection = new $connectionClass(
$dsn,
$dbConfig->{$sysCfgProps->user},
(string) $dbConfig->{$sysCfgProps->password},
$options
);
} else {
$connection = new $connectionClass(
$dsn, NULL, NULL, $options
);
}
return $connection;
}
}