Skip to content

Commit

Permalink
Allow reading from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
nickvergessen committed Jun 9, 2015
1 parent e544478 commit 34913f6
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions core/command/config/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,25 @@ protected function configure() {
->setDescription('Import a list of configs')
->addArgument(
'file',
InputArgument::REQUIRED,
InputArgument::OPTIONAL,
'File with the json array to import'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output) {
$importFile = $input->getArgument('file');
if ($importFile !== null) {
$content = $this->getArrayFromFile($importFile);
} else {
$content = $this->getArrayFromStdin();
}

try {
$configs = $this->getArrayFromFile($importFile);
$configs = $this->validateFileContent($content);
} catch (\UnexpectedValueException $e) {
$output->writeln('<error>' . $e->getMessage(). '</error>');
return;
}

if (!empty($configs['system'])) {
Expand All @@ -81,13 +88,37 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

/**
* Get the content from stdin ("config:import < file.json")
*
* @return string
*/
protected function getArrayFromStdin() {
// Read from stdin. stream_set_blocking is used to prevent blocking
// when nothing is passed via stdin.
stream_set_blocking(STDIN, 0);
$content = file_get_contents('php://stdin');
stream_set_blocking(STDIN, 1);
return $content;
}

/**
* Get the content of the specified file ("config:import file.json")
*
* @param string $importFile
* @return string
*/
protected function getArrayFromFile($importFile) {
$content = file_get_contents($importFile);
return $content;
}

/**
* @param string $content
* @return array
* @throws \UnexpectedValueException when the array is invalid
*/
protected function getArrayFromFile($importFile) {
$helo = file_get_contents($importFile);
$decodedContent = json_decode($helo, true);
protected function validateFileContent($content) {
$decodedContent = json_decode($content, true);
if (!is_array($decodedContent) || empty($decodedContent)) {
throw new \UnexpectedValueException('The file must contain a valid json array');
}
Expand Down

0 comments on commit 34913f6

Please sign in to comment.