Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class Config
- *
- */
- class Config
- {
- /** @var array */
- private $configs;
- /** @var string */
- private $dsn;
- /**
- * データソース名を適切なものを取得する
- * (MySQL/PostgreSQL/CUBRID/SQLite 3/SQLite 2/Oracleに対応)
- * @return mixed
- */
- public function buildDsn()
- {
- if (preg_match('/^(MySQL)+$/', $this->configs['type'])) {
- // MySQLの場合
- $this->dsn = 'mysql:';
- if (! empty($this->configs['server'])) {
- $this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
- }
- if (! empty($this->configs['port'])) {
- $this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
- }
- if (! empty($this->configs['dbname'])) {
- $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
- }
- if (! empty($this->configs['charset'])) {
- $this->dsn .= 'charset=' . $this->checkVar($this->configs['charset']);
- }
- if (! empty($this->configs['socket'])) {
- $this->dsn .= 'unix_socket=' . $this->checkVar($this->configs['socket']);
- }
- } elseif (preg_match('/^(PostgreSQL)+$/', $this->configs['type'])) {
- // PostgreSQLの場合
- $this->dsn = 'pgsql:';
- if (! empty($this->configs['server'])) {
- $this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
- }
- if (! empty($this->configs['port'])) {
- $this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
- }
- if (! empty($this->configs['dbname'])) {
- $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
- }
- if (! empty($this->configs['username'])) {
- $this->dsn .= 'user=' . $this->checkVar($this->configs['username']);
- }
- if (! empty($this->configs['password'])) {
- $this->dsn .= 'password=' . $this->checkVar($this->configs['password']);
- }
- } elseif (preg_match('/^(CUBRID)+$/', $this->configs['type'])) {
- $this->dsn = 'cubrid:';
- if (! empty($this->configs['server'])) {
- $this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
- }
- if (! empty($this->configs['port'])) {
- $this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
- }
- if (! empty($this->configs['dbname'])) {
- $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
- }
- } elseif (preg_match('/^(SQLite 3)+$/', $this->configs['type'])) {
- // SQLite 3の場合
- $this->dsn = 'sqlite:';
- if (! empty($this->configs['dbname'])) {
- $this->dsn .= $this->checkVar($this->configs['dbname']);
- }
- } elseif (preg_match('/^(SQLite 2)+$/', $this->configs['type'])) {
- // SQLite 2の場合
- $this->dsn = 'sqlite2:';
- if (! empty($this->configs['dbname'])) {
- $this->dsn .= $this->checkVar($this->configs['dbname']);
- }
- } elseif (preg_match('/^(Oracle)+$/', $this->configs['type'])) {
- // Oracleデータベースの場合
- $this->dsn = 'oci:';
- if (! empty($this->configs['dbname'])) {
- $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
- }
- if (! empty($this->configs['charset'])) {
- $this->dsn .= 'charset=' . $this->checkVar($this->configs['charset']);
- }
- }
- return preg_replace('/;$/', '', $this->dsn);
- }
- /**
- * データベースの文字コードを取得する
- * @return mixed|string
- */
- public function buildCharset()
- {
- return isset($this->configs['charset']) ? $this->configs['charset'] : '';
- }
- /**
- * データベースサーバーを取得
- * @return mixed
- */
- public function buildServer()
- {
- return isset($this->configs['server']) ? (string)$this->configs['server'] : '' ;
- }
- /**
- * データベース名を取得する
- * @return mixed
- */
- public function buildDatabaseName()
- {
- return isset($this->configs['dbname']) ? (string)$this->configs['dbname'] : '';
- }
- /**
- * データベースのポートを取得する
- * @return mixed
- */
- public function buildPort()
- {
- return isset($this->configs['port']) ? $this->configs['port'] : '';
- }
- /**
- * Unixソケットを取得する
- * @return mixed
- */
- public function buildSocket()
- {
- return isset($this->configs['socket']) ? (string)$this->configs['socket'] : '';
- }
- /**
- * データベースユーザー名を取得
- * @return string
- */
- public function buildUserName()
- {
- return isset($this->configs['username']) ? (string)$this->configs['username'] : '' ;
- }
- /**
- * データベースのパスワードを取得
- * @return string
- */
- public function buildPassword()
- {
- return isset($this->configs['password']) ? (string)$this->configs['password'] : '' ;
- }
- /**
- * 変数にセットされているかチェック
- * (buildDsnメソッド内で使用)
- * @param $var
- * @return string
- */
- private function checkVar($var)
- {
- // 渡されたものが nullもしくは空っぽの場合
- if (is_null($var) || empty($var)) {
- return $var . '';
- } elseif (isset($var)) {
- return $var . ';';
- }
- return $var;
- }
- /**
- * Unixソケットを設定する
- * @param null $socket
- * @return $this
- */
- public function databaseSocket($socket = null)
- {
- if (! empty($socket)) {
- $this->configs['socket'] = $socket;
- }
- return $this;
- }
- /**
- * 文字コードを設定する
- * @param null $charset
- * @return $this
- */
- public function databaseCharset($charset = null)
- {
- if (! empty($charset)) {
- $this->configs['charset'] = $charset;
- }
- return $this;
- }
- /**
- * データベース名を設定する
- * @param null $dbname
- * @return $this
- */
- public function databaseName($dbname = null)
- {
- if (! empty($dbname)) {
- $this->configs['dbname'] = $dbname;
- }
- return $this;
- }
- /**
- * データベースのパスワードを設定する
- * @param null $password
- * @return $this
- */
- public function databasePassword($password = null)
- {
- if (! empty($password)) {
- $this->configs['password'] = $password;
- }
- return $this;
- }
- /**
- * データベースユーザー名を設定する
- * @param null $username
- * @return $this
- */
- public function databaseUserName($username = null)
- {
- if (! empty($username)) {
- $this->configs['username'] = $username;
- }
- return $this;
- }
- /**
- * データベースのポートを設定する
- * @param null $port
- * @return $this
- */
- public function databasePort($port = null)
- {
- if (! empty($port)) {
- $this->configs['port'] = $port;
- }
- return $this;
- }
- /**
- * データベースサーバーを設定する
- * @param null $server
- * @return $this
- */
- public function databaseServer($server = null)
- {
- if (! empty($server)) {
- $this->configs['server'] = $server;
- }
- return $this;
- }
- /**
- * データベースの種類を設定する
- * @param null $type
- * @return $this
- */
- public function databaseType($type = null)
- {
- if (! empty($type)) {
- $this->configs['type'] = $type;
- }
- return $this;
- }
- /**
- * メソッドチェーンを作成する
- * @return Config
- */
- public static function create()
- {
- return new self;
- }
- }
Add Comment
Please, Sign In to add comment