Skip to content

Commit c4840a9

Browse files
authored
Allow for DSN values (#39)
1 parent 26efa49 commit c4840a9

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

DependencyInjection/BMBackupManagerExtension.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use League\Flysystem\Adapter\Local;
77
use League\Flysystem\AwsS3v3\AwsS3Adapter;
88
use League\Flysystem\Dropbox\DropboxAdapter;
9+
use Nyholm\DSN;
910
use Srmklive\Dropbox\Adapter\DropboxAdapter as Dropbox2Adapter;
1011
use League\Flysystem\Rackspace\RackspaceAdapter;
1112
use League\Flysystem\Sftp\SftpAdapter;
@@ -30,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container)
3031
$config['storage'] = isset($config['storage']) ? $config['storage'] : [];
3132
$config['database'] = isset($config['database']) ? $config['database'] : [];
3233
$this->validateStorage($config['storage']);
34+
$config['database'] = $this->parseDsn($config['database']);
3335

3436
$managerIdMap = [
3537
'Local' => 'backup_manager.filesystems.local_filesystem',
@@ -74,11 +76,35 @@ private function validateStorage(array $config)
7476
'Sftp' => ['package'=>'league/flysystem-sftp:^1.0', 'test'=>SftpAdapter::class],
7577
];
7678

77-
foreach ($config as $key=>$storageConfig) {
79+
foreach ($config as $key => $storageConfig) {
7880
$type = $storageConfig['type'];
7981
if (!class_exists($requirements[$type]['test'])) {
8082
throw new \LogicException(sprintf('To use the configuration key "%s" in "bm_backup_manager.stroage.%s.type" you need to install "%s"', $type, $key, $requirements[$type]['package']));
8183
}
8284
}
8385
}
86+
87+
/**
88+
* If a DSN is configured, then let it override other database storages.
89+
* @param array $config
90+
*
91+
* @param array
92+
*/
93+
private function parseDsn(array $config)
94+
{
95+
foreach ($config as $key => $databaseConfig) {
96+
if (isset($databaseConfig['dsn'])) {
97+
$dsn = new DSN($databaseConfig['dsn']);
98+
$config[$key]['type'] = $dsn->getProtocol();
99+
$config[$key]['host'] = $dsn->getFirstHost();
100+
$config[$key]['port'] = $dsn->getFirstPort();
101+
$config[$key]['user'] = $dsn->getUsername();
102+
$config[$key]['pass'] = $dsn->getPassword();
103+
$config[$key]['database'] = $dsn->getDatabase();
104+
unset($config[$key]['dsn']);
105+
}
106+
}
107+
108+
return $config;
109+
}
84110
}

DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function getConfigTreeBuilder()
9191
->scalarNode('user')->end()
9292
->scalarNode('pass')->end()
9393
->scalarNode('database')->end()
94+
->scalarNode('dsn')->end()
9495
->booleanNode('singleTransaction')->end()
9596
->booleanNode('ssl')->end()
9697
->arrayNode('ignoreTables')

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ bm_backup_manager:
7171
pass: password
7272
database: test
7373
ignoreTables: ['foo', 'bar']
74+
75+
# If DSN is specified, it will override other values
76+
dsn: 'mysql://root:[email protected]:3306/test_db'
7477
production:
7578
type: postgresql
7679
host: localhost
7780
port: 5432
7881
user: postgres
7982
pass: password
8083
database: test
84+
85+
# If DSN is specified, it will override other values
86+
dsn: 'pgsql://root:[email protected]:5432/test_db'
8187
storage:
8288
local:
8389
type: Local

Tests/Unit/DependencyInjection/BMBackupManagerExtensionTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ public function testReplacementOfConfig()
3333
$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_database', 0, $dbConfig);
3434
}
3535

36+
37+
public function testDsn()
38+
{
39+
$storageConfig = ['local'=>['type'=>'Local', 'root'=>'/foo']];
40+
$dbConfig = ['dev'=>[
41+
'type'=>'mysql',
42+
'host'=>'host.com',
43+
'port'=>'3306',
44+
'user'=>'user',
45+
'pass'=>'pass',
46+
'database'=>'db',
47+
// The DSN should override them all.
48+
'dsn'=>'pgsql://root:[email protected]:5432/test_db',
49+
]];
50+
51+
$this->load([
52+
'storage' => $storageConfig,
53+
'database' => $dbConfig,
54+
]);
55+
56+
$parsedConfig = ['dev'=>[
57+
'type'=>'pgsql',
58+
'host'=>'127.0.0.1',
59+
'port'=>'5432',
60+
'user'=>'root',
61+
'pass'=>'root_pass',
62+
'database'=>'test_db',
63+
]];
64+
65+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_storage', 0, $storageConfig);
66+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_database', 0, $parsedConfig);
67+
}
68+
3669
/**
3770
* Make sure we can have multiple storage names with the same type
3871
*/

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"require": {
1414
"php": "^5.5 || ^7.0",
1515
"backup-manager/backup-manager": "^1.2",
16+
"nyholm/dsn": "^0.1",
1617
"symfony/config": "^2.7 || ^3.1 || ^4.0",
1718
"symfony/console": "^2.7 || ^3.1 || ^4.0",
1819
"symfony/dependency-injection": "^2.7 || ^3.1 || ^4.0",

0 commit comments

Comments
 (0)