PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / console / Input / InputDefinition.php
matomo / app / vendor / prefixed / symfony / console / Input Last commit date
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
InputDefinition.php
370 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\LogicException;
15 /**
16 * A InputDefinition represents a set of valid command line arguments and options.
17 *
18 * Usage:
19 *
20 * $definition = new InputDefinition([
21 * new InputArgument('name', InputArgument::REQUIRED),
22 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
23 * ]);
24 *
25 * @author Fabien Potencier <fabien@symfony.com>
26 */
27 class InputDefinition
28 {
29 private $arguments;
30 private $requiredCount;
31 private $lastArrayArgument;
32 private $lastOptionalArgument;
33 private $options;
34 private $negations;
35 private $shortcuts;
36 /**
37 * @param array $definition An array of InputArgument and InputOption instance
38 */
39 public function __construct(array $definition = [])
40 {
41 $this->setDefinition($definition);
42 }
43 /**
44 * Sets the definition of the input.
45 */
46 public function setDefinition(array $definition)
47 {
48 $arguments = [];
49 $options = [];
50 foreach ($definition as $item) {
51 if ($item instanceof InputOption) {
52 $options[] = $item;
53 } else {
54 $arguments[] = $item;
55 }
56 }
57 $this->setArguments($arguments);
58 $this->setOptions($options);
59 }
60 /**
61 * Sets the InputArgument objects.
62 *
63 * @param InputArgument[] $arguments An array of InputArgument objects
64 */
65 public function setArguments(array $arguments = [])
66 {
67 $this->arguments = [];
68 $this->requiredCount = 0;
69 $this->lastOptionalArgument = null;
70 $this->lastArrayArgument = null;
71 $this->addArguments($arguments);
72 }
73 /**
74 * Adds an array of InputArgument objects.
75 *
76 * @param InputArgument[] $arguments An array of InputArgument objects
77 */
78 public function addArguments(?array $arguments = [])
79 {
80 if (null !== $arguments) {
81 foreach ($arguments as $argument) {
82 $this->addArgument($argument);
83 }
84 }
85 }
86 /**
87 * @throws LogicException When incorrect argument is given
88 */
89 public function addArgument(InputArgument $argument)
90 {
91 if (isset($this->arguments[$argument->getName()])) {
92 throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
93 }
94 if (null !== $this->lastArrayArgument) {
95 throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName()));
96 }
97 if ($argument->isRequired() && null !== $this->lastOptionalArgument) {
98 throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName()));
99 }
100 if ($argument->isArray()) {
101 $this->lastArrayArgument = $argument;
102 }
103 if ($argument->isRequired()) {
104 ++$this->requiredCount;
105 } else {
106 $this->lastOptionalArgument = $argument;
107 }
108 $this->arguments[$argument->getName()] = $argument;
109 }
110 /**
111 * Returns an InputArgument by name or by position.
112 *
113 * @param string|int $name The InputArgument name or position
114 *
115 * @return InputArgument
116 *
117 * @throws InvalidArgumentException When argument given doesn't exist
118 */
119 public function getArgument($name)
120 {
121 if (!$this->hasArgument($name)) {
122 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
123 }
124 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
125 return $arguments[$name];
126 }
127 /**
128 * Returns true if an InputArgument object exists by name or position.
129 *
130 * @param string|int $name The InputArgument name or position
131 *
132 * @return bool
133 */
134 public function hasArgument($name)
135 {
136 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
137 return isset($arguments[$name]);
138 }
139 /**
140 * Gets the array of InputArgument objects.
141 *
142 * @return InputArgument[]
143 */
144 public function getArguments()
145 {
146 return $this->arguments;
147 }
148 /**
149 * Returns the number of InputArguments.
150 *
151 * @return int
152 */
153 public function getArgumentCount()
154 {
155 return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
156 }
157 /**
158 * Returns the number of required InputArguments.
159 *
160 * @return int
161 */
162 public function getArgumentRequiredCount()
163 {
164 return $this->requiredCount;
165 }
166 /**
167 * @return array<string|bool|int|float|array|null>
168 */
169 public function getArgumentDefaults()
170 {
171 $values = [];
172 foreach ($this->arguments as $argument) {
173 $values[$argument->getName()] = $argument->getDefault();
174 }
175 return $values;
176 }
177 /**
178 * Sets the InputOption objects.
179 *
180 * @param InputOption[] $options An array of InputOption objects
181 */
182 public function setOptions(array $options = [])
183 {
184 $this->options = [];
185 $this->shortcuts = [];
186 $this->negations = [];
187 $this->addOptions($options);
188 }
189 /**
190 * Adds an array of InputOption objects.
191 *
192 * @param InputOption[] $options An array of InputOption objects
193 */
194 public function addOptions(array $options = [])
195 {
196 foreach ($options as $option) {
197 $this->addOption($option);
198 }
199 }
200 /**
201 * @throws LogicException When option given already exist
202 */
203 public function addOption(InputOption $option)
204 {
205 if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
206 throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
207 }
208 if (isset($this->negations[$option->getName()])) {
209 throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
210 }
211 if ($option->getShortcut()) {
212 foreach (explode('|', $option->getShortcut()) as $shortcut) {
213 if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
214 throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
215 }
216 }
217 }
218 $this->options[$option->getName()] = $option;
219 if ($option->getShortcut()) {
220 foreach (explode('|', $option->getShortcut()) as $shortcut) {
221 $this->shortcuts[$shortcut] = $option->getName();
222 }
223 }
224 if ($option->isNegatable()) {
225 $negatedName = 'no-' . $option->getName();
226 if (isset($this->options[$negatedName])) {
227 throw new LogicException(sprintf('An option named "%s" already exists.', $negatedName));
228 }
229 $this->negations[$negatedName] = $option->getName();
230 }
231 }
232 /**
233 * Returns an InputOption by name.
234 *
235 * @return InputOption
236 *
237 * @throws InvalidArgumentException When option given doesn't exist
238 */
239 public function getOption(string $name)
240 {
241 if (!$this->hasOption($name)) {
242 throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
243 }
244 return $this->options[$name];
245 }
246 /**
247 * Returns true if an InputOption object exists by name.
248 *
249 * This method can't be used to check if the user included the option when
250 * executing the command (use getOption() instead).
251 *
252 * @return bool
253 */
254 public function hasOption(string $name)
255 {
256 return isset($this->options[$name]);
257 }
258 /**
259 * Gets the array of InputOption objects.
260 *
261 * @return InputOption[]
262 */
263 public function getOptions()
264 {
265 return $this->options;
266 }
267 /**
268 * Returns true if an InputOption object exists by shortcut.
269 *
270 * @return bool
271 */
272 public function hasShortcut(string $name)
273 {
274 return isset($this->shortcuts[$name]);
275 }
276 /**
277 * Returns true if an InputOption object exists by negated name.
278 */
279 public function hasNegation(string $name) : bool
280 {
281 return isset($this->negations[$name]);
282 }
283 /**
284 * Gets an InputOption by shortcut.
285 *
286 * @return InputOption
287 */
288 public function getOptionForShortcut(string $shortcut)
289 {
290 return $this->getOption($this->shortcutToName($shortcut));
291 }
292 /**
293 * @return array<string|bool|int|float|array|null>
294 */
295 public function getOptionDefaults()
296 {
297 $values = [];
298 foreach ($this->options as $option) {
299 $values[$option->getName()] = $option->getDefault();
300 }
301 return $values;
302 }
303 /**
304 * Returns the InputOption name given a shortcut.
305 *
306 * @throws InvalidArgumentException When option given does not exist
307 *
308 * @internal
309 */
310 public function shortcutToName(string $shortcut) : string
311 {
312 if (!isset($this->shortcuts[$shortcut])) {
313 throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
314 }
315 return $this->shortcuts[$shortcut];
316 }
317 /**
318 * Returns the InputOption name given a negation.
319 *
320 * @throws InvalidArgumentException When option given does not exist
321 *
322 * @internal
323 */
324 public function negationToName(string $negation) : string
325 {
326 if (!isset($this->negations[$negation])) {
327 throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation));
328 }
329 return $this->negations[$negation];
330 }
331 /**
332 * Gets the synopsis.
333 *
334 * @return string
335 */
336 public function getSynopsis(bool $short = \false)
337 {
338 $elements = [];
339 if ($short && $this->getOptions()) {
340 $elements[] = '[options]';
341 } elseif (!$short) {
342 foreach ($this->getOptions() as $option) {
343 $value = '';
344 if ($option->acceptValue()) {
345 $value = sprintf(' %s%s%s', $option->isValueOptional() ? '[' : '', strtoupper($option->getName()), $option->isValueOptional() ? ']' : '');
346 }
347 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
348 $negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : '';
349 $elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation);
350 }
351 }
352 if (\count($elements) && $this->getArguments()) {
353 $elements[] = '[--]';
354 }
355 $tail = '';
356 foreach ($this->getArguments() as $argument) {
357 $element = '<' . $argument->getName() . '>';
358 if ($argument->isArray()) {
359 $element .= '...';
360 }
361 if (!$argument->isRequired()) {
362 $element = '[' . $element;
363 $tail .= ']';
364 }
365 $elements[] = $element;
366 }
367 return implode(' ', $elements) . $tail;
368 }
369 }
370