summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Input/Input.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/console/Input/Input.php')
-rw-r--r--vendor/symfony/console/Input/Input.php174
1 files changed, 174 insertions, 0 deletions
diff --git a/vendor/symfony/console/Input/Input.php b/vendor/symfony/console/Input/Input.php
new file mode 100644
index 0000000..5a8b9a2
--- /dev/null
+++ b/vendor/symfony/console/Input/Input.php
@@ -0,0 +1,174 @@
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
12namespace Symfony\Component\Console\Input;
13
14use Symfony\Component\Console\Exception\InvalidArgumentException;
15use Symfony\Component\Console\Exception\RuntimeException;
16
17/**
18 * Input is the base class for all concrete Input classes.
19 *
20 * Three concrete classes are provided by default:
21 *
22 * * `ArgvInput`: The input comes from the CLI arguments (argv)
23 * * `StringInput`: The input is provided as a string
24 * * `ArrayInput`: The input is provided as an array
25 *
26 * @author Fabien Potencier <fabien@symfony.com>
27 */
28abstract class Input implements InputInterface, StreamableInputInterface
29{
30 protected InputDefinition $definition;
31 /** @var resource */
32 protected $stream;
33 protected array $options = [];
34 protected array $arguments = [];
35 protected bool $interactive = true;
36
37 public function __construct(?InputDefinition $definition = null)
38 {
39 if (null === $definition) {
40 $this->definition = new InputDefinition();
41 } else {
42 $this->bind($definition);
43 $this->validate();
44 }
45 }
46
47 public function bind(InputDefinition $definition): void
48 {
49 $this->arguments = [];
50 $this->options = [];
51 $this->definition = $definition;
52
53 $this->parse();
54 }
55
56 /**
57 * Processes command line arguments.
58 */
59 abstract protected function parse(): void;
60
61 public function validate(): void
62 {
63 $definition = $this->definition;
64 $givenArguments = $this->arguments;
65
66 $missingArguments = array_filter(array_keys($definition->getArguments()), fn ($argument) => !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired());
67
68 if (\count($missingArguments) > 0) {
69 throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
70 }
71 }
72
73 public function isInteractive(): bool
74 {
75 return $this->interactive;
76 }
77
78 public function setInteractive(bool $interactive): void
79 {
80 $this->interactive = $interactive;
81 }
82
83 public function getArguments(): array
84 {
85 return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
86 }
87
88 public function getArgument(string $name): mixed
89 {
90 if (!$this->definition->hasArgument($name)) {
91 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
92 }
93
94 return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();
95 }
96
97 public function setArgument(string $name, mixed $value): void
98 {
99 if (!$this->definition->hasArgument($name)) {
100 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
101 }
102
103 $this->arguments[$name] = $value;
104 }
105
106 public function hasArgument(string $name): bool
107 {
108 return $this->definition->hasArgument($name);
109 }
110
111 public function getOptions(): array
112 {
113 return array_merge($this->definition->getOptionDefaults(), $this->options);
114 }
115
116 public function getOption(string $name): mixed
117 {
118 if ($this->definition->hasNegation($name)) {
119 if (null === $value = $this->getOption($this->definition->negationToName($name))) {
120 return $value;
121 }
122
123 return !$value;
124 }
125
126 if (!$this->definition->hasOption($name)) {
127 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
128 }
129
130 return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
131 }
132
133 public function setOption(string $name, mixed $value): void
134 {
135 if ($this->definition->hasNegation($name)) {
136 $this->options[$this->definition->negationToName($name)] = !$value;
137
138 return;
139 } elseif (!$this->definition->hasOption($name)) {
140 throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
141 }
142
143 $this->options[$name] = $value;
144 }
145
146 public function hasOption(string $name): bool
147 {
148 return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
149 }
150
151 /**
152 * Escapes a token through escapeshellarg if it contains unsafe chars.
153 */
154 public function escapeToken(string $token): string
155 {
156 return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
157 }
158
159 /**
160 * @param resource $stream
161 */
162 public function setStream($stream): void
163 {
164 $this->stream = $stream;
165 }
166
167 /**
168 * @return resource
169 */
170 public function getStream()
171 {
172 return $this->stream;
173 }
174}