Skip to content

Commit

Permalink
Allow for DSN values (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored Mar 3, 2018
1 parent 26efa49 commit c4840a9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
28 changes: 27 additions & 1 deletion DependencyInjection/BMBackupManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use League\Flysystem\Adapter\Local;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Dropbox\DropboxAdapter;
use Nyholm\DSN;
use Srmklive\Dropbox\Adapter\DropboxAdapter as Dropbox2Adapter;
use League\Flysystem\Rackspace\RackspaceAdapter;
use League\Flysystem\Sftp\SftpAdapter;
Expand All @@ -30,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container)
$config['storage'] = isset($config['storage']) ? $config['storage'] : [];
$config['database'] = isset($config['database']) ? $config['database'] : [];
$this->validateStorage($config['storage']);
$config['database'] = $this->parseDsn($config['database']);

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

foreach ($config as $key=>$storageConfig) {
foreach ($config as $key => $storageConfig) {
$type = $storageConfig['type'];
if (!class_exists($requirements[$type]['test'])) {
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']));
}
}
}

/**
* If a DSN is configured, then let it override other database storages.
* @param array $config
*
* @param array
*/
private function parseDsn(array $config)
{
foreach ($config as $key => $databaseConfig) {
if (isset($databaseConfig['dsn'])) {
$dsn = new DSN($databaseConfig['dsn']);
$config[$key]['type'] = $dsn->getProtocol();
$config[$key]['host'] = $dsn->getFirstHost();
$config[$key]['port'] = $dsn->getFirstPort();
$config[$key]['user'] = $dsn->getUsername();
$config[$key]['pass'] = $dsn->getPassword();
$config[$key]['database'] = $dsn->getDatabase();
unset($config[$key]['dsn']);
}
}

return $config;
}
}
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function getConfigTreeBuilder()
->scalarNode('user')->end()
->scalarNode('pass')->end()
->scalarNode('database')->end()
->scalarNode('dsn')->end()
->booleanNode('singleTransaction')->end()
->booleanNode('ssl')->end()
->arrayNode('ignoreTables')
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ bm_backup_manager:
pass: password
database: test
ignoreTables: ['foo', 'bar']

# If DSN is specified, it will override other values
dsn: 'mysql://root:[email protected]:3306/test_db'
production:
type: postgresql
host: localhost
port: 5432
user: postgres
pass: password
database: test

# If DSN is specified, it will override other values
dsn: 'pgsql://root:[email protected]:5432/test_db'
storage:
local:
type: Local
Expand Down
33 changes: 33 additions & 0 deletions Tests/Unit/DependencyInjection/BMBackupManagerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ public function testReplacementOfConfig()
$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_database', 0, $dbConfig);
}


public function testDsn()
{
$storageConfig = ['local'=>['type'=>'Local', 'root'=>'/foo']];
$dbConfig = ['dev'=>[
'type'=>'mysql',
'host'=>'host.com',
'port'=>'3306',
'user'=>'user',
'pass'=>'pass',
'database'=>'db',
// The DSN should override them all.
'dsn'=>'pgsql://root:[email protected]:5432/test_db',
]];

$this->load([
'storage' => $storageConfig,
'database' => $dbConfig,
]);

$parsedConfig = ['dev'=>[
'type'=>'pgsql',
'host'=>'127.0.0.1',
'port'=>'5432',
'user'=>'root',
'pass'=>'root_pass',
'database'=>'test_db',
]];

$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_storage', 0, $storageConfig);
$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_database', 0, $parsedConfig);
}

/**
* Make sure we can have multiple storage names with the same type
*/
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require": {
"php": "^5.5 || ^7.0",
"backup-manager/backup-manager": "^1.2",
"nyholm/dsn": "^0.1",
"symfony/config": "^2.7 || ^3.1 || ^4.0",
"symfony/console": "^2.7 || ^3.1 || ^4.0",
"symfony/dependency-injection": "^2.7 || ^3.1 || ^4.0",
Expand Down

0 comments on commit c4840a9

Please sign in to comment.