Command.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. namespace Symfony\Components\Console\Command;
  3. use Symfony\Components\Console\Input\InputDefinition;
  4. use Symfony\Components\Console\Input\InputOption;
  5. use Symfony\Components\Console\Input\InputArgument;
  6. use Symfony\Components\Console\Input\InputInterface;
  7. use Symfony\Components\Console\Output\OutputInterface;
  8. use Symfony\Components\Console\Application;
  9. /*
  10. * This file is part of the symfony framework.
  11. *
  12. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  13. *
  14. * This source file is subject to the MIT license that is bundled
  15. * with this source code in the file LICENSE.
  16. */
  17. /**
  18. * Base class for all commands.
  19. *
  20. * @package symfony
  21. * @subpackage console
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. */
  24. class Command
  25. {
  26. protected $name;
  27. protected $namespace;
  28. protected $aliases;
  29. protected $definition;
  30. protected $help;
  31. protected $application;
  32. protected $description;
  33. protected $ignoreValidationErrors;
  34. protected $formatter;
  35. protected $applicationDefinitionMerged;
  36. protected $code;
  37. /**
  38. * Constructor.
  39. *
  40. * @param string $name The name of the command
  41. */
  42. public function __construct($name = null)
  43. {
  44. $this->definition = new InputDefinition();
  45. $this->ignoreValidationErrors = false;
  46. $this->applicationDefinitionMerged = false;
  47. $this->aliases = array();
  48. if (null !== $name)
  49. {
  50. $this->setName($name);
  51. }
  52. $this->configure();
  53. if (!$this->name)
  54. {
  55. throw new \LogicException('The command name cannot be empty.');
  56. }
  57. }
  58. /**
  59. * Sets the application instance for this command.
  60. *
  61. * @param Application $application An Application instance
  62. */
  63. public function setApplication(Application $application = null)
  64. {
  65. $this->application = $application;
  66. }
  67. /**
  68. * Configures the current command.
  69. */
  70. protected function configure()
  71. {
  72. }
  73. /**
  74. * Executes the current command.
  75. *
  76. * @param InputInterface $input An InputInterface instance
  77. * @param OutputInterface $output An OutputInterface instance
  78. *
  79. * @return integer 0 if everything went fine, or an error code
  80. */
  81. protected function execute(InputInterface $input, OutputInterface $output)
  82. {
  83. throw new \LogicException('You must override the execute() method in the concrete command class.');
  84. }
  85. /**
  86. * Interacts with the user.
  87. *
  88. * @param InputInterface $input An InputInterface instance
  89. * @param OutputInterface $output An OutputInterface instance
  90. */
  91. protected function interact(InputInterface $input, OutputInterface $output)
  92. {
  93. }
  94. /**
  95. * Initializes the command just after the input has been validated.
  96. *
  97. * This is mainly useful when a lot of commands extends one main command
  98. * where some things need to be initialized based on the input arguments and options.
  99. *
  100. * @param InputInterface $input An InputInterface instance
  101. * @param OutputInterface $output An OutputInterface instance
  102. */
  103. protected function initialize(InputInterface $input, OutputInterface $output)
  104. {
  105. }
  106. /**
  107. * Runs the command.
  108. *
  109. * @param InputInterface $input An InputInterface instance
  110. * @param OutputInterface $output An OutputInterface instance
  111. */
  112. public function run(InputInterface $input, OutputInterface $output)
  113. {
  114. // add the application arguments and options
  115. $this->mergeApplicationDefinition();
  116. // bind the input against the command specific arguments/options
  117. try
  118. {
  119. $input->bind($this->definition);
  120. }
  121. catch (\Exception $e)
  122. {
  123. if (!$this->ignoreValidationErrors)
  124. {
  125. throw $e;
  126. }
  127. }
  128. $this->initialize($input, $output);
  129. if ($input->isInteractive())
  130. {
  131. $this->interact($input, $output);
  132. }
  133. $input->validate();
  134. if ($this->code)
  135. {
  136. return call_user_func($this->code, $input, $output);
  137. }
  138. else
  139. {
  140. return $this->execute($input, $output);
  141. }
  142. }
  143. /**
  144. * Sets the code to execute when running this command.
  145. *
  146. * @param \Closure $code A \Closure
  147. *
  148. * @return Command The current instance
  149. */
  150. public function setCode(\Closure $code)
  151. {
  152. $this->code = $code;
  153. return $this;
  154. }
  155. /**
  156. * Merges the application definition with the command definition.
  157. */
  158. protected function mergeApplicationDefinition()
  159. {
  160. if (null === $this->application || true === $this->applicationDefinitionMerged)
  161. {
  162. return;
  163. }
  164. $this->definition->setArguments(array_merge(
  165. $this->application->getDefinition()->getArguments(),
  166. $this->definition->getArguments()
  167. ));
  168. $this->definition->addOptions($this->application->getDefinition()->getOptions());
  169. $this->applicationDefinitionMerged = true;
  170. }
  171. /**
  172. * Sets an array of argument and option instances.
  173. *
  174. * @param array|Definition $definition An array of argument and option instances or a definition instance
  175. *
  176. * @return Command The current instance
  177. */
  178. public function setDefinition($definition)
  179. {
  180. if ($definition instanceof InputDefinition)
  181. {
  182. $this->definition = $definition;
  183. }
  184. else
  185. {
  186. $this->definition->setDefinition($definition);
  187. }
  188. $this->applicationDefinitionMerged = false;
  189. return $this;
  190. }
  191. /**
  192. * Gets the InputDefinition attached to this Command.
  193. *
  194. * @return InputDefinition $definition An InputDefinition instance
  195. */
  196. public function getDefinition()
  197. {
  198. return $this->definition;
  199. }
  200. /**
  201. * Adds an argument.
  202. *
  203. * @param string $name The argument name
  204. * @param integer $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
  205. * @param string $description A description text
  206. * @param mixed $default The default value (for InputArgument::OPTIONAL mode only)
  207. *
  208. * @return Command The current instance
  209. */
  210. public function addArgument($name, $mode = null, $description = '', $default = null)
  211. {
  212. $this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
  213. return $this;
  214. }
  215. /**
  216. * Adds an option.
  217. *
  218. * @param string $name The option name
  219. * @param string $shortcut The shortcut (can be null)
  220. * @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL
  221. * @param string $description A description text
  222. * @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE)
  223. *
  224. * @return Command The current instance
  225. */
  226. public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
  227. {
  228. $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
  229. return $this;
  230. }
  231. /**
  232. * Sets the name of the command.
  233. *
  234. * This method can set both the namespace and the name if
  235. * you separate them by a colon (:)
  236. *
  237. * $command->setName('foo:bar');
  238. *
  239. * @param string $name The command name
  240. *
  241. * @return Command The current instance
  242. */
  243. public function setName($name)
  244. {
  245. if (false !== $pos = strpos($name, ':'))
  246. {
  247. $namespace = substr($name, 0, $pos);
  248. $name = substr($name, $pos + 1);
  249. }
  250. else
  251. {
  252. $namespace = $this->namespace;
  253. }
  254. if (!$name)
  255. {
  256. throw new \InvalidArgumentException('A command name cannot be empty');
  257. }
  258. $this->namespace = $namespace;
  259. $this->name = $name;
  260. return $this;
  261. }
  262. /**
  263. * Returns the command namespace.
  264. *
  265. * @return string The command namespace
  266. */
  267. public function getNamespace()
  268. {
  269. return $this->namespace;
  270. }
  271. /**
  272. * Returns the command name
  273. *
  274. * @return string The command name
  275. */
  276. public function getName()
  277. {
  278. return $this->name;
  279. }
  280. /**
  281. * Returns the fully qualified command name.
  282. *
  283. * @return string The fully qualified command name
  284. */
  285. public function getFullName()
  286. {
  287. return $this->getNamespace() ? $this->getNamespace().':'.$this->getName() : $this->getName();
  288. }
  289. /**
  290. * Sets the description for the command.
  291. *
  292. * @param string $description The description for the command
  293. *
  294. * @return Command The current instance
  295. */
  296. public function setDescription($description)
  297. {
  298. $this->description = $description;
  299. return $this;
  300. }
  301. /**
  302. * Returns the description for the command.
  303. *
  304. * @return string The description for the command
  305. */
  306. public function getDescription()
  307. {
  308. return $this->description;
  309. }
  310. /**
  311. * Sets the help for the command.
  312. *
  313. * @param string $help The help for the command
  314. *
  315. * @return Command The current instance
  316. */
  317. public function setHelp($help)
  318. {
  319. $this->help = $help;
  320. return $this;
  321. }
  322. /**
  323. * Returns the help for the command.
  324. *
  325. * @return string The help for the command
  326. */
  327. public function getHelp()
  328. {
  329. return $this->help;
  330. }
  331. /**
  332. * Sets the aliases for the command.
  333. *
  334. * @param array $aliases An array of aliases for the command
  335. *
  336. * @return Command The current instance
  337. */
  338. public function setAliases($aliases)
  339. {
  340. $this->aliases = $aliases;
  341. return $this;
  342. }
  343. /**
  344. * Returns the aliases for the command.
  345. *
  346. * @return array An array of aliases for the command
  347. */
  348. public function getAliases()
  349. {
  350. return $this->aliases;
  351. }
  352. /**
  353. * Returns the synopsis for the command.
  354. *
  355. * @return string The synopsis
  356. */
  357. public function getSynopsis()
  358. {
  359. return sprintf('%s %s', $this->getFullName(), $this->definition->getSynopsis());
  360. }
  361. /**
  362. * Gets a helper instance by name.
  363. *
  364. * @param string $name The helper name
  365. *
  366. * @return mixed The helper value
  367. *
  368. * @throws \InvalidArgumentException if the helper is not defined
  369. */
  370. protected function getHelper($name)
  371. {
  372. return $this->application->getHelperSet()->get($name);
  373. }
  374. /**
  375. * Gets a helper instance by name.
  376. *
  377. * @param string $name The helper name
  378. *
  379. * @return mixed The helper value
  380. *
  381. * @throws \InvalidArgumentException if the helper is not defined
  382. */
  383. public function __get($name)
  384. {
  385. return $this->application->getHelperSet()->get($name);
  386. }
  387. /**
  388. * Returns a text representation of the command.
  389. *
  390. * @return string A string representing the command
  391. */
  392. public function asText()
  393. {
  394. $messages = array(
  395. '<comment>Usage:</comment>',
  396. ' '.$this->getSynopsis(),
  397. '',
  398. );
  399. if ($this->getAliases())
  400. {
  401. $messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $this->getAliases()).'</info>';
  402. }
  403. $messages[] = $this->definition->asText();
  404. if ($help = $this->getHelp())
  405. {
  406. $messages[] = '<comment>Help:</comment>';
  407. $messages[] = ' '.implode("\n ", explode("\n", $help))."\n";
  408. }
  409. return implode("\n", $messages);
  410. }
  411. /**
  412. * Returns an XML representation of the command.
  413. *
  414. * @param Boolean $asDom Whether to return a DOM or an XML string
  415. *
  416. * @return string|DOMDocument An XML string representing the command
  417. */
  418. public function asXml($asDom = false)
  419. {
  420. $dom = new \DOMDocument('1.0', 'UTF-8');
  421. $dom->formatOutput = true;
  422. $dom->appendChild($commandXML = $dom->createElement('command'));
  423. $commandXML->setAttribute('id', $this->getFullName());
  424. $commandXML->setAttribute('namespace', $this->getNamespace() ? $this->getNamespace() : '_global');
  425. $commandXML->setAttribute('name', $this->getName());
  426. $commandXML->appendChild($usageXML = $dom->createElement('usage'));
  427. $usageXML->appendChild($dom->createTextNode(sprintf($this->getSynopsis(), '')));
  428. $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
  429. $descriptionXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $this->getDescription()))));
  430. $commandXML->appendChild($helpXML = $dom->createElement('help'));
  431. $help = $this->help;
  432. $helpXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $help))));
  433. $commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
  434. foreach ($this->getAliases() as $alias)
  435. {
  436. $aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
  437. $aliasXML->appendChild($dom->createTextNode($alias));
  438. }
  439. $definition = $this->definition->asXml(true);
  440. $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('arguments')->item(0), true));
  441. $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('options')->item(0), true));
  442. return $asDom ? $dom : $dom->saveXml();
  443. }
  444. }