Advertisement
Guest User

AcceptAllArgvInput.php

a guest
Mar 21st, 2014
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.12 KB | None | 0 0
  1. use Symfony\Component\Console\Input\ArgvInput;
  2. use Symfony\Component\Console\Input\InputDefinition;
  3. use Symfony\Component\Console\Input\InputOption;
  4.  
  5. class AcceptAllArgvInput extends ArgvInput
  6. {
  7.  
  8.     private $tokens;
  9.     private $parsed;
  10.  
  11.     /**
  12.      * Constructor.
  13.      *
  14.      * @param array           $argv       An array of parameters from the CLI (in the argv format)
  15.      * @param InputDefinition $definition A InputDefinition instance
  16.      *
  17.      * @api
  18.      */
  19.     public function __construct(array $argv = null, InputDefinition $definition = null)
  20.     {
  21.         if (null === $argv) {
  22.             $argv = $_SERVER['argv'];
  23.         }
  24.  
  25.         // strip the application name
  26.         array_shift($argv);
  27.  
  28.         $this->tokens = $argv;
  29.  
  30.         parent::__construct($definition);
  31.     }
  32.  
  33.     protected function setTokens(array $tokens)
  34.     {
  35.         $this->tokens = $tokens;
  36.     }
  37.  
  38.     /**
  39.      * Processes command line arguments.
  40.      */
  41.     protected function parse()
  42.     {
  43.         $parseOptions = true;
  44.         $this->parsed = $this->tokens;
  45.         while (null !== $token = array_shift($this->parsed)) {
  46.             if ($parseOptions && '' == $token) {
  47.                 $this->parseArgument($token);
  48.             } elseif ($parseOptions && '--' == $token) {
  49.                 $parseOptions = false;
  50.             } elseif ($parseOptions && 0 === strpos($token, '--')) {
  51.                 $this->parseLongOption($token);
  52.             } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
  53.                 $this->parseShortOption($token);
  54.             } else {
  55.                 $this->parseArgument($token);
  56.             }
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * Parses a short option.
  62.      *
  63.      * @param string $token The current token.
  64.      */
  65.     private function parseShortOption($token)
  66.     {
  67.         $name = substr($token, 1);
  68.  
  69.         if (strlen($name) > 1) {
  70.             if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
  71.                 // an option with a value (with no space)
  72.                 $this->addShortOption($name[0], substr($name, 1));
  73.             } else {
  74.                 $this->parseShortOptionSet($name);
  75.             }
  76.         } else {
  77.             $this->addShortOption($name, null);
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Parses a short option set.
  83.      *
  84.      * @param string $name The current token
  85.      *
  86.      * @throws \RuntimeException When option given doesn't exist
  87.      */
  88.     private function parseShortOptionSet($name)
  89.     {
  90.         $len = strlen($name);
  91.         for ($i = 0; $i < $len; $i++) {
  92.             if (!$this->definition->hasShortcut($name[$i])) {
  93.                 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  94.             }
  95.  
  96.             $option = $this->definition->getOptionForShortcut($name[$i]);
  97.             if ($option->acceptValue()) {
  98.                 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  99.  
  100.                 break;
  101.             } else {
  102.                 $this->addLongOption($option->getName(), null);
  103.             }
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Parses a long option.
  109.      *
  110.      * @param string $token The current token
  111.      */
  112.     private function parseLongOption($token)
  113.     {
  114.         $name = substr($token, 2);
  115.  
  116.         if (false !== $pos = strpos($name, '=')) {
  117.             $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  118.         } else {
  119.             $this->addLongOption($name, null);
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Parses an argument.
  125.      *
  126.      * @param string $token The current token
  127.      *
  128.      * @throws \RuntimeException When too many arguments are given
  129.      */
  130.     private function parseArgument($token)
  131.     {
  132.         $c = count($this->arguments);
  133.  
  134.         // if input is expecting another argument, add it
  135.         if ($this->definition->hasArgument($c)) {
  136.             $arg = $this->definition->getArgument($c);
  137.             $this->arguments[$arg->getName()] = $arg->isArray()? array($token) : $token;
  138.  
  139.             // if last argument isArray(), append token to last argument
  140.         } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
  141.             $arg = $this->definition->getArgument($c - 1);
  142.             $this->arguments[$arg->getName()][] = $token;
  143.  
  144.             // unexpected argument
  145.         } else {
  146.             throw new \RuntimeException('Too many arguments.');
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * Adds a short option value.
  152.      *
  153.      * @param string $shortcut The short option key
  154.      * @param mixed  $value    The value for the option
  155.      *
  156.      * @throws \RuntimeException When option given doesn't exist
  157.      */
  158.     private function addShortOption($shortcut, $value)
  159.     {
  160.         if (!$this->definition->hasShortcut($shortcut)) {
  161.             throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  162.         }
  163.  
  164.         $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  165.     }
  166.  
  167.     /**
  168.      * Adds a long option value.
  169.      *
  170.      * @param string $name  The long option key
  171.      * @param mixed  $value The value for the option
  172.      *
  173.      * @throws \RuntimeException When option given doesn't exist
  174.      */
  175.     private function addLongOption($name, $value)
  176.     {
  177.         if (!$this->definition->hasOption($name)) {
  178.             $this->definition->addOption(new InputOption($name, null));
  179.         }
  180.  
  181.         $option = $this->definition->getOption($name);
  182.  
  183.         // Convert false values (from a previous call to substr()) to null
  184.         if (false === $value) {
  185.             $value = null;
  186.         }
  187.  
  188.         /*if (null !== $value && !$option->acceptValue()) {
  189.             throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name, $value));
  190.         }*/
  191.  
  192.         if (null === $value && $option->acceptValue() && count($this->parsed)) {
  193.             // if option accepts an optional or mandatory argument
  194.             // let's see if there is one provided
  195.             $next = array_shift($this->parsed);
  196.             if (isset($next[0]) && '-' !== $next[0]) {
  197.                 $value = $next;
  198.             } elseif (empty($next)) {
  199.                 $value = '';
  200.             } else {
  201.                 array_unshift($this->parsed, $next);
  202.             }
  203.         }
  204.  
  205.         if (null === $value) {
  206.             if ($option->isValueRequired()) {
  207.                 throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  208.             }
  209.  
  210.             if (!$option->isArray()) {
  211.                 $value = $option->isValueOptional() ? $option->getDefault() : true;
  212.             }
  213.         }
  214.  
  215.         if ($option->isArray()) {
  216.             $this->options[$name][] = $value;
  217.         } else {
  218.             $this->options[$name] = $value;
  219.         }
  220.     }
  221.  
  222.     /**
  223.      * Returns the first argument from the raw parameters (not parsed).
  224.      *
  225.      * @return string The value of the first argument or null otherwise
  226.      */
  227.     public function getFirstArgument()
  228.     {
  229.         foreach ($this->tokens as $token) {
  230.             if ($token && '-' === $token[0]) {
  231.                 continue;
  232.             }
  233.  
  234.             return $token;
  235.         }
  236.     }
  237.  
  238.     /**
  239.      * Returns true if the raw parameters (not parsed) contain a value.
  240.      *
  241.      * This method is to be used to introspect the input parameters
  242.      * before they have been validated. It must be used carefully.
  243.      *
  244.      * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  245.      *
  246.      * @return Boolean true if the value is contained in the raw parameters
  247.      */
  248.     public function hasParameterOption($values)
  249.     {
  250.         $values = (array) $values;
  251.  
  252.         foreach ($this->tokens as $token) {
  253.             foreach ($values as $value) {
  254.                 if ($token === $value || 0 === strpos($token, $value.'=')) {
  255.                     return true;
  256.                 }
  257.             }
  258.         }
  259.  
  260.         return false;
  261.     }
  262.  
  263.     /**
  264.      * Returns the value of a raw option (not parsed).
  265.      *
  266.      * This method is to be used to introspect the input parameters
  267.      * before they have been validated. It must be used carefully.
  268.      *
  269.      * @param string|array $values  The value(s) to look for in the raw parameters (can be an array)
  270.      * @param mixed        $default The default value to return if no result is found
  271.      *
  272.      * @return mixed The option value
  273.      */
  274.     public function getParameterOption($values, $default = false)
  275.     {
  276.         $values = (array) $values;
  277.  
  278.         $tokens = $this->tokens;
  279.         while ($token = array_shift($tokens)) {
  280.             foreach ($values as $value) {
  281.                 if ($token === $value || 0 === strpos($token, $value.'=')) {
  282.                     if (false !== $pos = strpos($token, '=')) {
  283.                         return substr($token, $pos + 1);
  284.                     }
  285.  
  286.                     return array_shift($tokens);
  287.                 }
  288.             }
  289.         }
  290.  
  291.         return $default;
  292.     }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement