Skip to content

Commit 7f92d2e

Browse files
authored
Bugfix when using enviroment variables and dsn (#43)
* Do not resolve arguments on build * Bugfix * cs
1 parent fc8597b commit 7f92d2e

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

DependencyInjection/BMBackupManagerExtension.php

-25
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function load(array $configs, ContainerBuilder $container)
3131
$config['storage'] = isset($config['storage']) ? $config['storage'] : [];
3232
$config['database'] = isset($config['database']) ? $config['database'] : [];
3333
$this->validateStorage($config['storage']);
34-
$config['database'] = $this->parseDsn($config['database']);
3534

3635
$managerIdMap = [
3736
'Local' => 'backup_manager.filesystems.local_filesystem',
@@ -83,28 +82,4 @@ private function validateStorage(array $config)
8382
}
8483
}
8584
}
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-
}
11085
}

DependencyInjection/Configuration.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function getConfigTreeBuilder()
6464
$valid = true;
6565
foreach ($databases as $name => $d) {
6666
if (isset($d['dsn'])) {
67-
// "Fake" "type" for now.
68-
$d['type'] = substr($d['dsn'], 0, strpos($d['dsn'], ':'));
67+
// We cannot resolve the DSN now. It might be a environment variable.
68+
continue;
6969
}
70-
if (!isset($d['type'])) {
70+
if (empty($d['type'])) {
7171
throw new InvalidConfigurationException(sprintf('You must define a "type" or "dsn" for database "%s"', $name));
7272
}
7373
if ($d['type'] !== 'mysql') {

Factory/ConfigFactory.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace BM\BackupManagerBundle\Factory;
4+
5+
use BackupManager\Config\Config;
6+
use Nyholm\DSN;
7+
8+
/**
9+
* A factory class to resolve DSN
10+
*
11+
* @author Tobias Nyholm <[email protected]>
12+
*/
13+
class ConfigFactory
14+
{
15+
/**
16+
* If a DSN is configured, then let it override other database storages.
17+
* @param array $config
18+
* @return Config
19+
*/
20+
public static function createConfig(array $config)
21+
{
22+
foreach ($config as $key => $databaseConfig) {
23+
if (isset($databaseConfig['dsn'])) {
24+
$dsn = new DSN($databaseConfig['dsn']);
25+
$config[$key]['type'] = $dsn->getProtocol();
26+
$config[$key]['host'] = $dsn->getFirstHost();
27+
$config[$key]['port'] = $dsn->getFirstPort();
28+
$config[$key]['user'] = $dsn->getUsername();
29+
$config[$key]['pass'] = $dsn->getPassword();
30+
$config[$key]['database'] = $dsn->getDatabase();
31+
unset($config[$key]['dsn']);
32+
}
33+
}
34+
35+
return new Config($config);
36+
}
37+
}

Resources/config/services.yml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ services:
5454
# Database
5555
backup_manager.config_database:
5656
class: BackupManager\Config\Config
57+
factory: 'BM\BackupManagerBundle\Factory\ConfigFactory::createConfig'
5758
arguments: [[]]
5859
public: false
5960

0 commit comments

Comments
 (0)