Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / symfony / console / Input / Input.php
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     /**
31      * @var InputDefinition
32      */
33     protected $definition;
34     protected $stream;
35     protected $options = array();
36     protected $arguments = array();
37     protected $interactive = true;
38
39     /**
40      * Constructor.
41      *
42      * @param InputDefinition|null $definition A InputDefinition instance
43      */
44     public function __construct(InputDefinition $definition = null)
45     {
46         if (null === $definition) {
47             $this->definition = new InputDefinition();
48         } else {
49             $this->bind($definition);
50             $this->validate();
51         }
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     public function bind(InputDefinition $definition)
58     {
59         $this->arguments = array();
60         $this->options = array();
61         $this->definition = $definition;
62
63         $this->parse();
64     }
65
66     /**
67      * Processes command line arguments.
68      */
69     abstract protected function parse();
70
71     /**
72      * {@inheritdoc}
73      */
74     public function validate()
75     {
76         $definition = $this->definition;
77         $givenArguments = $this->arguments;
78
79         $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
80             return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
81         });
82
83         if (count($missingArguments) > 0) {
84             throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
85         }
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function isInteractive()
92     {
93         return $this->interactive;
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     public function setInteractive($interactive)
100     {
101         $this->interactive = (bool) $interactive;
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     public function getArguments()
108     {
109         return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
110     }
111
112     /**
113      * {@inheritdoc}
114      */
115     public function getArgument($name)
116     {
117         if (!$this->definition->hasArgument($name)) {
118             throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
119         }
120
121         return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
122     }
123
124     /**
125      * {@inheritdoc}
126      */
127     public function setArgument($name, $value)
128     {
129         if (!$this->definition->hasArgument($name)) {
130             throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
131         }
132
133         $this->arguments[$name] = $value;
134     }
135
136     /**
137      * {@inheritdoc}
138      */
139     public function hasArgument($name)
140     {
141         return $this->definition->hasArgument($name);
142     }
143
144     /**
145      * {@inheritdoc}
146      */
147     public function getOptions()
148     {
149         return array_merge($this->definition->getOptionDefaults(), $this->options);
150     }
151
152     /**
153      * {@inheritdoc}
154      */
155     public function getOption($name)
156     {
157         if (!$this->definition->hasOption($name)) {
158             throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
159         }
160
161         return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
162     }
163
164     /**
165      * {@inheritdoc}
166      */
167     public function setOption($name, $value)
168     {
169         if (!$this->definition->hasOption($name)) {
170             throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
171         }
172
173         $this->options[$name] = $value;
174     }
175
176     /**
177      * {@inheritdoc}
178      */
179     public function hasOption($name)
180     {
181         return $this->definition->hasOption($name);
182     }
183
184     /**
185      * Escapes a token through escapeshellarg if it contains unsafe chars.
186      *
187      * @param string $token
188      *
189      * @return string
190      */
191     public function escapeToken($token)
192     {
193         return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
194     }
195
196     /**
197      * {@inheritdoc}
198      */
199     public function setStream($stream)
200     {
201         $this->stream = $stream;
202     }
203
204     /**
205      * {@inheritdoc}
206      */
207     public function getStream()
208     {
209         return $this->stream;
210     }
211 }