comparison vendor/symfony/console/Input/Input.php @ 0:c75dbcec494b

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