Skip to content

Commit d77b2d4

Browse files
Add quotation to children's input options if required (#42)
1 parent 4a68593 commit d77b2d4

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Parallelization.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,35 @@ private function serializeInputOptions(InputInterface $input, array $blackListPa
541541
$optionString .= ' --' . $name;
542542
} elseif ($option->isArray()) {
543543
foreach ($value as $arrayValue) {
544-
$optionString .= ' --'.$name.'='.$arrayValue;
544+
$optionString .= ' --'.$name.'='.$this->quoteOptionValue($arrayValue);
545545
}
546546
} else {
547-
$optionString .= ' --'.$name.'='.$value;
547+
$optionString .= ' --'.$name.'='.$this->quoteOptionValue($value);
548548
}
549549

550550
$preparedOptionList[] = $optionString;
551551
}
552552
return $preparedOptionList;
553553
}
554+
555+
/**
556+
* Ensure that an option value is quoted correctly, before it is passed to a child process.
557+
* @param mixed $value the input option value, which is typically a string but can be of any other primitive type.
558+
* @return mixed the replaced and quoted value, if $value contained a character that required quoting.
559+
*/
560+
protected function quoteOptionValue($value) {
561+
562+
if($this->isValueRequiresQuoting($value)) {
563+
return sprintf('"%s"', str_replace('"', '\"', $value));
564+
} else {
565+
return $value;
566+
}
567+
}
568+
569+
/**
570+
* Validate whether a command option requires quoting or not, depending on its content.
571+
*/
572+
protected function isValueRequiresQuoting($value) : bool {
573+
return 0 < preg_match('/[\s \\\\ \' " & | < > = ! @]/x', $value);
574+
}
554575
}

0 commit comments

Comments
 (0)