default = $default; $this->configManager = $config; } public static function connection(?string $name = null): PDO { return self::getInstance()->getConnection($name); } public function getConnection(?string $name = null): PDO { $name = $name ?? $this->default; return $this->connections[$name] ?? $this->addConnection($name); } public function addConnection(string $name): PDO { $section = "Database.{$name}"; if (!$this->configManager->contains($section, 'driver')) { throw new InvalidArgumentException('Config section not found!'); } $driver = $this->configManager->get($section, 'driver'); if (!in_array($driver, self::SUPPORTED)) { throw new InvalidArgumentException('Unsupported driver.'); } $dsn = $driver . ':'; $options = [ PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => false, ]; switch ($driver) { case 'sqlite': if ($this->configManager->get($section, 'memory', 'bool', false)) { $dsn .= ':memory:'; } else { $databasePath = realpath( $this->configManager->get($section, 'database', 'string', __DIR__ . '/../store/misuzu.db') ); if ($databasePath === false) { throw new \UnexpectedValueException("Database does not exist."); } $dsn .= $databasePath . ';'; } break; case 'mysql': $is_unix_socket = $this->configManager->contains($section, 'unix_socket'); if ($is_unix_socket) { $dsn .= 'unix_socket=' . $this->configManager->get($section, 'unix_socket', 'string') . ';'; } else { $dsn .= 'host=' . $this->configManager->get($section, 'host', 'string', self::DEFAULT_HOST) . ';'; $dsn .= 'port=' . $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_MYSQL) . ';'; } $dsn .= 'charset=' . ( $this->configManager->contains($section, 'charset') ? $this->configManager->get($section, 'charset', 'string') : 'utf8mb4' ) . ';'; $dsn .= 'dbname=' . $this->configManager->get($section, 'database', 'string', 'misuzu') . ';'; $options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"; break; } $connection = new PDO( $dsn, $this->configManager->get($section, 'username', 'string', null), $this->configManager->get($section, 'password', 'string', null), $options ); return $this->connections[$name] = $connection; } }