Skip to content

Commit

Permalink
Bugfix when using enviroment variables and dsn (#43)
Browse files Browse the repository at this point in the history
* Do not resolve arguments on build

* Bugfix

* cs
  • Loading branch information
Nyholm authored Mar 5, 2018
1 parent fc8597b commit 7f92d2e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
25 changes: 0 additions & 25 deletions DependencyInjection/BMBackupManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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 @@ -83,28 +82,4 @@ private function validateStorage(array $config)
}
}
}

/**
* 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;
}
}
6 changes: 3 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function getConfigTreeBuilder()
$valid = true;
foreach ($databases as $name => $d) {
if (isset($d['dsn'])) {
// "Fake" "type" for now.
$d['type'] = substr($d['dsn'], 0, strpos($d['dsn'], ':'));
// We cannot resolve the DSN now. It might be a environment variable.
continue;
}
if (!isset($d['type'])) {
if (empty($d['type'])) {
throw new InvalidConfigurationException(sprintf('You must define a "type" or "dsn" for database "%s"', $name));
}
if ($d['type'] !== 'mysql') {
Expand Down
37 changes: 37 additions & 0 deletions Factory/ConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace BM\BackupManagerBundle\Factory;

use BackupManager\Config\Config;
use Nyholm\DSN;

/**
* A factory class to resolve DSN
*
* @author Tobias Nyholm <[email protected]>
*/
class ConfigFactory
{
/**
* If a DSN is configured, then let it override other database storages.
* @param array $config
* @return Config
*/
public static function createConfig(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 new Config($config);
}
}
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ services:
# Database
backup_manager.config_database:
class: BackupManager\Config\Config
factory: 'BM\BackupManagerBundle\Factory\ConfigFactory::createConfig'
arguments: [[]]
public: false

Expand Down

0 comments on commit 7f92d2e

Please sign in to comment.