Skip to content

Commit

Permalink
Add new trait ArrayInputTrait, which will parse nested data. Switch m…
Browse files Browse the repository at this point in the history
…uti value options to array to handle them properly, when receive params through the options (#3712)
  • Loading branch information
LOBsTerr authored and jmolivas committed Jan 20, 2018
1 parent ca88d11 commit 723d580
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Command/Generate/ThemeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\Console\Command\Generate;

use Drupal\Console\Command\Shared\ArrayInputTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -32,6 +33,7 @@ class ThemeCommand extends Command
use ConfirmationTrait;
use ThemeRegionTrait;
use ThemeBreakpointTrait;
use ArrayInputTrait;

/**
* @var Manager
Expand Down Expand Up @@ -148,7 +150,7 @@ protected function configure()
->addOption(
'libraries',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.generate.theme.options.libraries')
)
->addOption(
Expand All @@ -160,13 +162,13 @@ protected function configure()
->addOption(
'regions',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.generate.theme.options.regions')
)
->addOption(
'breakpoints',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.generate.theme.options.breakpoints')
)
->setAliases(['gt']);
Expand Down Expand Up @@ -199,6 +201,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$libraries = $input->getOption('libraries');
$regions = $input->getOption('regions');
$breakpoints = $input->getOption('breakpoints');
$noInteraction = $input->getOption('no-interaction');

// Parse nested data.
if ($noInteraction) {
$libraries = $this->explodeInlineArray($libraries);
$regions = $this->explodeInlineArray($regions);
$breakpoints = $this->explodeInlineArray($breakpoints);
}

$this->generator->generate(
$theme,
Expand Down Expand Up @@ -345,9 +355,11 @@ function ($theme_path) use ($machine_name) {
) {
// @see \Drupal\Console\Command\Shared\ThemeRegionTrait::libraryQuestion
$libraries = $this->libraryQuestion();
$input->setOption('libraries', $libraries);
}
} else {
$libraries = $this->explodeInlineArray($libraries);
}
$input->setOption('libraries', $libraries);

// --regions option.
$regions = $input->getOption('regions');
Expand All @@ -359,9 +371,11 @@ function ($theme_path) use ($machine_name) {
) {
// @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
$regions = $this->regionQuestion();
$input->setOption('regions', $regions);
}
} else {
$regions = $this->explodeInlineArray($regions);
}
$input->setOption('regions', $regions);

// --breakpoints option.
$breakpoints = $input->getOption('breakpoints');
Expand All @@ -373,8 +387,10 @@ function ($theme_path) use ($machine_name) {
) {
// @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
$breakpoints = $this->breakpointQuestion();
$input->setOption('breakpoints', $breakpoints);
}
} else {
$breakpoints = $this->explodeInlineArray($breakpoints);
}
$input->setOption('breakpoints', $breakpoints);
}
}
45 changes: 45 additions & 0 deletions src/Command/Shared/ArrayInputTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @file
* Contains Drupal\Console\Command\Shared\ArrayInputTrait.
*/

namespace Drupal\Console\Command\Shared;

/**
* Class ArrayInputTrait
*
* @package Drupal\Console\Command
*/
trait ArrayInputTrait
{
/**
* Parse strings to array '"key":"value","key1":"value1"'.
*
* @param string $inlineInputs
* Input from the user.
* @return array
* Input array.
*/
public function explodeInlineArray($inlineInputs)
{
$inputs = [];
foreach ($inlineInputs as $inlineInput) {
$explodeInput = explode(',', $inlineInput);
$parameters = [];
foreach ($explodeInput as $inlineParameter) {
$inlineParameter = trim($inlineParameter);
list($key, $value) = explode(':', $inlineParameter);
$key = rtrim(ltrim($key, '"'), '"');
$value = rtrim(ltrim($value, '"'), '"');
if (!empty($value)) {
$parameters[$key] = $value;
}
}
$inputs[] = $parameters;
}

return $inputs;
}
}

0 comments on commit 723d580

Please sign in to comment.