<?php
/**
 * DATABASE_CONFIG
 *
 * @author   Jun Nishikawa <topaz2@m0n0m0n0.com>
 * @link     http://www.netcommons.org NetCommons Project
 * @license  http://www.netcommons.org/license.txt NetCommons License
 * @codingStandardsIgnoreFile
 * @codeCoverageIgnore
 **/

class DATABASE_CONFIG {

	private $identities = array(
		'mysql' => array(
			'datasource' => 'Database/Mysql',
			'host' => '127.0.0.1',
			'login' => 'root'
		),
		'mariadb' => array(
			'datasource' => 'Database/Mysql',
			'host' => '0.0.0.0',
			'login' => 'travis'
		),
		'pgsql' => array(
			'datasource' => 'Database/Postgres',
			'host' => '127.0.0.1',
			'login' => 'postgres',
			'database' => 'cakephp_test',
			'schema' => array(
				'master' => 'public',
				'slave1' => 'public',
				'test' => 'public',
				'test2' => 'test2',
				'test_database_three' => 'test3'
			)
		),
	);

	public $master = array(
		'persistent' => false,
		'host' => '',
		'login' => '',
		'password' => '',
		'database' => 'cakephp_test',
		'prefix' => ''
	);

	public $slave1 = array(
		'persistent' => false,
		'host' => '',
		'login' => '',
		'password' => '',
		'database' => 'cakephp_test',
		'prefix' => ''
	);

	public $test = array(
		'persistent' => false,
		'host' => '',
		'login' => '',
		'password' => '',
		'database' => 'cakephp_test',
		'prefix' => ''
	);

	public $test2 = array(
		'persistent' => false,
		'host' => '',
		'login' => '',
		'password' => '',
		'database' => 'cakephp_test2',
		'prefix' => ''
	);

	public $test_database_three = array(
		'persistent' => false,
		'host' => '',
		'login' => '',
		'password' => '',
		'database' => 'cakephp_test3',
		'prefix' => ''
	);

	public function __construct() {
		$db = 'mysql';
		if (!empty($_SERVER['DB'])) {
			$db = $_SERVER['DB'];
		}
		if (in_array($db, ['mariadb-10.0', 'mariadb-10.1', 'mariadb-10.2'], true)) {
			$db = 'mariadb';
		}
		foreach (array('master', 'slave1', 'test', 'test2', 'test_database_three') as $source) {
			$config = array_merge($this->{$source}, $this->identities[$db]);
			if (is_array($config['database'])) {
				$config['database'] = $config['database'][$source];
			}
			if (!empty($config['schema']) && is_array($config['schema'])) {
				$config['schema'] = $config['schema'][$source];
			}
			$this->{$source} = $config;
		}
	}
}
