2 namespace Drush\Preflight;
5 * Map commandline arguments from one value to anohter during preflight.
9 protected $remapOptions;
10 protected $remapCommandAliases;
13 * ArgsRemapper constructor
15 public function __construct($remapOptions, $remapCommandAliases)
17 $this->remapOptions = $remapOptions;
18 $this->remapCommandAliases = $remapCommandAliases;
22 * Given an $argv array, apply all remap operations on each item
25 * @param string[] $argv
27 public function remap($argv)
31 foreach ($argv as $arg) {
32 $arg = $this->checkRemap($arg, $sawCommand);
41 * Check to see if the provided single arg needs to be remapped. If
42 * it does, then the remapping is performed.
44 * @param string $arg One argument to inspect
45 * @param string $sawCommand True if drush command was found
46 * @return string The altered argument
48 protected function checkRemap($arg, &$sawCommand)
50 if (!$sawCommand && ctype_alpha($arg[0])) {
52 return $this->remapCommandAlias($arg);
54 return $this->remapOptions($arg);
57 protected function remapOptions($arg)
59 foreach ($this->remapOptions as $from => $to) {
60 if ($this->matches($arg, $from)) {
61 return $to . substr($arg, strlen($from));
67 protected function remapCommandAlias($arg)
69 foreach ($this->remapCommandAliases as $from => $to) {
78 * Check to see if the provided single arg matches the candidate.
79 * If the candidate is `--foo`, then we will match the exact string
80 * `--foo`, or the leading substring `--foo=`, and nothing else.
82 * @param string $candidate
85 protected function matches($arg, $candidate)
87 if (strpos($arg, $candidate) !== 0) {
91 if (strlen($arg) == strlen($candidate)) {
95 return $arg[strlen($candidate)] == '=';