Skip to content

Commit

Permalink
Extract YAML dump processing to writeToSymbol method of ConfigLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 28, 2015
1 parent 051f332 commit e931069
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
25 changes: 2 additions & 23 deletions src/LazyRecord/Command/DataSourceCommand/SetDefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
use SQLBuilder\Driver\PDODriverFactory;
use SQLBuilder\ArgumentArray;
use SQLBuilder\Universal\Query\CreateDatabaseQuery;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;
use Exception;
use PDO;

class SetDefaultCommand extends BaseCommand
{
public function brief()
public function brief()
{
return 'set default data source for PDO connections.';
}
Expand All @@ -39,28 +37,9 @@ public function execute($defaultDataSource)
$config = $configLoader->getConfigStash();
$config['data_source']['default'] = $defaultDataSource;

$this->logger->debug("Checking symbol link file: " . $configLoader->symbolFilename);
if (!file_exists($configLoader->symbolFilename)) {
$this->logger->error($configLoader->symbolFilename . " is missing. please use lazy build-conf {filename} to update your config link.");
return false;
}

$targetFile = readlink($configLoader->symbolFilename);
if ($targetFile === false || !file_exists($targetFile)) {
$this->logger->error('Missing target config file. incorrect symbol link.');
return false;
}

$this->logger->debug("Writing config back to $targetFile");

$yaml = Yaml::dump($config, $inlineLevel = 4, $indentSpaces = 2, $exceptionOnInvalidType = true);
if (false === file_put_contents($targetFile, "---\n" . $yaml)) {
$this->logger->error("YAML config update failed: $targetFile");
return false;
}
$configLoader->writeToSymbol();
$this->logger->info("Config file is updated successfully.");
}

}


Expand Down
30 changes: 29 additions & 1 deletion src/LazyRecord/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ArrayAccess;
use PDO;
use LazyRecord\DSN\DSN;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;

/**
* Available config key:
Expand Down Expand Up @@ -62,6 +64,24 @@ public function loadFromSymbol($force = false)
}
}

public function writeToSymbol()
{
if (!file_exists($this->symbolFilename) ) {
throw new Exception("symbol link " . $this->symbolFilename . ' does not exist.');
}

$targetFile = readlink($this->symbolFilename);
if ($targetFile === false || !file_exists($targetFile)) {
throw new Exception('Missing target config file. incorrect symbol link.');
}

$yaml = Yaml::dump($this->config, $inlineLevel = 4, $indentSpaces = 2, $exceptionOnInvalidType = true);
if (false === file_put_contents($targetFile, "---\n" . $yaml)) {
throw new Exception("YAML config update failed: $targetFile");
}
return true;
}

/**
* Load config from array directly.
*
Expand All @@ -79,6 +99,9 @@ public function setLoaded($loaded = true)
}





/**
* Convert data source config to DSN object
*
Expand All @@ -105,13 +128,18 @@ static public function buildDSNObject(array $config)
}






static public function preprocessConfig(array $config)
{
if (isset($config['data_source']['nodes'])) {
$config['data_source']['nodes'] = self::preprocessDataSourceConfig($config['data_source']['nodes']);
} else if (isset($config['data_sources'])) {
// convert 'data_sources' to ['data_sources']['nodes']
$config['data_source']['nodes'] = $config['data_sources'] = self::preprocessDataSourceConfig($config['data_sources']);
$config['data_source']['nodes'] = self::preprocessDataSourceConfig($config['data_sources']);
unset($config['data_sources']);
}
return $config;
}
Expand Down

0 comments on commit e931069

Please sign in to comment.