1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Console\Input;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41: class ArgvInput extends Input
42: {
43: private $tokens;
44: private $parsed;
45:
46: 47: 48: 49: 50: 51: 52: 53:
54: public function __construct(array $argv = null, InputDefinition $definition = null)
55: {
56: if (null === $argv) {
57: $argv = $_SERVER['argv'];
58: }
59:
60:
61: array_shift($argv);
62:
63: $this->tokens = $argv;
64:
65: parent::__construct($definition);
66: }
67:
68: protected function setTokens(array $tokens)
69: {
70: $this->tokens = $tokens;
71: }
72:
73: 74: 75:
76: protected function parse()
77: {
78: $parseOptions = true;
79: $this->parsed = $this->tokens;
80: while (null !== $token = array_shift($this->parsed)) {
81: if ($parseOptions && '--' == $token) {
82: $parseOptions = false;
83: } elseif ($parseOptions && 0 === strpos($token, '--')) {
84: $this->parseLongOption($token);
85: } elseif ($parseOptions && '-' === $token[0]) {
86: $this->parseShortOption($token);
87: } else {
88: $this->parseArgument($token);
89: }
90: }
91: }
92:
93: 94: 95: 96: 97:
98: private function parseShortOption($token)
99: {
100: $name = substr($token, 1);
101:
102: if (strlen($name) > 1) {
103: if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
104:
105: $this->addShortOption($name[0], substr($name, 1));
106: } else {
107: $this->parseShortOptionSet($name);
108: }
109: } else {
110: $this->addShortOption($name, null);
111: }
112: }
113:
114: 115: 116: 117: 118: 119: 120:
121: private function parseShortOptionSet($name)
122: {
123: $len = strlen($name);
124: for ($i = 0; $i < $len; $i++) {
125: if (!$this->definition->hasShortcut($name[$i])) {
126: throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
127: }
128:
129: $option = $this->definition->getOptionForShortcut($name[$i]);
130: if ($option->acceptValue()) {
131: $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
132:
133: break;
134: } else {
135: $this->addLongOption($option->getName(), true);
136: }
137: }
138: }
139:
140: 141: 142: 143: 144:
145: private function parseLongOption($token)
146: {
147: $name = substr($token, 2);
148:
149: if (false !== $pos = strpos($name, '=')) {
150: $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
151: } else {
152: $this->addLongOption($name, null);
153: }
154: }
155:
156: 157: 158: 159: 160: 161: 162:
163: private function parseArgument($token)
164: {
165: $c = count($this->arguments);
166:
167:
168: if ($this->definition->hasArgument($c)) {
169: $arg = $this->definition->getArgument($c);
170: $this->arguments[$arg->getName()] = $arg->isArray()? array($token) : $token;
171:
172:
173: } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
174: $arg = $this->definition->getArgument($c - 1);
175: $this->arguments[$arg->getName()][] = $token;
176:
177:
178: } else {
179: throw new \RuntimeException('Too many arguments.');
180: }
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190:
191: private function addShortOption($shortcut, $value)
192: {
193: if (!$this->definition->hasShortcut($shortcut)) {
194: throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
195: }
196:
197: $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207:
208: private function addLongOption($name, $value)
209: {
210: if (!$this->definition->hasOption($name)) {
211: throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
212: }
213:
214: $option = $this->definition->getOption($name);
215:
216: if (null === $value && $option->acceptValue()) {
217:
218:
219: $next = array_shift($this->parsed);
220: if ('-' !== $next[0]) {
221: $value = $next;
222: } else {
223: array_unshift($this->parsed, $next);
224: }
225: }
226:
227: if (null === $value) {
228: if ($option->isValueRequired()) {
229: throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
230: }
231:
232: $value = $option->isValueOptional() ? $option->getDefault() : true;
233: }
234:
235: if ($option->isArray()) {
236: $this->options[$name][] = $value;
237: } else {
238: $this->options[$name] = $value;
239: }
240: }
241:
242: 243: 244: 245: 246:
247: public function getFirstArgument()
248: {
249: foreach ($this->tokens as $token) {
250: if ($token && '-' === $token[0]) {
251: continue;
252: }
253:
254: return $token;
255: }
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265: 266: 267:
268: public function hasParameterOption($values)
269: {
270: $values = (array) $values;
271:
272: foreach ($this->tokens as $v) {
273: if (in_array($v, $values)) {
274: return true;
275: }
276: }
277:
278: return false;
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291:
292: public function getParameterOption($values, $default = false)
293: {
294: $values = (array) $values;
295:
296: $tokens = $this->tokens;
297: while ($token = array_shift($tokens)) {
298: foreach ($values as $value) {
299: if (0 === strpos($token, $value)) {
300: if (false !== $pos = strpos($token, '=')) {
301: return substr($token, $pos + 1);
302: }
303:
304: return array_shift($tokens);
305: }
306: }
307: }
308:
309: return $default;
310: }
311: }
312: