TYPO3  7.6
Input.php
Go to the documentation of this file.
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 
25 abstract class Input implements InputInterface
26 {
30  protected $definition;
31  protected $options = array();
32  protected $arguments = array();
33  protected $interactive = true;
34 
40  public function __construct(InputDefinition $definition = null)
41  {
42  if (null === $definition) {
43  $this->definition = new InputDefinition();
44  } else {
45  $this->bind($definition);
46  $this->validate();
47  }
48  }
49 
55  public function bind(InputDefinition $definition)
56  {
57  $this->arguments = array();
58  $this->options = array();
59  $this->definition = $definition;
60 
61  $this->parse();
62  }
63 
67  abstract protected function parse();
68 
74  public function validate()
75  {
76  if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
77  throw new \RuntimeException('Not enough arguments.');
78  }
79  }
80 
86  public function isInteractive()
87  {
88  return $this->interactive;
89  }
90 
96  public function setInteractive($interactive)
97  {
98  $this->interactive = (bool) $interactive;
99  }
100 
106  public function getArguments()
107  {
108  return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
109  }
110 
120  public function getArgument($name)
121  {
122  if (!$this->definition->hasArgument($name)) {
123  throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
124  }
125 
126  return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
127  }
128 
137  public function setArgument($name, $value)
138  {
139  if (!$this->definition->hasArgument($name)) {
140  throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
141  }
142 
143  $this->arguments[$name] = $value;
144  }
145 
153  public function hasArgument($name)
154  {
155  return $this->definition->hasArgument($name);
156  }
157 
163  public function getOptions()
164  {
165  return array_merge($this->definition->getOptionDefaults(), $this->options);
166  }
167 
177  public function getOption($name)
178  {
179  if (!$this->definition->hasOption($name)) {
180  throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
181  }
182 
183  return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
184  }
185 
194  public function setOption($name, $value)
195  {
196  if (!$this->definition->hasOption($name)) {
197  throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
198  }
199 
200  $this->options[$name] = $value;
201  }
202 
210  public function hasOption($name)
211  {
212  return $this->definition->hasOption($name);
213  }
214 
222  public function escapeToken($token)
223  {
224  return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
225  }
226 }