MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
ArgvInput.php
Ir para a documentação deste ficheiro.
1<?php
2
4
5/*
6 * This file is part of the Symfony framework.
7 *
8 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9 *
10 * This source file is subject to the MIT license that is bundled
11 * with this source code in the file LICENSE.
12 */
13
39class ArgvInput extends Input
40{
41 protected $tokens;
42 protected $parsed;
43
50 public function __construct(array $argv = null, InputDefinition $definition = null)
51 {
52 if (null === $argv) {
53 $argv = $_SERVER['argv'];
54 }
55
56 // strip the program name
57 array_shift($argv);
58
59 $this->tokens = $argv;
60
61 parent::__construct($definition);
62 }
63
67 protected function parse()
68 {
69 $this->parsed = $this->tokens;
70 while (null !== $token = array_shift($this->parsed)) {
71 if ('--' === substr($token, 0, 2)) {
72 $this->parseLongOption($token);
73 } elseif ('-' === $token[0]) {
74 $this->parseShortOption($token);
75 } else {
76 $this->parseArgument($token);
77 }
78 }
79 }
80
86 protected function parseShortOption($token)
87 {
88 $name = substr($token, 1);
89
90 if (strlen($name) > 1) {
91 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
92 // an option with a value (with no space)
93 $this->addShortOption($name[0], substr($name, 1));
94 } else {
95 $this->parseShortOptionSet($name);
96 }
97 } else {
98 $this->addShortOption($name, null);
99 }
100 }
101
109 protected function parseShortOptionSet($name)
110 {
111 $len = strlen($name);
112 for ($i = 0; $i < $len; $i++) {
113 if (!$this->definition->hasShortcut($name[$i])) {
114 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
115 }
116
117 $option = $this->definition->getOptionForShortcut($name[$i]);
118 if ($option->acceptValue()) {
119 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
120
121 break;
122 } else {
123 $this->addLongOption($option->getName(), true);
124 }
125 }
126 }
127
133 protected function parseLongOption($token)
134 {
135 $name = substr($token, 2);
136
137 if (false !== $pos = strpos($name, '=')) {
138 $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
139 } else {
140 $this->addLongOption($name, null);
141 }
142 }
143
151 protected function parseArgument($token)
152 {
153 if (!$this->definition->hasArgument(count($this->arguments))) {
154 throw new \RuntimeException('Too many arguments.');
155 }
156
157 $this->arguments[$this->definition->getArgument(count($this->arguments))->getName()] = $token;
158 }
159
168 protected function addShortOption($shortcut, $value)
169 {
170 if (!$this->definition->hasShortcut($shortcut)) {
171 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
172 }
173
174 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
175 }
176
185 protected function addLongOption($name, $value)
186 {
187 if (!$this->definition->hasOption($name)) {
188 throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
189 }
190
191 $option = $this->definition->getOption($name);
192
193 if (null === $value && $option->acceptValue()) {
194 // if option accepts an optional or mandatory argument
195 // let's see if there is one provided
196 $next = array_shift($this->parsed);
197 if ('-' !== $next[0]) {
198 $value = $next;
199 } else {
200 array_unshift($this->parsed, $next);
201 }
202 }
203
204 if (null === $value) {
205 if ($option->isValueRequired()) {
206 throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
207 }
208
209 $value = $option->isValueOptional() ? $option->getDefault() : true;
210 }
211
212 $this->options[$name] = $value;
213 }
214
220 public function getFirstArgument()
221 {
222 foreach ($this->tokens as $token) {
223 if ($token && '-' === $token[0]) {
224 continue;
225 }
226
227 return $token;
228 }
229 }
230
241 public function hasParameterOption($values)
242 {
243 if (!is_array($values)) {
244 $values = array($values);
245 }
246
247 foreach ($this->tokens as $v) {
248 if (in_array($v, $values)) {
249 return true;
250 }
251 }
252
253 return false;
254 }
255}
__construct(array $argv=null, InputDefinition $definition=null)
Definição ArgvInput.php:50