Skip to content

Commit

Permalink
[chain] Improve placeholder default values management. (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Dec 8, 2016
1 parent 9582b34 commit eec3f76
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
27 changes: 25 additions & 2 deletions src/Command/Chain/ChainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ protected function configure()
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
// Check if the constructor passed a value for file.
$file = $input->getOption('file');

if (!$file) {
Expand Down Expand Up @@ -169,8 +168,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$chainContent = $this->getFileContents($file);
$environmentPlaceHolders = $this->extractEnvironmentPlaceHolders($chainContent);
$inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);

if ($inlinePlaceHolders) {
foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
if (!strpos($inlinePlaceHolder, '|')) {
continue;
}

$placeholderParts = explode('|', $inlinePlaceHolder);
$inlinePlaceHolder = $placeholderParts[0];
$inlinePlaceHolderDefault = $placeholderParts[1];

if (!$inlinePlaceHolderDefault) {
continue;
}

if ($placeholder && array_key_exists($inlinePlaceHolder, $placeholder[0])) {
continue;
}

$placeholder[0][$inlinePlaceHolder] = $inlinePlaceHolderDefault;
}
}

$environmentPlaceHolders = $this->extractEnvironmentPlaceHolders($chainContent);
$envPlaceHolderMap = [];
$missingEnvironmentPlaceHolders = [];
foreach ($environmentPlaceHolders as $envPlaceHolder) {
Expand Down Expand Up @@ -230,6 +252,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$missingInlinePlaceHolders = [];

foreach ($inlinePlaceHolders as $inlinePlaceHolder) {
if (!array_key_exists($inlinePlaceHolder, $inlinePlaceHolderMap)) {
$missingInlinePlaceHolders[$inlinePlaceHolder] = sprintf(
Expand Down
43 changes: 28 additions & 15 deletions src/Command/Chain/ChainCustomCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Command\Shared\InputTrait;

/**
* Class ChainCustomCommand
Expand All @@ -22,6 +23,7 @@
class ChainCustomCommand extends Command
{
use CommandTrait;
use InputTrait;

/**
* @var string
Expand All @@ -39,12 +41,12 @@ class ChainCustomCommand extends Command
protected $file;

/**
* ChainRegister constructor.
*
* @param $name
* @param $description
* @param $file
*/
* ChainRegister constructor.
*
* @param $name
* @param $description
* @param $file
*/
public function __construct($name, $description, $file)
{
$this->name = $name;
Expand All @@ -55,8 +57,8 @@ public function __construct($name, $description, $file)
}

/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
protected function configure()
{
$this
Expand All @@ -71,23 +73,34 @@ protected function configure()
}

/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('chain');

$arguments = [
'command' => 'chain',
'--file' => $this->file,
'--placeholder' => $input->getOption('placeholder'),
'--generate-inline' => $input->hasOption('generate-inline'),
'--generate-chain' => $input->hasOption('generate-chain'),
'--learning' => $input->hasOption('learning'),
'--no-interaction' => $input->hasOption('no-interaction')
];

if ($placeholder = $input->getOption('placeholder')) {
$arguments['--placeholder'] = $this->inlineValueAsArray($placeholder);
}

foreach($input->getOptions() as $option => $value) {
if ($option != 'placeholder' && $value) {
if (is_bool($value)) {
$value = true;
}
$arguments['--'.$option] = $value;
}
}

$commandInput = new ArrayInput($arguments);
if (array_key_exists('--no-interaction', $arguments)) {
$commandInput->setInteractive(false);
}

return $command->run($commandInput, $output);
}
Expand Down

0 comments on commit eec3f76

Please sign in to comment.