ArgvInput.php
1 year ago
ArrayInput.php
1 year ago
Input.php
1 year ago
InputArgument.php
2 years ago
InputAwareInterface.php
2 years ago
InputDefinition.php
1 year ago
InputInterface.php
1 year ago
InputOption.php
1 year ago
StreamableInputInterface.php
2 years ago
StringInput.php
2 years ago
Input.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\Console\Input; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\Console\Exception\InvalidArgumentException; |
| 14 | use Matomo\Dependencies\Symfony\Component\Console\Exception\RuntimeException; |
| 15 | /** |
| 16 | * Input is the base class for all concrete Input classes. |
| 17 | * |
| 18 | * Three concrete classes are provided by default: |
| 19 | * |
| 20 | * * `ArgvInput`: The input comes from the CLI arguments (argv) |
| 21 | * * `StringInput`: The input is provided as a string |
| 22 | * * `ArrayInput`: The input is provided as an array |
| 23 | * |
| 24 | * @author Fabien Potencier <fabien@symfony.com> |
| 25 | */ |
| 26 | abstract class Input implements InputInterface, StreamableInputInterface |
| 27 | { |
| 28 | protected $definition; |
| 29 | protected $stream; |
| 30 | protected $options = []; |
| 31 | protected $arguments = []; |
| 32 | protected $interactive = \true; |
| 33 | public function __construct(?InputDefinition $definition = null) |
| 34 | { |
| 35 | if (null === $definition) { |
| 36 | $this->definition = new InputDefinition(); |
| 37 | } else { |
| 38 | $this->bind($definition); |
| 39 | $this->validate(); |
| 40 | } |
| 41 | } |
| 42 | /** |
| 43 | * {@inheritdoc} |
| 44 | */ |
| 45 | public function bind(InputDefinition $definition) |
| 46 | { |
| 47 | $this->arguments = []; |
| 48 | $this->options = []; |
| 49 | $this->definition = $definition; |
| 50 | $this->parse(); |
| 51 | } |
| 52 | /** |
| 53 | * Processes command line arguments. |
| 54 | */ |
| 55 | protected abstract function parse(); |
| 56 | /** |
| 57 | * {@inheritdoc} |
| 58 | */ |
| 59 | public function validate() |
| 60 | { |
| 61 | $definition = $this->definition; |
| 62 | $givenArguments = $this->arguments; |
| 63 | $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use($definition, $givenArguments) { |
| 64 | return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); |
| 65 | }); |
| 66 | if (\count($missingArguments) > 0) { |
| 67 | throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments))); |
| 68 | } |
| 69 | } |
| 70 | /** |
| 71 | * {@inheritdoc} |
| 72 | */ |
| 73 | public function isInteractive() |
| 74 | { |
| 75 | return $this->interactive; |
| 76 | } |
| 77 | /** |
| 78 | * {@inheritdoc} |
| 79 | */ |
| 80 | public function setInteractive(bool $interactive) |
| 81 | { |
| 82 | $this->interactive = $interactive; |
| 83 | } |
| 84 | /** |
| 85 | * {@inheritdoc} |
| 86 | */ |
| 87 | public function getArguments() |
| 88 | { |
| 89 | return array_merge($this->definition->getArgumentDefaults(), $this->arguments); |
| 90 | } |
| 91 | /** |
| 92 | * {@inheritdoc} |
| 93 | */ |
| 94 | public function getArgument(string $name) |
| 95 | { |
| 96 | if (!$this->definition->hasArgument($name)) { |
| 97 | throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
| 98 | } |
| 99 | return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); |
| 100 | } |
| 101 | /** |
| 102 | * {@inheritdoc} |
| 103 | */ |
| 104 | public function setArgument(string $name, $value) |
| 105 | { |
| 106 | if (!$this->definition->hasArgument($name)) { |
| 107 | throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
| 108 | } |
| 109 | $this->arguments[$name] = $value; |
| 110 | } |
| 111 | /** |
| 112 | * {@inheritdoc} |
| 113 | */ |
| 114 | public function hasArgument(string $name) |
| 115 | { |
| 116 | return $this->definition->hasArgument($name); |
| 117 | } |
| 118 | /** |
| 119 | * {@inheritdoc} |
| 120 | */ |
| 121 | public function getOptions() |
| 122 | { |
| 123 | return array_merge($this->definition->getOptionDefaults(), $this->options); |
| 124 | } |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
| 128 | public function getOption(string $name) |
| 129 | { |
| 130 | if ($this->definition->hasNegation($name)) { |
| 131 | if (null === ($value = $this->getOption($this->definition->negationToName($name)))) { |
| 132 | return $value; |
| 133 | } |
| 134 | return !$value; |
| 135 | } |
| 136 | if (!$this->definition->hasOption($name)) { |
| 137 | throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
| 138 | } |
| 139 | return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); |
| 140 | } |
| 141 | /** |
| 142 | * {@inheritdoc} |
| 143 | */ |
| 144 | public function setOption(string $name, $value) |
| 145 | { |
| 146 | if ($this->definition->hasNegation($name)) { |
| 147 | $this->options[$this->definition->negationToName($name)] = !$value; |
| 148 | return; |
| 149 | } elseif (!$this->definition->hasOption($name)) { |
| 150 | throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
| 151 | } |
| 152 | $this->options[$name] = $value; |
| 153 | } |
| 154 | /** |
| 155 | * {@inheritdoc} |
| 156 | */ |
| 157 | public function hasOption(string $name) |
| 158 | { |
| 159 | return $this->definition->hasOption($name) || $this->definition->hasNegation($name); |
| 160 | } |
| 161 | /** |
| 162 | * Escapes a token through escapeshellarg if it contains unsafe chars. |
| 163 | * |
| 164 | * @return string |
| 165 | */ |
| 166 | public function escapeToken(string $token) |
| 167 | { |
| 168 | return preg_match('{^[\\w-]+$}', $token) ? $token : escapeshellarg($token); |
| 169 | } |
| 170 | /** |
| 171 | * {@inheritdoc} |
| 172 | */ |
| 173 | public function setStream($stream) |
| 174 | { |
| 175 | $this->stream = $stream; |
| 176 | } |
| 177 | /** |
| 178 | * {@inheritdoc} |
| 179 | */ |
| 180 | public function getStream() |
| 181 | { |
| 182 | return $this->stream; |
| 183 | } |
| 184 | } |
| 185 |