MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Command.php
Ir para a documentação deste ficheiro.
1<?php
2
4
11
12/*
13 * This file is part of the Symfony framework.
14 *
15 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
16 *
17 * This source file is subject to the MIT license that is bundled
18 * with this source code in the file LICENSE.
19 */
20
27{
28 protected $name;
29 protected $namespace;
30 protected $aliases;
31 protected $definition;
32 protected $help;
33 protected $application;
34 protected $description;
37 protected $code;
38
46 public function __construct($name = null)
47 {
48 $this->definition = new InputDefinition();
49 $this->ignoreValidationErrors = false;
50 $this->applicationDefinitionMerged = false;
51 $this->aliases = array();
52
53 if (null !== $name) {
54 $this->setName($name);
55 }
56
57 $this->configure();
58
59 if (!$this->name) {
60 throw new \LogicException('The command name cannot be empty.');
61 }
62 }
63
69 public function setApplication(Application $application = null)
70 {
71 $this->application = $application;
72 }
73
77 protected function configure()
78 {
79 }
80
91 protected function execute(InputInterface $input, OutputInterface $output)
92 {
93 throw new \LogicException('You must override the execute() method in the concrete command class.');
94 }
95
102 protected function interact(InputInterface $input, OutputInterface $output)
103 {
104 }
105
115 protected function initialize(InputInterface $input, OutputInterface $output)
116 {
117 }
118
125 public function run(InputInterface $input, OutputInterface $output)
126 {
127 // add the application arguments and options
129
130 // bind the input against the command specific arguments/options
131 try {
132 $input->bind($this->definition);
133 } catch (\Exception $e) {
134 if (!$this->ignoreValidationErrors) {
135 throw $e;
136 }
137 }
138
139 $this->initialize($input, $output);
140
141 if ($input->isInteractive()) {
142 $this->interact($input, $output);
143 }
144
145 $input->validate();
146
147 if ($this->code) {
148 return call_user_func($this->code, $input, $output);
149 } else {
150 return $this->execute($input, $output);
151 }
152 }
153
161 public function setCode(\Closure $code)
162 {
163 $this->code = $code;
164
165 return $this;
166 }
167
171 protected function mergeApplicationDefinition()
172 {
173 if (null === $this->application || true === $this->applicationDefinitionMerged) {
174 return;
175 }
176
177 $this->definition->setArguments(array_merge(
178 $this->application->getDefinition()->getArguments(),
179 $this->definition->getArguments()
180 ));
181
182 $this->definition->addOptions($this->application->getDefinition()->getOptions());
183
184 $this->applicationDefinitionMerged = true;
185 }
186
194 public function setDefinition($definition)
195 {
196 if ($definition instanceof InputDefinition) {
197 $this->definition = $definition;
198 } else {
199 $this->definition->setDefinition($definition);
200 }
201
202 $this->applicationDefinitionMerged = false;
203
204 return $this;
205 }
206
212 public function getDefinition()
213 {
214 return $this->definition;
215 }
216
227 public function addArgument($name, $mode = null, $description = '', $default = null)
228 {
229 $this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
230
231 return $this;
232 }
233
245 public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
246 {
247 $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
248
249 return $this;
250 }
251
266 public function setName($name)
267 {
268 if (false !== $pos = strrpos($name, ':')) {
269 $namespace = substr($name, 0, $pos);
270 $name = substr($name, $pos + 1);
271 } else {
273 }
274
275 if (!$name) {
276 throw new \InvalidArgumentException('A command name cannot be empty.');
277 }
278
279 $this->namespace = $namespace;
280 $this->name = $name;
281
282 return $this;
283 }
284
290 public function getNamespace()
291 {
292 return $this->namespace;
293 }
294
300 public function getName()
301 {
302 return $this->name;
303 }
304
310 public function getFullName()
311 {
312 return $this->getNamespace() ? $this->getNamespace().':'.$this->getName() : $this->getName();
313 }
314
323 {
324 $this->description = $description;
325
326 return $this;
327 }
328
334 public function getDescription()
335 {
336 return $this->description;
337 }
338
346 public function setHelp($help)
347 {
348 $this->help = $help;
349
350 return $this;
351 }
352
358 public function getHelp()
359 {
360 return $this->help;
361 }
362
369 public function getProcessedHelp()
370 {
371 $name = $this->namespace.':'.$this->name;
372
373 $placeholders = array(
374 '%command.name%',
375 '%command.full_name%'
376 );
377 $replacements = array(
378 $name,
379 $_SERVER['PHP_SELF'].' '.$name
380 );
381
382 return str_replace($placeholders, $replacements, $this->getHelp());
383 }
384
392 public function setAliases($aliases)
393 {
394 $this->aliases = $aliases;
395
396 return $this;
397 }
398
404 public function getAliases()
405 {
406 return $this->aliases;
407 }
408
414 public function getSynopsis()
415 {
416 return sprintf('%s %s', $this->getFullName(), $this->definition->getSynopsis());
417 }
418
428 protected function getHelper($name)
429 {
430 return $this->application->getHelperSet()->get($name);
431 }
432
442 public function __get($name)
443 {
444 return $this->application->getHelperSet()->get($name);
445 }
446
452 public function asText()
453 {
454 $messages = array(
455 '<comment>Usage:</comment>',
456 ' '.$this->getSynopsis(),
457 '',
458 );
459
460 if ($this->getAliases()) {
461 $messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $this->getAliases()).'</info>';
462 }
463
464 $messages[] = $this->definition->asText();
465
466 if ($help = $this->getProcessedHelp()) {
467 $messages[] = '<comment>Help:</comment>';
468 $messages[] = ' '.implode("\n ", explode("\n", $help))."\n";
469 }
470
471 return implode("\n", $messages);
472 }
473
481 public function asXml($asDom = false)
482 {
483 $dom = new \DOMDocument('1.0', 'UTF-8');
484 $dom->formatOutput = true;
485 $dom->appendChild($commandXML = $dom->createElement('command'));
486 $commandXML->setAttribute('id', $this->getFullName());
487 $commandXML->setAttribute('namespace', $this->getNamespace() ? $this->getNamespace() : '_global');
488 $commandXML->setAttribute('name', $this->getName());
489
490 $commandXML->appendChild($usageXML = $dom->createElement('usage'));
491 $usageXML->appendChild($dom->createTextNode(sprintf($this->getSynopsis(), '')));
492
493 $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
494 $descriptionXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $this->getDescription()))));
495
496 $commandXML->appendChild($helpXML = $dom->createElement('help'));
498 $helpXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $help))));
499
500 $commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
501 foreach ($this->getAliases() as $alias) {
502 $aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
503 $aliasXML->appendChild($dom->createTextNode($alias));
504 }
505
506 $definition = $this->definition->asXml(true);
507 $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('arguments')->item(0), true));
508 $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('options')->item(0), true));
509
510 return $asDom ? $dom : $dom->saveXml();
511 }
512}
addArgument($name, $mode=null, $description='', $default=null)
Definição Command.php:227
interact(InputInterface $input, OutputInterface $output)
Definição Command.php:102
execute(InputInterface $input, OutputInterface $output)
Definição Command.php:91
setApplication(Application $application=null)
Definição Command.php:69
run(InputInterface $input, OutputInterface $output)
Definição Command.php:125
initialize(InputInterface $input, OutputInterface $output)
Definição Command.php:115
addOption($name, $shortcut=null, $mode=null, $description='', $default=null)
Definição Command.php:245