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