/** * testMapUsage * * Map custom schemes in the dsn class to mysql for the default port (convenience only) * Map custom schemes in the wrapper class to a custom datasources * * @dataProvider mapUsageProvider * @return void */ public function testMapUsage($url, $expected) { Dsn::map(['nysql' => '\\AD7six\\Dsn\\Db\\MysqlDsn', 'oysql' => '\\AD7six\\Dsn\\Db\\MysqlDsn']); DbDsn::map(['mongo' => 'MongoDb.MongodbSource', 'mysql' => 'Plugin.DboClass', 'nysql' => 'Plugin.Directory/DboClass', 'oysql' => 'Plugin.Directory/SubDirectory/DboClass']); $dsn = new DbDsn($url); $return = $dsn->toArray(); $this->assertSame($expected, $return, 'The url should parse as expected'); $return = $dsn->__toString(); $this->assertSame($url, $return, 'The dsn should parse back to the same url'); }
/** * Return the array representation of this dsn * * @return array */ public function toArray() { $raw = $this->dsn->toArray(); $allKeys = array_unique(array_merge(static::$mandatoryKeys, array_keys($raw))); $return = []; foreach ($allKeys as $key) { if (isset($this->keyMap[$key])) { $key = $this->keyMap[$key]; if (!$key) { continue; } } $val = $this->{$key}; if ($val !== null) { $return[$key] = $val; } } return $return; }
/** * @param $config * * @throws \Exception */ private static function prepareDoctrine(&$config) { $databases = $config['databases']; if (isset($databases['main'])) { $config['parameters']['main_database'] = $databases['main']; } else { $config['parameters']['main_database'] = $databases['mysql']['enabled'] ? 'mysql' : 'mongo'; } $mapping = array_merge(['AppBundle' => ['type' => 'yml', 'prefix' => 'Discord\\Base\\AppBundle\\Model', 'alias' => 'App']], isset($config['databases']['mappings']) ? $config['databases']['mappings'] : []); unset($config['databases']); if ($databases['mysql']['enabled']) { /** @var MysqlDsn $mysql */ $mysql = Dsn::parse($databases['mysql']['dsn'])->toArray(); $config['doctrine'] = ['dbal' => ['connections' => ['default' => ['dbname' => $mysql['database'], 'host' => $mysql['host'], 'port' => $mysql['port'], 'user' => $mysql['user'], 'password' => $mysql['pass']]]], 'orm' => ['auto_mapping' => false, 'mappings' => !isset($databases['main']) || $databases['main'] === 'mysql' ? $mapping : null]]; } if ($databases['mongo']['enabled']) { $config['doctrine_mongodb'] = ['connections' => ['default' => ['server' => $databases['mongo']['dsn']]], 'document_managers' => ['default' => ['auto_mapping' => false, 'mappings' => !isset($databases['main']) || $databases['main'] === 'mongo' ? $mapping : null]]]; } }
/** * __construct * * Create and assign the raw dsn instance, merge default options * and then set keyMap and replacements properties from passed options * * @param string $url * @param array $options * @return void */ public function __construct($url = '', $options = []) { $this->dsn = DsnInstance::parse($url); $options = $this->mergeDefaultOptions($options); $opts = ['keyMap', 'replacements']; foreach ($opts as $key) { if (!empty($options[$key])) { $this->{$key}($options[$key]); } } }
/** * testMap * * @return void */ public function testMap() { Dsn::map('special', '\\AD7six\\Dsn\\Test\\TestCase\\SpecialTestDsn'); $dsn = Dsn::parse('special://host/path'); $this->assertInstanceOf('\\AD7six\\Dsn\\Test\\TestCase\\SpecialTestDsn', $dsn, 'Modifying the scheme map should be honored'); }