ArgvInput.php
2 years ago
ArrayInput.php
2 years ago
Input.php
2 years ago
InputArgument.php
2 years ago
InputAwareInterface.php
2 years ago
InputDefinition.php
2 years ago
InputInterface.php
2 years ago
InputOption.php
2 years ago
StreamableInputInterface.php
2 years ago
StringInput.php
2 years ago
Input.php
186 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 IAWP_SCOPED\Symfony\Component\Console\Input; |
| 12 | |
| 13 | use IAWP_SCOPED\Symfony\Component\Console\Exception\InvalidArgumentException; |
| 14 | use IAWP_SCOPED\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 | * @internal |
| 26 | */ |
| 27 | abstract class Input implements InputInterface, StreamableInputInterface |
| 28 | { |
| 29 | protected $definition; |
| 30 | protected $stream; |
| 31 | protected $options = []; |
| 32 | protected $arguments = []; |
| 33 | protected $interactive = \true; |
| 34 | public function __construct(InputDefinition $definition = null) |
| 35 | { |
| 36 | if (null === $definition) { |
| 37 | $this->definition = new InputDefinition(); |
| 38 | } else { |
| 39 | $this->bind($definition); |
| 40 | $this->validate(); |
| 41 | } |
| 42 | } |
| 43 | /** |
| 44 | * {@inheritdoc} |
| 45 | */ |
| 46 | public function bind(InputDefinition $definition) |
| 47 | { |
| 48 | $this->arguments = []; |
| 49 | $this->options = []; |
| 50 | $this->definition = $definition; |
| 51 | $this->parse(); |
| 52 | } |
| 53 | /** |
| 54 | * Processes command line arguments. |
| 55 | */ |
| 56 | protected abstract function parse(); |
| 57 | /** |
| 58 | * {@inheritdoc} |
| 59 | */ |
| 60 | public function validate() |
| 61 | { |
| 62 | $definition = $this->definition; |
| 63 | $givenArguments = $this->arguments; |
| 64 | $missingArguments = \array_filter(\array_keys($definition->getArguments()), function ($argument) use($definition, $givenArguments) { |
| 65 | return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); |
| 66 | }); |
| 67 | if (\count($missingArguments) > 0) { |
| 68 | throw new RuntimeException(\sprintf('Not enough arguments (missing: "%s").', \implode(', ', $missingArguments))); |
| 69 | } |
| 70 | } |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function isInteractive() |
| 75 | { |
| 76 | return $this->interactive; |
| 77 | } |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function setInteractive(bool $interactive) |
| 82 | { |
| 83 | $this->interactive = $interactive; |
| 84 | } |
| 85 | /** |
| 86 | * {@inheritdoc} |
| 87 | */ |
| 88 | public function getArguments() |
| 89 | { |
| 90 | return \array_merge($this->definition->getArgumentDefaults(), $this->arguments); |
| 91 | } |
| 92 | /** |
| 93 | * {@inheritdoc} |
| 94 | */ |
| 95 | public function getArgument(string $name) |
| 96 | { |
| 97 | if (!$this->definition->hasArgument($name)) { |
| 98 | throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name)); |
| 99 | } |
| 100 | return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); |
| 101 | } |
| 102 | /** |
| 103 | * {@inheritdoc} |
| 104 | */ |
| 105 | public function setArgument(string $name, $value) |
| 106 | { |
| 107 | if (!$this->definition->hasArgument($name)) { |
| 108 | throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name)); |
| 109 | } |
| 110 | $this->arguments[$name] = $value; |
| 111 | } |
| 112 | /** |
| 113 | * {@inheritdoc} |
| 114 | */ |
| 115 | public function hasArgument(string $name) |
| 116 | { |
| 117 | return $this->definition->hasArgument($name); |
| 118 | } |
| 119 | /** |
| 120 | * {@inheritdoc} |
| 121 | */ |
| 122 | public function getOptions() |
| 123 | { |
| 124 | return \array_merge($this->definition->getOptionDefaults(), $this->options); |
| 125 | } |
| 126 | /** |
| 127 | * {@inheritdoc} |
| 128 | */ |
| 129 | public function getOption(string $name) |
| 130 | { |
| 131 | if ($this->definition->hasNegation($name)) { |
| 132 | if (null === ($value = $this->getOption($this->definition->negationToName($name)))) { |
| 133 | return $value; |
| 134 | } |
| 135 | return !$value; |
| 136 | } |
| 137 | if (!$this->definition->hasOption($name)) { |
| 138 | throw new InvalidArgumentException(\sprintf('The "%s" option does not exist.', $name)); |
| 139 | } |
| 140 | return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); |
| 141 | } |
| 142 | /** |
| 143 | * {@inheritdoc} |
| 144 | */ |
| 145 | public function setOption(string $name, $value) |
| 146 | { |
| 147 | if ($this->definition->hasNegation($name)) { |
| 148 | $this->options[$this->definition->negationToName($name)] = !$value; |
| 149 | return; |
| 150 | } elseif (!$this->definition->hasOption($name)) { |
| 151 | throw new InvalidArgumentException(\sprintf('The "%s" option does not exist.', $name)); |
| 152 | } |
| 153 | $this->options[$name] = $value; |
| 154 | } |
| 155 | /** |
| 156 | * {@inheritdoc} |
| 157 | */ |
| 158 | public function hasOption(string $name) |
| 159 | { |
| 160 | return $this->definition->hasOption($name) || $this->definition->hasNegation($name); |
| 161 | } |
| 162 | /** |
| 163 | * Escapes a token through escapeshellarg if it contains unsafe chars. |
| 164 | * |
| 165 | * @return string |
| 166 | */ |
| 167 | public function escapeToken(string $token) |
| 168 | { |
| 169 | return \preg_match('{^[\\w-]+$}', $token) ? $token : \escapeshellarg($token); |
| 170 | } |
| 171 | /** |
| 172 | * {@inheritdoc} |
| 173 | */ |
| 174 | public function setStream($stream) |
| 175 | { |
| 176 | $this->stream = $stream; |
| 177 | } |
| 178 | /** |
| 179 | * {@inheritdoc} |
| 180 | */ |
| 181 | public function getStream() |
| 182 | { |
| 183 | return $this->stream; |
| 184 | } |
| 185 | } |
| 186 |