Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / src / Preflight / ArgsRemapper.php
diff --git a/vendor/drush/drush/src/Preflight/ArgsRemapper.php b/vendor/drush/drush/src/Preflight/ArgsRemapper.php
new file mode 100644 (file)
index 0000000..a408408
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+namespace Drush\Preflight;
+
+/**
+ * Map commandline arguments from one value to anohter during preflight.
+ */
+class ArgsRemapper
+{
+    protected $remapOptions;
+    protected $remapCommandAliases;
+
+    /**
+     * ArgsRemapper constructor
+     */
+    public function __construct($remapOptions, $remapCommandAliases)
+    {
+        $this->remapOptions = $remapOptions;
+        $this->remapCommandAliases = $remapCommandAliases;
+    }
+
+    /**
+     * Given an $argv array, apply all remap operations on each item
+     * within it.
+     *
+     * @param string[] $argv
+     */
+    public function remap($argv)
+    {
+        $result = [];
+        $sawCommand = false;
+        foreach ($argv as $arg) {
+            $arg = $this->checkRemap($arg, $sawCommand);
+            if (isset($arg)) {
+                $result[] = $arg;
+            }
+        }
+        return $result;
+    }
+
+    /**
+     * Check to see if the provided single arg needs to be remapped. If
+     * it does, then the remapping is performed.
+     *
+     * @param string $arg One argument to inspect
+     * @param string $sawCommand True if drush command was found
+     * @return string The altered argument
+     */
+    protected function checkRemap($arg, &$sawCommand)
+    {
+        if (!$sawCommand && ctype_alpha($arg[0])) {
+            $sawCommand = true;
+            return $this->remapCommandAlias($arg);
+        }
+        return $this->remapOptions($arg);
+    }
+
+    protected function remapOptions($arg)
+    {
+        foreach ($this->remapOptions as $from => $to) {
+            if ($this->matches($arg, $from)) {
+                return $to . substr($arg, strlen($from));
+            }
+        }
+        return $arg;
+    }
+
+    protected function remapCommandAlias($arg)
+    {
+        foreach ($this->remapCommandAliases as $from => $to) {
+            if ($arg == $from) {
+                return $to;
+            }
+        }
+        return $arg;
+    }
+
+    /**
+     * Check to see if the provided single arg matches the candidate.
+     * If the candidate is `--foo`, then we will match the exact string
+     * `--foo`, or the leading substring `--foo=`, and nothing else.
+     * @param string $arg
+     * @param string $candidate
+     * @return bool
+     */
+    protected function matches($arg, $candidate)
+    {
+        if (strpos($arg, $candidate) !== 0) {
+            return false;
+        }
+
+        if (strlen($arg) == strlen($candidate)) {
+            return true;
+        }
+
+        return $arg[strlen($candidate)] == '=';
+    }
+}