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: /**
15: * Input is the base class for all concrete Input classes.
16: *
17: * Three concrete classes are provided by default:
18: *
19: * * `ArgvInput`: The input comes from the CLI arguments (argv)
20: * * `StringInput`: The input is provided as a string
21: * * `ArrayInput`: The input is provided as an array
22: *
23: * @author Fabien Potencier <fabien@symfony.com>
24: */
25: abstract class Input implements InputInterface
26: {
27: protected $definition;
28: protected $options;
29: protected $arguments;
30: protected $interactive = true;
31:
32: /**
33: * Constructor.
34: *
35: * @param InputDefinition $definition A InputDefinition instance
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: /**
48: * Binds the current Input instance with the given arguments and options.
49: *
50: * @param InputDefinition $definition A InputDefinition instance
51: */
52: public function bind(InputDefinition $definition)
53: {
54: $this->arguments = array();
55: $this->options = array();
56: $this->definition = $definition;
57:
58: $this->parse();
59: }
60:
61: /**
62: * Processes command line arguments.
63: */
64: abstract protected function parse();
65:
66: /**
67: * Validates the input.
68: *
69: * @throws \RuntimeException When not enough arguments are given
70: */
71: public function validate()
72: {
73: if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
74: throw new \RuntimeException('Not enough arguments.');
75: }
76: }
77:
78: /**
79: * Checks if the input is interactive.
80: *
81: * @return Boolean Returns true if the input is interactive
82: */
83: public function isInteractive()
84: {
85: return $this->interactive;
86: }
87:
88: /**
89: * Sets the input interactivity.
90: *
91: * @param Boolean $interactive If the input should be interactive
92: */
93: public function setInteractive($interactive)
94: {
95: $this->interactive = (Boolean) $interactive;
96: }
97:
98: /**
99: * Returns the argument values.
100: *
101: * @return array An array of argument values
102: */
103: public function getArguments()
104: {
105: return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
106: }
107:
108: /**
109: * Returns the argument value for a given argument name.
110: *
111: * @param string $name The argument name
112: *
113: * @return mixed The argument value
114: *
115: * @throws \InvalidArgumentException When argument given doesn't exist
116: */
117: public function getArgument($name)
118: {
119: if (!$this->definition->hasArgument($name)) {
120: throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
121: }
122:
123: return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
124: }
125:
126: /**
127: * Sets an argument value by name.
128: *
129: * @param string $name The argument name
130: * @param string $value The argument value
131: *
132: * @throws \InvalidArgumentException When argument given doesn't exist
133: */
134: public function setArgument($name, $value)
135: {
136: if (!$this->definition->hasArgument($name)) {
137: throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
138: }
139:
140: $this->arguments[$name] = $value;
141: }
142:
143: /**
144: * Returns true if an InputArgument object exists by name or position.
145: *
146: * @param string|integer $name The InputArgument name or position
147: *
148: * @return Boolean true if the InputArgument object exists, false otherwise
149: */
150: public function hasArgument($name)
151: {
152: return $this->definition->hasArgument($name);
153: }
154:
155: /**
156: * Returns the options values.
157: *
158: * @return array An array of option values
159: */
160: public function getOptions()
161: {
162: return array_merge($this->definition->getOptionDefaults(), $this->options);
163: }
164:
165: /**
166: * Returns the option value for a given option name.
167: *
168: * @param string $name The option name
169: *
170: * @return mixed The option value
171: *
172: * @throws \InvalidArgumentException When option given doesn't exist
173: */
174: public function getOption($name)
175: {
176: if (!$this->definition->hasOption($name)) {
177: throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
178: }
179:
180: return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
181: }
182:
183: /**
184: * Sets an option value by name.
185: *
186: * @param string $name The option name
187: * @param string $value The option value
188: *
189: * @throws \InvalidArgumentException When option given doesn't exist
190: */
191: public function setOption($name, $value)
192: {
193: if (!$this->definition->hasOption($name)) {
194: throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
195: }
196:
197: $this->options[$name] = $value;
198: }
199:
200: /**
201: * Returns true if an InputOption object exists by name.
202: *
203: * @param string $name The InputOption name
204: *
205: * @return Boolean true if the InputOption object exists, false otherwise
206: */
207: public function hasOption($name)
208: {
209: return $this->definition->hasOption($name);
210: }
211: }
212: