Skip to content

Commit

Permalink
Fixes #3054: Only replace command aliases (e.g. 'en' + 'si') when the…
Browse files Browse the repository at this point in the history
…y appear as the first non-option / non-alias parameter on the argv list. (#3066)
  • Loading branch information
greg-1-anderson authored Oct 14, 2017
1 parent 2f52b20 commit 5f64eb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
57 changes: 25 additions & 32 deletions src/Preflight/ArgsRemapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
*/
class ArgsRemapper
{
protected $remapOptions;
protected $remapCommandAliases;

/**
* ArgsRemapper constructor
*/
public function __construct($remap, $remove)
public function __construct($remapOptions, $remapCommandAliases)
{
$this->remap = $remap;
$this->remove = $remove;
$this->remapOptions = $remapOptions;
$this->remapCommandAliases = $remapCommandAliases;
}

/**
Expand All @@ -24,8 +27,9 @@ public function __construct($remap, $remove)
public function remap($argv)
{
$result = [];
$sawCommmand = false;
foreach ($argv as $arg) {
$arg = $this->remapArgument($arg);
$arg = $this->checkRemap($arg, $sawCommmand);
if (isset($arg)) {
$result[] = $arg;
}
Expand All @@ -34,47 +38,36 @@ public function remap($argv)
}

/**
* Apply any transformations to a single arg.
* Check to see if the provided single arg needs to be remapped. If
* it does, then the remapping is performed.
*
* @param stinrg $arg One arguent to remap
* @param stinrg $arg One arguent to inspect
* @return string The altered argument
*/
protected function remapArgument($arg)
protected function checkRemap($arg, &$sawCommmand)
{
if ($this->checkRemoval($arg)) {
return null;
if (!$sawCommmand && ctype_alpha($arg[0])) {
$sawCommand = true;
return $this->remapCommandAlias($arg);
}
return $this->checkRemap($arg);
return $this->remapOptions($arg);
}

/**
* Check to see if the provided argument should be removed / ignored.
*
* @param stinrg $arg One arguent to inspect
* @return bool
*/
protected function checkRemoval($arg)
protected function remapOptions($arg)
{
foreach ($this->remove as $removalCandidate) {
if ($this->matches($arg, $removalCandidate)) {
return true;
foreach ($this->remapOptions as $from => $to) {
if ($this->matches($arg, $from)) {
return $to . substr($arg, strlen($from));
}
}
return false;
return $arg;
}

/**
* Check to see if the provided single arg needs to be remapped. If
* it does, then the remapping is performed.
*
* @param stinrg $arg One arguent to inspect
* @return string The altered argument
*/
protected function checkRemap($arg)
protected function remapCommandAlias($arg)
{
foreach ($this->remap as $from => $to) {
if ($this->matches($arg, $from)) {
return $to . substr($arg, strlen($from));
foreach ($this->remapCommandAliases as $from => $to) {
if ($arg == $from) {
return $to;
}
}
return $arg;
Expand Down
25 changes: 14 additions & 11 deletions src/Preflight/Preflight.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function init(PreflightArgs $preflightArgs)
* Eventually, we might want to expose this table to some form of
* 'help' output, so folks can see the available conversions.
*/
protected function remapArguments()
protected function remapOptions()
{
return [
'--ssh-options' => '-Dssh.options',
Expand All @@ -94,21 +94,24 @@ protected function remapArguments()
'--db-su' => '-Dsql.db-su',
'--notify' => '-Dnotify.duration',
'--xh-link' => '-Dxh.link',
// Map command aliases which Console complains about.
'si' => 'site-install',
'en' => 'pm-enable',
];
}

/**
* Removal table for arguments. Anything found here will be silently
* removed. The option value is ignored; ergo, both --strict and
* --strict=0 will be removed; however, --stricter will not be removed.
* Symfony Console dislikes certain command aliases, because
* they are too similar to other Drush commands that contain
* the same characters. To avoid the "I don't know which
* command you mean"-type errors, we will replace problematic
* aliases with their longhand equivalents.
*
* This should be fixed in Symfony Console.
*/
protected function removeArguments()
protected function remapCommandAliases()
{
// Now we are going to support rather than remove --strict.
return [];
return [
'si' => 'site-install',
'en' => 'pm-enable',
];
}

/**
Expand All @@ -119,7 +122,7 @@ protected function removeArguments()
public function preflightArgs($argv)
{
$argProcessor = new ArgsPreprocessor();
$remapper = new ArgsRemapper($this->remapArguments(), $this->removeArguments());
$remapper = new ArgsRemapper($this->remapOptions(), $this->remapCommandAliases());
$preflightArgs = new PreflightArgs([]);
$argProcessor->setArgsRemapper($remapper);

Expand Down

0 comments on commit 5f64eb5

Please sign in to comment.