1
0
mirror of synced 2024-12-14 15:16:04 +03:00

DBAL-16 - Update Symfony\Component\Console to latest version to fix upstream bug

This commit is contained in:
Benjamin Eberlei 2010-10-30 13:24:50 +02:00
parent aa2a80f3ff
commit b5c5ec3c69
25 changed files with 3170 additions and 3387 deletions

View File

@ -19,7 +19,7 @@ use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\DialogHelper; use Symfony\Component\Console\Helper\DialogHelper;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -40,15 +40,12 @@ use Symfony\Component\Console\Helper\DialogHelper;
* $app->addCommand(new SimpleCommand()); * $app->addCommand(new SimpleCommand());
* $app->run(); * $app->run();
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class Application class Application
{ {
protected $commands; protected $commands;
protected $aliases; protected $aliases;
protected $application;
protected $wantHelps = false; protected $wantHelps = false;
protected $runningCommand; protected $runningCommand;
protected $name; protected $name;
@ -87,7 +84,7 @@ class Application
new InputOption('--quiet', '-q', InputOption::PARAMETER_NONE, 'Do not output any message.'), new InputOption('--quiet', '-q', InputOption::PARAMETER_NONE, 'Do not output any message.'),
new InputOption('--verbose', '-v', InputOption::PARAMETER_NONE, 'Increase verbosity of messages.'), new InputOption('--verbose', '-v', InputOption::PARAMETER_NONE, 'Increase verbosity of messages.'),
new InputOption('--version', '-V', InputOption::PARAMETER_NONE, 'Display this program version.'), new InputOption('--version', '-V', InputOption::PARAMETER_NONE, 'Display this program version.'),
new InputOption('--color', '-c', InputOption::PARAMETER_NONE, 'Force ANSI color output.'), new InputOption('--ansi', '-a', InputOption::PARAMETER_NONE, 'Force ANSI output.'),
new InputOption('--no-interaction', '-n', InputOption::PARAMETER_NONE, 'Do not ask any interactive question.'), new InputOption('--no-interaction', '-n', InputOption::PARAMETER_NONE, 'Do not ask any interactive question.'),
)); ));
} }
@ -99,27 +96,23 @@ class Application
* @param OutputInterface $output An Output instance * @param OutputInterface $output An Output instance
* *
* @return integer 0 if everything went fine, or an error code * @return integer 0 if everything went fine, or an error code
*
* @throws \Exception When doRun returns Exception
*/ */
public function run(InputInterface $input = null, OutputInterface $output = null) public function run(InputInterface $input = null, OutputInterface $output = null)
{ {
if (null === $input) if (null === $input) {
{
$input = new ArgvInput(); $input = new ArgvInput();
} }
if (null === $output) if (null === $output) {
{
$output = new ConsoleOutput(); $output = new ConsoleOutput();
} }
try try {
{
$statusCode = $this->doRun($input, $output); $statusCode = $this->doRun($input, $output);
} } catch (\Exception $e) {
catch (\Exception $e) if (!$this->catchExceptions) {
{
if (!$this->catchExceptions)
{
throw $e; throw $e;
} }
@ -129,14 +122,14 @@ class Application
$statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1; $statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
} }
if ($this->autoExit) if ($this->autoExit) {
{ if ($statusCode > 255) {
$statusCode = 255;
}
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
exit($statusCode); exit($statusCode);
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} } else {
else
{
return $statusCode; return $statusCode;
} }
} }
@ -151,49 +144,38 @@ class Application
*/ */
public function doRun(InputInterface $input, OutputInterface $output) public function doRun(InputInterface $input, OutputInterface $output)
{ {
$name = $input->getFirstArgument('command'); $name = $this->getCommandName($input);
if (true === $input->hasParameterOption(array('--color', '-c'))) if (true === $input->hasParameterOption(array('--ansi', '-a'))) {
{
$output->setDecorated(true); $output->setDecorated(true);
} }
if (true === $input->hasParameterOption(array('--help', '-H'))) if (true === $input->hasParameterOption(array('--help', '-h'))) {
{ if (!$name) {
if (!$name)
{
$name = 'help'; $name = 'help';
$input = new ArrayInput(array('command' => 'help')); $input = new ArrayInput(array('command' => 'help'));
} } else {
else
{
$this->wantHelps = true; $this->wantHelps = true;
} }
} }
if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) {
{
$input->setInteractive(false); $input->setInteractive(false);
} }
if (true === $input->hasParameterOption(array('--quiet', '-q'))) if (true === $input->hasParameterOption(array('--quiet', '-q'))) {
{
$output->setVerbosity(Output::VERBOSITY_QUIET); $output->setVerbosity(Output::VERBOSITY_QUIET);
} } elseif (true === $input->hasParameterOption(array('--verbose', '-v'))) {
elseif (true === $input->hasParameterOption(array('--verbose', '-v')))
{
$output->setVerbosity(Output::VERBOSITY_VERBOSE); $output->setVerbosity(Output::VERBOSITY_VERBOSE);
} }
if (true === $input->hasParameterOption(array('--version', '-V'))) if (true === $input->hasParameterOption(array('--version', '-V'))) {
{
$output->writeln($this->getLongVersion()); $output->writeln($this->getLongVersion());
return 0; return 0;
} }
if (!$name) if (!$name) {
{
$name = 'list'; $name = 'list';
$input = new ArrayInput(array('command' => 'list')); $input = new ArrayInput(array('command' => 'list'));
} }
@ -221,7 +203,7 @@ class Application
/** /**
* Get the helper set associated with the command * Get the helper set associated with the command
* *
* @return HelperSet The HelperSet isntance associated with this command * @return HelperSet The HelperSet instance associated with this command
*/ */
public function getHelperSet() public function getHelperSet()
{ {
@ -253,8 +235,7 @@ class Application
'<comment>Options:</comment>', '<comment>Options:</comment>',
); );
foreach ($this->definition->getOptions() as $option) foreach ($this->definition->getOptions() as $option) {
{
$messages[] = sprintf(' %-29s %s %s', $messages[] = sprintf(' %-29s %s %s',
'<info>--'.$option->getName().'</info>', '<info>--'.$option->getName().'</info>',
$option->getShortcut() ? '<info>-'.$option->getShortcut().'</info>' : ' ', $option->getShortcut() ? '<info>-'.$option->getShortcut().'</info>' : ' ',
@ -332,12 +313,9 @@ class Application
*/ */
public function getLongVersion() public function getLongVersion()
{ {
if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
{
return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion()); return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
} } else {
else
{
return '<info>Console Tool</info>'; return '<info>Console Tool</info>';
} }
} }
@ -357,12 +335,11 @@ class Application
/** /**
* Adds an array of command objects. * Adds an array of command objects.
* *
* @param array $commands An array of commands * @param Command[] $commands An array of commands
*/ */
public function addCommands(array $commands) public function addCommands(array $commands)
{ {
foreach ($commands as $command) foreach ($commands as $command) {
{
$this->addCommand($command); $this->addCommand($command);
} }
} }
@ -382,8 +359,7 @@ class Application
$this->commands[$command->getFullName()] = $command; $this->commands[$command->getFullName()] = $command;
foreach ($command->getAliases() as $alias) foreach ($command->getAliases() as $alias) {
{
$this->aliases[$alias] = $command; $this->aliases[$alias] = $command;
} }
@ -396,18 +372,18 @@ class Application
* @param string $name The command name or alias * @param string $name The command name or alias
* *
* @return Command A Command object * @return Command A Command object
*
* @throws \InvalidArgumentException When command name given does not exist
*/ */
public function getCommand($name) public function getCommand($name)
{ {
if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
{
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
} }
$command = isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name]; $command = isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
if ($this->wantHelps) if ($this->wantHelps) {
{
$this->wantHelps = false; $this->wantHelps = false;
$helpCommand = $this->getCommand('help'); $helpCommand = $this->getCommand('help');
@ -441,10 +417,8 @@ class Application
public function getNamespaces() public function getNamespaces()
{ {
$namespaces = array(); $namespaces = array();
foreach ($this->commands as $command) foreach ($this->commands as $command) {
{ if ($command->getNamespace()) {
if ($command->getNamespace())
{
$namespaces[$command->getNamespace()] = true; $namespaces[$command->getNamespace()] = true;
} }
} }
@ -456,18 +430,18 @@ class Application
* Finds a registered namespace by a name or an abbreviation. * Finds a registered namespace by a name or an abbreviation.
* *
* @return string A registered namespace * @return string A registered namespace
*
* @throws \InvalidArgumentException When namespace is incorrect or ambiguous
*/ */
public function findNamespace($namespace) public function findNamespace($namespace)
{ {
$abbrevs = static::getAbbreviations($this->getNamespaces()); $abbrevs = static::getAbbreviations($this->getNamespaces());
if (!isset($abbrevs[$namespace])) if (!isset($abbrevs[$namespace])) {
{
throw new \InvalidArgumentException(sprintf('There are no commands defined in the "%s" namespace.', $namespace)); throw new \InvalidArgumentException(sprintf('There are no commands defined in the "%s" namespace.', $namespace));
} }
if (count($abbrevs[$namespace]) > 1) if (count($abbrevs[$namespace]) > 1) {
{
throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions($abbrevs[$namespace]))); throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions($abbrevs[$namespace])));
} }
@ -483,13 +457,14 @@ class Application
* @param string $name A command name or a command alias * @param string $name A command name or a command alias
* *
* @return Command A Command instance * @return Command A Command instance
*
* @throws \InvalidArgumentException When command name is incorrect or ambiguous
*/ */
public function findCommand($name) public function findCommand($name)
{ {
// namespace // namespace
$namespace = ''; $namespace = '';
if (false !== $pos = strrpos($name, ':')) if (false !== $pos = strrpos($name, ':')) {
{
$namespace = $this->findNamespace(substr($name, 0, $pos)); $namespace = $this->findNamespace(substr($name, 0, $pos));
$name = substr($name, $pos + 1); $name = substr($name, $pos + 1);
} }
@ -498,22 +473,18 @@ class Application
// name // name
$commands = array(); $commands = array();
foreach ($this->commands as $command) foreach ($this->commands as $command) {
{ if ($command->getNamespace() == $namespace) {
if ($command->getNamespace() == $namespace)
{
$commands[] = $command->getName(); $commands[] = $command->getName();
} }
} }
$abbrevs = static::getAbbreviations($commands); $abbrevs = static::getAbbreviations($commands);
if (isset($abbrevs[$name]) && 1 == count($abbrevs[$name])) if (isset($abbrevs[$name]) && 1 == count($abbrevs[$name])) {
{
return $this->getCommand($namespace ? $namespace.':'.$abbrevs[$name][0] : $abbrevs[$name][0]); return $this->getCommand($namespace ? $namespace.':'.$abbrevs[$name][0] : $abbrevs[$name][0]);
} }
if (isset($abbrevs[$name]) && count($abbrevs[$name]) > 1) if (isset($abbrevs[$name]) && count($abbrevs[$name]) > 1) {
{
$suggestions = $this->getAbbreviationSuggestions(array_map(function ($command) use ($namespace) { return $namespace.':'.$command; }, $abbrevs[$name])); $suggestions = $this->getAbbreviationSuggestions(array_map(function ($command) use ($namespace) { return $namespace.':'.$command; }, $abbrevs[$name]));
throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $suggestions)); throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $suggestions));
@ -521,13 +492,11 @@ class Application
// aliases // aliases
$abbrevs = static::getAbbreviations(array_keys($this->aliases)); $abbrevs = static::getAbbreviations(array_keys($this->aliases));
if (!isset($abbrevs[$fullName])) if (!isset($abbrevs[$fullName])) {
{
throw new \InvalidArgumentException(sprintf('Command "%s" is not defined.', $fullName)); throw new \InvalidArgumentException(sprintf('Command "%s" is not defined.', $fullName));
} }
if (count($abbrevs[$fullName]) > 1) if (count($abbrevs[$fullName]) > 1) {
{
throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $this->getAbbreviationSuggestions($abbrevs[$fullName]))); throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $this->getAbbreviationSuggestions($abbrevs[$fullName])));
} }
@ -545,16 +514,13 @@ class Application
*/ */
public function getCommands($namespace = null) public function getCommands($namespace = null)
{ {
if (null === $namespace) if (null === $namespace) {
{
return $this->commands; return $this->commands;
} }
$commands = array(); $commands = array();
foreach ($this->commands as $name => $command) foreach ($this->commands as $name => $command) {
{ if ($namespace === $command->getNamespace()) {
if ($namespace === $command->getNamespace())
{
$commands[$name] = $command; $commands[$name] = $command;
} }
} }
@ -565,32 +531,26 @@ class Application
/** /**
* Returns an array of possible abbreviations given a set of names. * Returns an array of possible abbreviations given a set of names.
* *
* @param array An array of names * @param array $names An array of names
* *
* @return array An array of abbreviations * @return array An array of abbreviations
*/ */
static public function getAbbreviations($names) static public function getAbbreviations($names)
{ {
$abbrevs = array(); $abbrevs = array();
foreach ($names as $name) foreach ($names as $name) {
{ for ($len = strlen($name) - 1; $len > 0; --$len) {
for ($len = strlen($name) - 1; $len > 0; --$len)
{
$abbrev = substr($name, 0, $len); $abbrev = substr($name, 0, $len);
if (!isset($abbrevs[$abbrev])) if (!isset($abbrevs[$abbrev])) {
{
$abbrevs[$abbrev] = array($name); $abbrevs[$abbrev] = array($name);
} } else {
else
{
$abbrevs[$abbrev][] = $name; $abbrevs[$abbrev][] = $name;
} }
} }
} }
// Non-abbreviations always get entered, even if they aren't unique // Non-abbreviations always get entered, even if they aren't unique
foreach ($names as $name) foreach ($names as $name) {
{
$abbrevs[$name] = array($name); $abbrevs[$name] = array($name);
} }
@ -609,32 +569,25 @@ class Application
$commands = $namespace ? $this->getCommands($this->findNamespace($namespace)) : $this->commands; $commands = $namespace ? $this->getCommands($this->findNamespace($namespace)) : $this->commands;
$messages = array($this->getHelp(), ''); $messages = array($this->getHelp(), '');
if ($namespace) if ($namespace) {
{
$messages[] = sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $namespace); $messages[] = sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $namespace);
} } else {
else
{
$messages[] = '<comment>Available commands:</comment>'; $messages[] = '<comment>Available commands:</comment>';
} }
$width = 0; $width = 0;
foreach ($commands as $command) foreach ($commands as $command) {
{
$width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width; $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
} }
$width += 2; $width += 2;
// add commands by namespace // add commands by namespace
foreach ($this->sortCommands($commands) as $space => $commands) foreach ($this->sortCommands($commands) as $space => $commands) {
{ if (!$namespace && '_global' !== $space) {
if (!$namespace && '_global' !== $space)
{
$messages[] = '<comment>'.$space.'</comment>'; $messages[] = '<comment>'.$space.'</comment>';
} }
foreach ($commands as $command) foreach ($commands as $command) {
{
$aliases = $command->getAliases() ? '<comment> ('.implode(', ', $command->getAliases()).')</comment>' : ''; $aliases = $command->getAliases() ? '<comment> ('.implode(', ', $command->getAliases()).')</comment>' : '';
$messages[] = sprintf(" <info>%-${width}s</info> %s%s", ($command->getNamespace() ? ':' : '').$command->getName(), $command->getDescription(), $aliases); $messages[] = sprintf(" <info>%-${width}s</info> %s%s", ($command->getNamespace() ? ':' : '').$command->getName(), $command->getDescription(), $aliases);
@ -662,36 +615,26 @@ class Application
$xml->appendChild($commandsXML = $dom->createElement('commands')); $xml->appendChild($commandsXML = $dom->createElement('commands'));
if ($namespace) if ($namespace) {
{
$commandsXML->setAttribute('namespace', $namespace); $commandsXML->setAttribute('namespace', $namespace);
} } else {
else
{
$xml->appendChild($namespacesXML = $dom->createElement('namespaces')); $xml->appendChild($namespacesXML = $dom->createElement('namespaces'));
} }
// add commands by namespace // add commands by namespace
foreach ($this->sortCommands($commands) as $space => $commands) foreach ($this->sortCommands($commands) as $space => $commands) {
{ if (!$namespace) {
if (!$namespace)
{
$namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace')); $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
$namespaceArrayXML->setAttribute('id', $space); $namespaceArrayXML->setAttribute('id', $space);
} }
foreach ($commands as $command) foreach ($commands as $command) {
{ if (!$namespace) {
if (!$namespace)
{
$namespaceArrayXML->appendChild($commandXML = $dom->createElement('command')); $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
$commandXML->appendChild($dom->createTextNode($command->getName())); $commandXML->appendChild($dom->createTextNode($command->getName()));
} }
$commandXML = new \DOMDocument('1.0', 'UTF-8'); $node = $command->asXml(true)->getElementsByTagName('command')->item(0);
$commandXML->formatOutput = true;
$commandXML->loadXML($command->asXml());
$node = $commandXML->getElementsByTagName('command')->item(0);
$node = $dom->importNode($node, true); $node = $dom->importNode($node, true);
$commandsXML->appendChild($node); $commandsXML->appendChild($node);
@ -717,36 +660,31 @@ class Application
$title = sprintf(' [%s] ', get_class($e)); $title = sprintf(' [%s] ', get_class($e));
$len = $strlen($title); $len = $strlen($title);
$lines = array(); $lines = array();
foreach (explode("\n", $e->getMessage()) as $line) foreach (explode("\n", $e->getMessage()) as $line) {
{
$lines[] = sprintf(' %s ', $line); $lines[] = sprintf(' %s ', $line);
$len = max($strlen($line) + 4, $len); $len = max($strlen($line) + 4, $len);
} }
$messages = array(str_repeat(' ', $len), $title.str_repeat(' ', $len - $strlen($title))); $messages = array(str_repeat(' ', $len), $title.str_repeat(' ', $len - $strlen($title)));
foreach ($lines as $line) foreach ($lines as $line) {
{
$messages[] = $line.str_repeat(' ', $len - $strlen($line)); $messages[] = $line.str_repeat(' ', $len - $strlen($line));
} }
$messages[] = str_repeat(' ', $len); $messages[] = str_repeat(' ', $len);
$output->writeln("\n"); $output->writeln("\n");
foreach ($messages as $message) foreach ($messages as $message) {
{ $output->writeln('<error>'.$message.'</error>');
$output->writeln("<error>$message</error>");
} }
$output->writeln("\n"); $output->writeln("\n");
if (null !== $this->runningCommand) if (null !== $this->runningCommand) {
{
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName()))); $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
$output->writeln("\n"); $output->writeln("\n");
} }
if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) {
{
$output->writeln('</comment>Exception trace:</comment>'); $output->writeln('</comment>Exception trace:</comment>');
// exception related properties // exception related properties
@ -758,8 +696,7 @@ class Application
'args' => array(), 'args' => array(),
)); ));
for ($i = 0, $count = count($trace); $i < $count; $i++) for ($i = 0, $count = count($trace); $i < $count; $i++) {
{
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
$type = isset($trace[$i]['type']) ? $trace[$i]['type'] : ''; $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
$function = $trace[$i]['function']; $function = $trace[$i]['function'];
@ -773,15 +710,18 @@ class Application
} }
} }
private function sortCommands($commands) protected function getCommandName(InputInterface $input)
{
return $input->getFirstArgument('command');
}
protected function sortCommands($commands)
{ {
$namespacedCommands = array(); $namespacedCommands = array();
foreach ($commands as $name => $command) foreach ($commands as $name => $command) {
{
$key = $command->getNamespace() ? $command->getNamespace() : '_global'; $key = $command->getNamespace() ? $command->getNamespace() : '_global';
if (!isset($namespacedCommands[$key])) if (!isset($namespacedCommands[$key])) {
{
$namespacedCommands[$key] = array(); $namespacedCommands[$key] = array();
} }
@ -789,15 +729,14 @@ class Application
} }
ksort($namespacedCommands); ksort($namespacedCommands);
foreach ($namespacedCommands as $name => &$commands) foreach ($namespacedCommands as $name => &$commands) {
{
ksort($commands); ksort($commands);
} }
return $namespacedCommands; return $namespacedCommands;
} }
private function getAbbreviationSuggestions($abbrevs) protected function getAbbreviationSuggestions($abbrevs)
{ {
return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : ''); return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
} }

View File

@ -10,7 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -21,8 +21,6 @@ use Symfony\Component\Console\Application;
/** /**
* Base class for all commands. * Base class for all commands.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class Command class Command
@ -35,7 +33,6 @@ class Command
protected $application; protected $application;
protected $description; protected $description;
protected $ignoreValidationErrors; protected $ignoreValidationErrors;
protected $formatter;
protected $applicationDefinitionMerged; protected $applicationDefinitionMerged;
protected $code; protected $code;
@ -43,6 +40,8 @@ class Command
* Constructor. * Constructor.
* *
* @param string $name The name of the command * @param string $name The name of the command
*
* @throws \LogicException When the command name is empty
*/ */
public function __construct($name = null) public function __construct($name = null)
{ {
@ -51,15 +50,13 @@ class Command
$this->applicationDefinitionMerged = false; $this->applicationDefinitionMerged = false;
$this->aliases = array(); $this->aliases = array();
if (null !== $name) if (null !== $name) {
{
$this->setName($name); $this->setName($name);
} }
$this->configure(); $this->configure();
if (!$this->name) if (!$this->name) {
{
throw new \LogicException('The command name cannot be empty.'); throw new \LogicException('The command name cannot be empty.');
} }
} }
@ -88,6 +85,8 @@ class Command
* @param OutputInterface $output An OutputInterface instance * @param OutputInterface $output An OutputInterface instance
* *
* @return integer 0 if everything went fine, or an error code * @return integer 0 if everything went fine, or an error code
*
* @throws \LogicException When this abstract class is not implemented
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
@ -129,33 +128,25 @@ class Command
$this->mergeApplicationDefinition(); $this->mergeApplicationDefinition();
// bind the input against the command specific arguments/options // bind the input against the command specific arguments/options
try try {
{
$input->bind($this->definition); $input->bind($this->definition);
} } catch (\Exception $e) {
catch (\Exception $e) if (!$this->ignoreValidationErrors) {
{
if (!$this->ignoreValidationErrors)
{
throw $e; throw $e;
} }
} }
$this->initialize($input, $output); $this->initialize($input, $output);
if ($input->isInteractive()) if ($input->isInteractive()) {
{
$this->interact($input, $output); $this->interact($input, $output);
} }
$input->validate(); $input->validate();
if ($this->code) if ($this->code) {
{
return call_user_func($this->code, $input, $output); return call_user_func($this->code, $input, $output);
} } else {
else
{
return $this->execute($input, $output); return $this->execute($input, $output);
} }
} }
@ -179,8 +170,7 @@ class Command
*/ */
protected function mergeApplicationDefinition() protected function mergeApplicationDefinition()
{ {
if (null === $this->application || true === $this->applicationDefinitionMerged) if (null === $this->application || true === $this->applicationDefinitionMerged) {
{
return; return;
} }
@ -203,12 +193,9 @@ class Command
*/ */
public function setDefinition($definition) public function setDefinition($definition)
{ {
if ($definition instanceof InputDefinition) if ($definition instanceof InputDefinition) {
{
$this->definition = $definition; $this->definition = $definition;
} } else {
else
{
$this->definition->setDefinition($definition); $this->definition->setDefinition($definition);
} }
@ -220,7 +207,7 @@ class Command
/** /**
* Gets the InputDefinition attached to this Command. * Gets the InputDefinition attached to this Command.
* *
* @return InputDefinition $definition An InputDefinition instance * @return InputDefinition An InputDefinition instance
*/ */
public function getDefinition() public function getDefinition()
{ {
@ -273,22 +260,20 @@ class Command
* @param string $name The command name * @param string $name The command name
* *
* @return Command The current instance * @return Command The current instance
*
* @throws \InvalidArgumentException When command name given is empty
*/ */
public function setName($name) public function setName($name)
{ {
if (false !== $pos = strrpos($name, ':')) if (false !== $pos = strrpos($name, ':')) {
{
$namespace = substr($name, 0, $pos); $namespace = substr($name, 0, $pos);
$name = substr($name, $pos + 1); $name = substr($name, $pos + 1);
} } else {
else
{
$namespace = $this->namespace; $namespace = $this->namespace;
} }
if (!$name) if (!$name) {
{ throw new \InvalidArgumentException('A command name cannot be empty.');
throw new \InvalidArgumentException('A command name cannot be empty');
} }
$this->namespace = $namespace; $this->namespace = $namespace;
@ -472,15 +457,13 @@ class Command
'', '',
); );
if ($this->getAliases()) if ($this->getAliases()) {
{
$messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $this->getAliases()).'</info>'; $messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $this->getAliases()).'</info>';
} }
$messages[] = $this->definition->asText(); $messages[] = $this->definition->asText();
if ($help = $this->getProcessedHelp()) if ($help = $this->getProcessedHelp()) {
{
$messages[] = '<comment>Help:</comment>'; $messages[] = '<comment>Help:</comment>';
$messages[] = ' '.implode("\n ", explode("\n", $help))."\n"; $messages[] = ' '.implode("\n ", explode("\n", $help))."\n";
} }
@ -515,8 +498,7 @@ class Command
$helpXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $help)))); $helpXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $help))));
$commandXML->appendChild($aliasesXML = $dom->createElement('aliases')); $commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
foreach ($this->getAliases() as $alias) foreach ($this->getAliases() as $alias) {
{
$aliasesXML->appendChild($aliasXML = $dom->createElement('alias')); $aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
$aliasXML->appendChild($dom->createTextNode($alias)); $aliasXML->appendChild($dom->createTextNode($alias));
} }

View File

@ -10,7 +10,7 @@ use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -21,8 +21,6 @@ use Symfony\Component\Console\Command\Command;
/** /**
* HelpCommand displays the help for a given command. * HelpCommand displays the help for a given command.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class HelpCommand extends Command class HelpCommand extends Command
@ -47,11 +45,11 @@ class HelpCommand extends Command
->setHelp(<<<EOF ->setHelp(<<<EOF
The <info>help</info> command displays help for a given command: The <info>help</info> command displays help for a given command:
<info>./symfony help test:all</info> <info>./symfony help list</info>
You can also output the help as XML by using the <comment>--xml</comment> option: You can also output the help as XML by using the <comment>--xml</comment> option:
<info>./symfony help --xml test:all</info> <info>./symfony help --xml list</info>
EOF EOF
); );
} }
@ -66,17 +64,13 @@ EOF
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
if (null === $this->command) if (null === $this->command) {
{
$this->command = $this->application->getCommand($input->getArgument('command_name')); $this->command = $this->application->getCommand($input->getArgument('command_name'));
} }
if ($input->getOption('xml')) if ($input->getOption('xml')) {
{
$output->writeln($this->command->asXml(), Output::OUTPUT_RAW); $output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
} } else {
else
{
$output->writeln($this->command->asText()); $output->writeln($this->command->asText());
} }
} }

View File

@ -10,7 +10,7 @@ use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -21,8 +21,6 @@ use Symfony\Component\Console\Command\Command;
/** /**
* ListCommand displays the list of all available commands for the application. * ListCommand displays the list of all available commands for the application.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class ListCommand extends Command class ListCommand extends Command
@ -60,12 +58,9 @@ EOF
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
if ($input->getOption('xml')) if ($input->getOption('xml')) {
{
$output->writeln($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW); $output->writeln($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW);
} } else {
else
{
$output->writeln($this->application->asText($input->getArgument('namespace'))); $output->writeln($this->application->asText($input->getArgument('namespace')));
} }
} }

View File

@ -5,7 +5,7 @@ namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -16,8 +16,6 @@ use Symfony\Component\Console\Output\OutputInterface;
/** /**
* The Dialog class provides helpers to interact with the user. * The Dialog class provides helpers to interact with the user.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class DialogHelper extends Helper class DialogHelper extends Helper
@ -29,7 +27,7 @@ class DialogHelper extends Helper
* @param string|array $question The question to ask * @param string|array $question The question to ask
* @param string $default The default answer if none is given by the user * @param string $default The default answer if none is given by the user
* *
* @param string The user answer * @return string The user answer
*/ */
public function ask(OutputInterface $output, $question, $default = null) public function ask(OutputInterface $output, $question, $default = null)
{ {
@ -51,23 +49,19 @@ class DialogHelper extends Helper
* @param string|array $question The question to ask * @param string|array $question The question to ask
* @param Boolean $default The default answer if the user enters nothing * @param Boolean $default The default answer if the user enters nothing
* *
* @param Boolean true if the user has confirmed, false otherwise * @return Boolean true if the user has confirmed, false otherwise
*/ */
public function askConfirmation(OutputInterface $output, $question, $default = true) public function askConfirmation(OutputInterface $output, $question, $default = true)
{ {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
$answer = 'z'; $answer = 'z';
while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
{
$answer = $this->ask($output, $question); $answer = $this->ask($output, $question);
} }
if (false === $default) if (false === $default) {
{
return $answer && 'y' == strtolower($answer[0]); return $answer && 'y' == strtolower($answer[0]);
} } else {
else
{
return !$answer || 'y' == strtolower($answer[0]); return !$answer || 'y' == strtolower($answer[0]);
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
@ -82,26 +76,23 @@ class DialogHelper extends Helper
* @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite) * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
* *
* @return mixed * @return mixed
*
* @throws \Exception When any of the validator returns an error
*/ */
public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false) public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false)
{ {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
$error = null; $error = null;
while (false === $attempts || $attempts--) while (false === $attempts || $attempts--) {
{ if (null !== $error) {
if (null !== $error)
{
$output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
} }
$value = $this->ask($output, $question, null); $value = $this->ask($output, $question, null);
try try {
{
return $validator($value); return $validator($value);
} } catch (\Exception $error) {
catch (\Exception $error)
{
} }
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Helper; namespace Symfony\Component\Console\Helper;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Helper;
/** /**
* The Formatter class provides helpers to format messages. * The Formatter class provides helpers to format messages.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class FormatterHelper extends Helper class FormatterHelper extends Helper
@ -29,7 +27,7 @@ class FormatterHelper extends Helper
*/ */
public function formatSection($section, $message, $style = 'info') public function formatSection($section, $message, $style = 'info')
{ {
return sprintf("<%s>[%s]</%s> %s", $style, $section, $style, $message); return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
} }
/** /**
@ -43,31 +41,26 @@ class FormatterHelper extends Helper
*/ */
public function formatBlock($messages, $style, $large = false) public function formatBlock($messages, $style, $large = false)
{ {
if (!is_array($messages)) if (!is_array($messages)) {
{
$messages = array($messages); $messages = array($messages);
} }
$len = 0; $len = 0;
$lines = array(); $lines = array();
foreach ($messages as $message) foreach ($messages as $message) {
{
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message); $lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
$len = max($this->strlen($message) + ($large ? 4 : 2), $len); $len = max($this->strlen($message) + ($large ? 4 : 2), $len);
} }
$messages = $large ? array(str_repeat(' ', $len)) : array(); $messages = $large ? array(str_repeat(' ', $len)) : array();
foreach ($lines as $line) foreach ($lines as $line) {
{
$messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); $messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
} }
if ($large) if ($large) {
{
$messages[] = str_repeat(' ', $len); $messages[] = str_repeat(' ', $len);
} }
foreach ($messages as &$message) foreach ($messages as &$message) {
{
$message = sprintf('<%s>%s</%s>', $style, $message, $style); $message = sprintf('<%s>%s</%s>', $style, $message, $style);
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Helper; namespace Symfony\Component\Console\Helper;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,14 +14,11 @@ namespace Symfony\Component\Console\Helper;
/** /**
* Helper is the base class for all helper classes. * Helper is the base class for all helper classes.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
abstract class Helper implements HelperInterface abstract class Helper implements HelperInterface
{ {
protected protected $helperSet = null;
$helperSet = null;
/** /**
* Sets the helper set associated with this helper. * Sets the helper set associated with this helper.

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Helper; namespace Symfony\Component\Console\Helper;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Helper;
/** /**
* HelperInterface is the interface all helpers must implement. * HelperInterface is the interface all helpers must implement.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
interface HelperInterface interface HelperInterface

View File

@ -5,7 +5,7 @@ namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -16,20 +16,20 @@ use Symfony\Component\Console\Command\Command;
/** /**
* HelperSet represents a set of helpers to be used with a command. * HelperSet represents a set of helpers to be used with a command.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class HelperSet class HelperSet
{ {
protected protected $helpers;
$helpers = array(), protected $command;
$command = null;
/**
* @param Helper[] $helpers An array of helper.
*/
public function __construct(array $helpers = array()) public function __construct(array $helpers = array())
{ {
foreach ($helpers as $alias => $helper) $this->helpers = array();
{ foreach ($helpers as $alias => $helper) {
$this->set($helper, is_int($alias) ? null : $alias); $this->set($helper, is_int($alias) ? null : $alias);
} }
} }
@ -43,8 +43,7 @@ class HelperSet
public function set(HelperInterface $helper, $alias = null) public function set(HelperInterface $helper, $alias = null)
{ {
$this->helpers[$helper->getName()] = $helper; $this->helpers[$helper->getName()] = $helper;
if (null !== $alias) if (null !== $alias) {
{
$this->helpers[$alias] = $helper; $this->helpers[$alias] = $helper;
} }
@ -74,8 +73,7 @@ class HelperSet
*/ */
public function get($name) public function get($name)
{ {
if (!$this->has($name)) if (!$this->has($name)) {
{
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -20,7 +20,7 @@ namespace Symfony\Component\Console\Input;
* *
* By default, the `$_SERVER['argv']` array is used for the input values. * By default, the `$_SERVER['argv']` array is used for the input values.
* *
* This can be overriden by explicitly passing the input values in the constructor: * This can be overridden by explicitly passing the input values in the constructor:
* *
* $input = new ArgvInput($_SERVER['argv']); * $input = new ArgvInput($_SERVER['argv']);
* *
@ -31,8 +31,6 @@ namespace Symfony\Component\Console\Input;
* the same rules as the argv one. It's almost always better to use the * the same rules as the argv one. It's almost always better to use the
* `StringInput` when you want to provide your own input. * `StringInput` when you want to provide your own input.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
* *
* @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
@ -51,8 +49,7 @@ class ArgvInput extends Input
*/ */
public function __construct(array $argv = null, InputDefinition $definition = null) public function __construct(array $argv = null, InputDefinition $definition = null)
{ {
if (null === $argv) if (null === $argv) {
{
$argv = $_SERVER['argv']; $argv = $_SERVER['argv'];
} }
@ -70,18 +67,12 @@ class ArgvInput extends Input
protected function parse() protected function parse()
{ {
$this->parsed = $this->tokens; $this->parsed = $this->tokens;
while (null !== ($token = array_shift($this->parsed))) while (null !== $token = array_shift($this->parsed)) {
{ if ('--' === substr($token, 0, 2)) {
if ('--' === substr($token, 0, 2))
{
$this->parseLongOption($token); $this->parseLongOption($token);
} } elseif ('-' === $token[0]) {
elseif ('-' === $token[0])
{
$this->parseShortOption($token); $this->parseShortOption($token);
} } else {
else
{
$this->parseArgument($token); $this->parseArgument($token);
} }
} }
@ -96,20 +87,14 @@ class ArgvInput extends Input
{ {
$name = substr($token, 1); $name = substr($token, 1);
if (strlen($name) > 1) if (strlen($name) > 1) {
{ if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptParameter()) {
if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptParameter())
{
// an option with a value (with no space) // an option with a value (with no space)
$this->addShortOption($name[0], substr($name, 1)); $this->addShortOption($name[0], substr($name, 1));
} } else {
else
{
$this->parseShortOptionSet($name); $this->parseShortOptionSet($name);
} }
} } else {
else
{
$this->addShortOption($name, null); $this->addShortOption($name, null);
} }
} }
@ -118,26 +103,23 @@ class ArgvInput extends Input
* Parses a short option set. * Parses a short option set.
* *
* @param string $token The current token * @param string $token The current token
*
* @throws \RuntimeException When option given doesn't exist
*/ */
protected function parseShortOptionSet($name) protected function parseShortOptionSet($name)
{ {
$len = strlen($name); $len = strlen($name);
for ($i = 0; $i < $len; $i++) for ($i = 0; $i < $len; $i++) {
{ if (!$this->definition->hasShortcut($name[$i])) {
if (!$this->definition->hasShortcut($name[$i]))
{
throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i])); throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
} }
$option = $this->definition->getOptionForShortcut($name[$i]); $option = $this->definition->getOptionForShortcut($name[$i]);
if ($option->acceptParameter()) if ($option->acceptParameter()) {
{
$this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
break; break;
} } else {
else
{
$this->addLongOption($option->getName(), true); $this->addLongOption($option->getName(), true);
} }
} }
@ -152,12 +134,9 @@ class ArgvInput extends Input
{ {
$name = substr($token, 2); $name = substr($token, 2);
if (false !== $pos = strpos($name, '=')) if (false !== $pos = strpos($name, '=')) {
{
$this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1)); $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
} } else {
else
{
$this->addLongOption($name, null); $this->addLongOption($name, null);
} }
} }
@ -166,11 +145,12 @@ class ArgvInput extends Input
* Parses an argument. * Parses an argument.
* *
* @param string $token The current token * @param string $token The current token
*
* @throws \RuntimeException When too many arguments are given
*/ */
protected function parseArgument($token) protected function parseArgument($token)
{ {
if (!$this->definition->hasArgument(count($this->arguments))) if (!$this->definition->hasArgument(count($this->arguments))) {
{
throw new \RuntimeException('Too many arguments.'); throw new \RuntimeException('Too many arguments.');
} }
@ -182,11 +162,12 @@ class ArgvInput extends Input
* *
* @param string $shortcut The short option key * @param string $shortcut The short option key
* @param mixed $value The value for the option * @param mixed $value The value for the option
*
* @throws \RuntimeException When option given doesn't exist
*/ */
protected function addShortOption($shortcut, $value) protected function addShortOption($shortcut, $value)
{ {
if (!$this->definition->hasShortcut($shortcut)) if (!$this->definition->hasShortcut($shortcut)) {
{
throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
} }
@ -198,35 +179,30 @@ class ArgvInput extends Input
* *
* @param string $name The long option key * @param string $name The long option key
* @param mixed $value The value for the option * @param mixed $value The value for the option
*
* @throws \RuntimeException When option given doesn't exist
*/ */
protected function addLongOption($name, $value) protected function addLongOption($name, $value)
{ {
if (!$this->definition->hasOption($name)) if (!$this->definition->hasOption($name)) {
{
throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name)); throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
} }
$option = $this->definition->getOption($name); $option = $this->definition->getOption($name);
if (null === $value && $option->acceptParameter()) if (null === $value && $option->acceptParameter()) {
{
// if option accepts an optional or mandatory argument // if option accepts an optional or mandatory argument
// let's see if there is one provided // let's see if there is one provided
$next = array_shift($this->parsed); $next = array_shift($this->parsed);
if ('-' !== $next[0]) if ('-' !== $next[0]) {
{
$value = $next; $value = $next;
} } else {
else
{
array_unshift($this->parsed, $next); array_unshift($this->parsed, $next);
} }
} }
if (null === $value) if (null === $value) {
{ if ($option->isParameterRequired()) {
if ($option->isParameterRequired())
{
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name)); throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
} }
@ -243,10 +219,8 @@ class ArgvInput extends Input
*/ */
public function getFirstArgument() public function getFirstArgument()
{ {
foreach ($this->tokens as $token) foreach ($this->tokens as $token) {
{ if ($token && '-' === $token[0]) {
if ($token && '-' === $token[0])
{
continue; continue;
} }
@ -266,15 +240,12 @@ class ArgvInput extends Input
*/ */
public function hasParameterOption($values) public function hasParameterOption($values)
{ {
if (!is_array($values)) if (!is_array($values)) {
{
$values = array($values); $values = array($values);
} }
foreach ($this->tokens as $v) foreach ($this->tokens as $v) {
{ if (in_array($v, $values)) {
if (in_array($v, $values))
{
return true; return true;
} }
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -18,8 +18,6 @@ namespace Symfony\Component\Console\Input;
* *
* $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar')); * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class ArrayInput extends Input class ArrayInput extends Input
@ -46,10 +44,8 @@ class ArrayInput extends Input
*/ */
public function getFirstArgument() public function getFirstArgument()
{ {
foreach ($this->parameters as $key => $value) foreach ($this->parameters as $key => $value) {
{ if ($key && '-' === $key[0]) {
if ($key && '-' === $key[0])
{
continue; continue;
} }
@ -69,20 +65,16 @@ class ArrayInput extends Input
*/ */
public function hasParameterOption($values) public function hasParameterOption($values)
{ {
if (!is_array($values)) if (!is_array($values)) {
{
$values = array($values); $values = array($values);
} }
foreach ($this->parameters as $k => $v) foreach ($this->parameters as $k => $v) {
{ if (!is_int($k)) {
if (!is_int($k))
{
$v = $k; $v = $k;
} }
if (in_array($v, $values)) if (in_array($v, $values)) {
{
return true; return true;
} }
} }
@ -95,18 +87,12 @@ class ArrayInput extends Input
*/ */
protected function parse() protected function parse()
{ {
foreach ($this->parameters as $key => $value) foreach ($this->parameters as $key => $value) {
{ if ('--' === substr($key, 0, 2)) {
if ('--' === substr($key, 0, 2))
{
$this->addLongOption(substr($key, 2), $value); $this->addLongOption(substr($key, 2), $value);
} } elseif ('-' === $key[0]) {
elseif ('-' === $key[0])
{
$this->addShortOption(substr($key, 1), $value); $this->addShortOption(substr($key, 1), $value);
} } else {
else
{
$this->addArgument($key, $value); $this->addArgument($key, $value);
} }
} }
@ -117,12 +103,13 @@ class ArrayInput extends Input
* *
* @param string $shortcut The short option key * @param string $shortcut The short option key
* @param mixed $value The value for the option * @param mixed $value The value for the option
*
* @throws \RuntimeException When option given doesn't exist
*/ */
protected function addShortOption($shortcut, $value) protected function addShortOption($shortcut, $value)
{ {
if (!$this->definition->hasShortcut($shortcut)) if (!$this->definition->hasShortcut($shortcut)) {
{ throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
} }
$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
@ -133,21 +120,21 @@ class ArrayInput extends Input
* *
* @param string $name The long option key * @param string $name The long option key
* @param mixed $value The value for the option * @param mixed $value The value for the option
*
* @throws \InvalidArgumentException When option given doesn't exist
* @throws \InvalidArgumentException When a required value is missing
*/ */
protected function addLongOption($name, $value) protected function addLongOption($name, $value)
{ {
if (!$this->definition->hasOption($name)) if (!$this->definition->hasOption($name)) {
{ throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
} }
$option = $this->definition->getOption($name); $option = $this->definition->getOption($name);
if (null === $value) if (null === $value) {
{ if ($option->isParameterRequired()) {
if ($option->isParameterRequired()) throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
{
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
} }
$value = $option->isParameterOptional() ? $option->getDefault() : true; $value = $option->isParameterOptional() ? $option->getDefault() : true;
@ -161,12 +148,13 @@ class ArrayInput extends Input
* *
* @param string $name The argument name * @param string $name The argument name
* @param mixed $value The value for the argument * @param mixed $value The value for the argument
*
* @throws \InvalidArgumentException When argument given doesn't exist
*/ */
protected function addArgument($name, $value) protected function addArgument($name, $value)
{ {
if (!$this->definition->hasArgument($name)) if (!$this->definition->hasArgument($name)) {
{ throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
throw new \RuntimeException(sprintf('The "%s" argument does not exist.', $name));
} }
$this->arguments[$name] = $value; $this->arguments[$name] = $value;

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -20,8 +20,6 @@ namespace Symfony\Component\Console\Input;
* * `StringInput`: The input is provided as a string * * `StringInput`: The input is provided as a string
* * `ArrayInput`: The input is provided as an array * * `ArrayInput`: The input is provided as an array
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
abstract class Input implements InputInterface abstract class Input implements InputInterface
@ -38,12 +36,9 @@ abstract class Input implements InputInterface
*/ */
public function __construct(InputDefinition $definition = null) public function __construct(InputDefinition $definition = null)
{ {
if (null === $definition) if (null === $definition) {
{
$this->definition = new InputDefinition(); $this->definition = new InputDefinition();
} } else {
else
{
$this->bind($definition); $this->bind($definition);
$this->validate(); $this->validate();
} }
@ -68,10 +63,12 @@ abstract class Input implements InputInterface
*/ */
abstract protected function parse(); abstract protected function parse();
/**
* @throws \RuntimeException When not enough arguments are given
*/
public function validate() public function validate()
{ {
if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
{
throw new \RuntimeException('Not enough arguments.'); throw new \RuntimeException('Not enough arguments.');
} }
} }
@ -102,11 +99,12 @@ abstract class Input implements InputInterface
* @param string $name The argument name * @param string $name The argument name
* *
* @return mixed The argument value * @return mixed The argument value
*
* @throws \InvalidArgumentException When argument given doesn't exist
*/ */
public function getArgument($name) public function getArgument($name)
{ {
if (!$this->definition->hasArgument($name)) if (!$this->definition->hasArgument($name)) {
{
throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
} }
@ -118,11 +116,12 @@ abstract class Input implements InputInterface
* *
* @param string $name The argument name * @param string $name The argument name
* @param string $value The argument value * @param string $value The argument value
*
* @throws \InvalidArgumentException When argument given doesn't exist
*/ */
public function setArgument($name, $value) public function setArgument($name, $value)
{ {
if (!$this->definition->hasArgument($name)) if (!$this->definition->hasArgument($name)) {
{
throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
} }
@ -157,11 +156,12 @@ abstract class Input implements InputInterface
* @param string $name The option name * @param string $name The option name
* *
* @return mixed The option value * @return mixed The option value
*
* @throws \InvalidArgumentException When option given doesn't exist
*/ */
public function getOption($name) public function getOption($name)
{ {
if (!$this->definition->hasOption($name)) if (!$this->definition->hasOption($name)) {
{
throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
} }
@ -173,11 +173,12 @@ abstract class Input implements InputInterface
* *
* @param string $name The option name * @param string $name The option name
* @param string $value The option value * @param string $value The option value
*
* @throws \InvalidArgumentException When option given doesn't exist
*/ */
public function setOption($name, $value) public function setOption($name, $value)
{ {
if (!$this->definition->hasOption($name)) if (!$this->definition->hasOption($name)) {
{
throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Input;
/** /**
* Represents a command line argument. * Represents a command line argument.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class InputArgument class InputArgument
@ -36,15 +34,14 @@ class InputArgument
* @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
* @param string $description A description text * @param string $description A description text
* @param mixed $default The default value (for self::OPTIONAL mode only) * @param mixed $default The default value (for self::OPTIONAL mode only)
*
* @throws \InvalidArgumentException When argument mode is not valid
*/ */
public function __construct($name, $mode = null, $description = '', $default = null) public function __construct($name, $mode = null, $description = '', $default = null)
{ {
if (null === $mode) if (null === $mode) {
{
$mode = self::OPTIONAL; $mode = self::OPTIONAL;
} } else if (is_string($mode) || $mode > 7) {
else if (is_string($mode) || $mode > 7)
{
throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode)); throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
} }
@ -89,22 +86,19 @@ class InputArgument
* Sets the default value. * Sets the default value.
* *
* @param mixed $default The default value * @param mixed $default The default value
*
* @throws \LogicException When incorrect default value is given
*/ */
public function setDefault($default = null) public function setDefault($default = null)
{ {
if (self::REQUIRED === $this->mode && null !== $default) if (self::REQUIRED === $this->mode && null !== $default) {
{
throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.'); throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.');
} }
if ($this->isArray()) if ($this->isArray()) {
{ if (null === $default) {
if (null === $default)
{
$default = array(); $default = array();
} } else if (!is_array($default)) {
else if (!is_array($default))
{
throw new \LogicException('A default value for an array argument must be an array.'); throw new \LogicException('A default value for an array argument must be an array.');
} }
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -21,8 +21,6 @@ namespace Symfony\Component\Console\Input;
* new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED), * new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED),
* )); * ));
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class InputDefinition class InputDefinition
@ -48,14 +46,10 @@ class InputDefinition
{ {
$arguments = array(); $arguments = array();
$options = array(); $options = array();
foreach ($definition as $item) foreach ($definition as $item) {
{ if ($item instanceof InputOption) {
if ($item instanceof InputOption)
{
$options[] = $item; $options[] = $item;
} } else {
else
{
$arguments[] = $item; $arguments[] = $item;
} }
} }
@ -74,20 +68,19 @@ class InputDefinition
$this->arguments = array(); $this->arguments = array();
$this->requiredCount = 0; $this->requiredCount = 0;
$this->hasOptional = false; $this->hasOptional = false;
$this->hasAnArrayArgument = false;
$this->addArguments($arguments); $this->addArguments($arguments);
} }
/** /**
* Add an array of InputArgument objects. * Add an array of InputArgument objects.
* *
* @param array $arguments An array of InputArgument objects * @param InputArgument[] $arguments An array of InputArgument objects
*/ */
public function addArguments($arguments = array()) public function addArguments($arguments = array())
{ {
if (null !== $arguments) if (null !== $arguments) {
{ foreach ($arguments as $argument) {
foreach ($arguments as $argument)
{
$this->addArgument($argument); $this->addArgument($argument);
} }
} }
@ -97,35 +90,30 @@ class InputDefinition
* Add an InputArgument object. * Add an InputArgument object.
* *
* @param InputArgument $argument An InputArgument object * @param InputArgument $argument An InputArgument object
*
* @throws \LogicException When incorrect argument is given
*/ */
public function addArgument(InputArgument $argument) public function addArgument(InputArgument $argument)
{ {
if (isset($this->arguments[$argument->getName()])) if (isset($this->arguments[$argument->getName()])) {
{
throw new \LogicException(sprintf('An argument with name "%s" already exist.', $argument->getName())); throw new \LogicException(sprintf('An argument with name "%s" already exist.', $argument->getName()));
} }
if ($this->hasAnArrayArgument) if ($this->hasAnArrayArgument) {
{
throw new \LogicException('Cannot add an argument after an array argument.'); throw new \LogicException('Cannot add an argument after an array argument.');
} }
if ($argument->isRequired() && $this->hasOptional) if ($argument->isRequired() && $this->hasOptional) {
{
throw new \LogicException('Cannot add a required argument after an optional one.'); throw new \LogicException('Cannot add a required argument after an optional one.');
} }
if ($argument->isArray()) if ($argument->isArray()) {
{
$this->hasAnArrayArgument = true; $this->hasAnArrayArgument = true;
} }
if ($argument->isRequired()) if ($argument->isRequired()) {
{
++$this->requiredCount; ++$this->requiredCount;
} } else {
else
{
$this->hasOptional = true; $this->hasOptional = true;
} }
@ -138,13 +126,14 @@ class InputDefinition
* @param string|integer $name The InputArgument name or position * @param string|integer $name The InputArgument name or position
* *
* @return InputArgument An InputArgument object * @return InputArgument An InputArgument object
*
* @throws \InvalidArgumentException When argument given doesn't exist
*/ */
public function getArgument($name) public function getArgument($name)
{ {
$arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
if (!$this->hasArgument($name)) if (!$this->hasArgument($name)) {
{
throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
} }
@ -203,8 +192,7 @@ class InputDefinition
public function getArgumentDefaults() public function getArgumentDefaults()
{ {
$values = array(); $values = array();
foreach ($this->arguments as $argument) foreach ($this->arguments as $argument) {
{
$values[$argument->getName()] = $argument->getDefault(); $values[$argument->getName()] = $argument->getDefault();
} }
@ -226,12 +214,11 @@ class InputDefinition
/** /**
* Add an array of InputOption objects. * Add an array of InputOption objects.
* *
* @param array $options An array of InputOption objects * @param InputOption[] $options An array of InputOption objects
*/ */
public function addOptions($options = array()) public function addOptions($options = array())
{ {
foreach ($options as $option) foreach ($options as $option) {
{
$this->addOption($option); $this->addOption($option);
} }
} }
@ -240,21 +227,19 @@ class InputDefinition
* Add an InputOption object. * Add an InputOption object.
* *
* @param InputOption $option An InputOption object * @param InputOption $option An InputOption object
*
* @throws \LogicException When option given already exist
*/ */
public function addOption(InputOption $option) public function addOption(InputOption $option)
{ {
if (isset($this->options[$option->getName()])) if (isset($this->options[$option->getName()])) {
{
throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName())); throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName()));
} } else if (isset($this->shortcuts[$option->getShortcut()])) {
else if (isset($this->shortcuts[$option->getShortcut()]))
{
throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut())); throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut()));
} }
$this->options[$option->getName()] = $option; $this->options[$option->getName()] = $option;
if ($option->getShortcut()) if ($option->getShortcut()) {
{
$this->shortcuts[$option->getShortcut()] = $option->getName(); $this->shortcuts[$option->getShortcut()] = $option->getName();
} }
} }
@ -268,8 +253,7 @@ class InputDefinition
*/ */
public function getOption($name) public function getOption($name)
{ {
if (!$this->hasOption($name)) if (!$this->hasOption($name)) {
{
throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
} }
@ -328,8 +312,7 @@ class InputDefinition
public function getOptionDefaults() public function getOptionDefaults()
{ {
$values = array(); $values = array();
foreach ($this->options as $option) foreach ($this->options as $option) {
{
$values[$option->getName()] = $option->getDefault(); $values[$option->getName()] = $option->getDefault();
} }
@ -342,11 +325,12 @@ class InputDefinition
* @param string $shortcut The shortcut * @param string $shortcut The shortcut
* *
* @return string The InputOption name * @return string The InputOption name
*
* @throws \InvalidArgumentException When option given does not exist
*/ */
protected function shortcutToName($shortcut) protected function shortcutToName($shortcut)
{ {
if (!isset($this->shortcuts[$shortcut])) if (!isset($this->shortcuts[$shortcut])) {
{
throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
} }
@ -361,18 +345,15 @@ class InputDefinition
public function getSynopsis() public function getSynopsis()
{ {
$elements = array(); $elements = array();
foreach ($this->getOptions() as $option) foreach ($this->getOptions() as $option) {
{
$shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : ''; $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
$elements[] = sprintf('['.($option->isParameterRequired() ? '%s--%s="..."' : ($option->isParameterOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName()); $elements[] = sprintf('['.($option->isParameterRequired() ? '%s--%s="..."' : ($option->isParameterOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName());
} }
foreach ($this->getArguments() as $argument) foreach ($this->getArguments() as $argument) {
{
$elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : '')); $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : ''));
if ($argument->isArray()) if ($argument->isArray()) {
{
$elements[] = sprintf('... [%sN]', $argument->getName()); $elements[] = sprintf('... [%sN]', $argument->getName());
} }
} }
@ -389,29 +370,22 @@ class InputDefinition
{ {
// find the largest option or argument name // find the largest option or argument name
$max = 0; $max = 0;
foreach ($this->getOptions() as $option) foreach ($this->getOptions() as $option) {
{
$max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max; $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
} }
foreach ($this->getArguments() as $argument) foreach ($this->getArguments() as $argument) {
{
$max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max; $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
} }
++$max; ++$max;
$text = array(); $text = array();
if ($this->getArguments()) if ($this->getArguments()) {
{
$text[] = '<comment>Arguments:</comment>'; $text[] = '<comment>Arguments:</comment>';
foreach ($this->getArguments() as $argument) foreach ($this->getArguments() as $argument) {
{ if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault())))
{
$default = sprintf('<comment> (default: %s)</comment>', is_array($argument->getDefault()) ? str_replace("\n", '', var_export($argument->getDefault(), true)): $argument->getDefault()); $default = sprintf('<comment> (default: %s)</comment>', is_array($argument->getDefault()) ? str_replace("\n", '', var_export($argument->getDefault(), true)): $argument->getDefault());
} } else {
else
{
$default = ''; $default = '';
} }
@ -421,18 +395,13 @@ class InputDefinition
$text[] = ''; $text[] = '';
} }
if ($this->getOptions()) if ($this->getOptions()) {
{
$text[] = '<comment>Options:</comment>'; $text[] = '<comment>Options:</comment>';
foreach ($this->getOptions() as $option) foreach ($this->getOptions() as $option) {
{ if ($option->acceptParameter() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
if ($option->acceptParameter() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault())))
{
$default = sprintf('<comment> (default: %s)</comment>', is_array($option->getDefault()) ? str_replace("\n", '', print_r($option->getDefault(), true)): $option->getDefault()); $default = sprintf('<comment> (default: %s)</comment>', is_array($option->getDefault()) ? str_replace("\n", '', print_r($option->getDefault(), true)): $option->getDefault());
} } else {
else
{
$default = ''; $default = '';
} }
@ -460,8 +429,7 @@ class InputDefinition
$dom->appendChild($definitionXML = $dom->createElement('definition')); $dom->appendChild($definitionXML = $dom->createElement('definition'));
$definitionXML->appendChild($argumentsXML = $dom->createElement('arguments')); $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
foreach ($this->getArguments() as $argument) foreach ($this->getArguments() as $argument) {
{
$argumentsXML->appendChild($argumentXML = $dom->createElement('argument')); $argumentsXML->appendChild($argumentXML = $dom->createElement('argument'));
$argumentXML->setAttribute('name', $argument->getName()); $argumentXML->setAttribute('name', $argument->getName());
$argumentXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0); $argumentXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
@ -471,16 +439,14 @@ class InputDefinition
$argumentXML->appendChild($defaultsXML = $dom->createElement('defaults')); $argumentXML->appendChild($defaultsXML = $dom->createElement('defaults'));
$defaults = is_array($argument->getDefault()) ? $argument->getDefault() : ($argument->getDefault() ? array($argument->getDefault()) : array()); $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : ($argument->getDefault() ? array($argument->getDefault()) : array());
foreach ($defaults as $default) foreach ($defaults as $default) {
{
$defaultsXML->appendChild($defaultXML = $dom->createElement('default')); $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
$defaultXML->appendChild($dom->createTextNode($default)); $defaultXML->appendChild($dom->createTextNode($default));
} }
} }
$definitionXML->appendChild($optionsXML = $dom->createElement('options')); $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
foreach ($this->getOptions() as $option) foreach ($this->getOptions() as $option) {
{
$optionsXML->appendChild($optionXML = $dom->createElement('option')); $optionsXML->appendChild($optionXML = $dom->createElement('option'));
$optionXML->setAttribute('name', '--'.$option->getName()); $optionXML->setAttribute('name', '--'.$option->getName());
$optionXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : ''); $optionXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
@ -490,12 +456,10 @@ class InputDefinition
$optionXML->appendChild($descriptionXML = $dom->createElement('description')); $optionXML->appendChild($descriptionXML = $dom->createElement('description'));
$descriptionXML->appendChild($dom->createTextNode($option->getDescription())); $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
if ($option->acceptParameter()) if ($option->acceptParameter()) {
{
$optionXML->appendChild($defaultsXML = $dom->createElement('defaults')); $optionXML->appendChild($defaultsXML = $dom->createElement('defaults'));
$defaults = is_array($option->getDefault()) ? $option->getDefault() : ($option->getDefault() ? array($option->getDefault()) : array()); $defaults = is_array($option->getDefault()) ? $option->getDefault() : ($option->getDefault() ? array($option->getDefault()) : array());
foreach ($defaults as $default) foreach ($defaults as $default) {
{
$defaultsXML->appendChild($defaultXML = $dom->createElement('default')); $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
$defaultXML->appendChild($dom->createTextNode($default)); $defaultXML->appendChild($dom->createTextNode($default));
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Input;
/** /**
* InputInterface is the interface implemented by all input classes. * InputInterface is the interface implemented by all input classes.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
interface InputInterface interface InputInterface

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Input;
/** /**
* Represents a command line option. * Represents a command line option.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class InputOption class InputOption
@ -39,33 +37,28 @@ class InputOption
* @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL * @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL
* @param string $description A description text * @param string $description A description text
* @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE) * @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE)
*
* @throws \InvalidArgumentException If option mode is invalid or incompatible
*/ */
public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null) public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
{ {
if ('--' === substr($name, 0, 2)) if ('--' === substr($name, 0, 2)) {
{
$name = substr($name, 2); $name = substr($name, 2);
} }
if (empty($shortcut)) if (empty($shortcut)) {
{
$shortcut = null; $shortcut = null;
} }
if (null !== $shortcut) if (null !== $shortcut) {
{ if ('-' === $shortcut[0]) {
if ('-' === $shortcut[0])
{
$shortcut = substr($shortcut, 1); $shortcut = substr($shortcut, 1);
} }
} }
if (null === $mode) if (null === $mode) {
{
$mode = self::PARAMETER_NONE; $mode = self::PARAMETER_NONE;
} } else if (!is_int($mode) || $mode > 15) {
else if (!is_int($mode) || $mode > 15)
{
throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
} }
@ -74,8 +67,7 @@ class InputOption
$this->mode = $mode; $this->mode = $mode;
$this->description = $description; $this->description = $description;
if ($this->isArray() && !$this->acceptParameter()) if ($this->isArray() && !$this->acceptParameter()) {
{
throw new \InvalidArgumentException('Impossible to have an option mode PARAMETER_IS_ARRAY if the option does not accept a parameter.'); throw new \InvalidArgumentException('Impossible to have an option mode PARAMETER_IS_ARRAY if the option does not accept a parameter.');
} }
@ -149,19 +141,14 @@ class InputOption
*/ */
public function setDefault($default = null) public function setDefault($default = null)
{ {
if (self::PARAMETER_NONE === (self::PARAMETER_NONE & $this->mode) && null !== $default) if (self::PARAMETER_NONE === (self::PARAMETER_NONE & $this->mode) && null !== $default) {
{
throw new \LogicException('Cannot set a default value when using Option::PARAMETER_NONE mode.'); throw new \LogicException('Cannot set a default value when using Option::PARAMETER_NONE mode.');
} }
if ($this->isArray()) if ($this->isArray()) {
{ if (null === $default) {
if (null === $default)
{
$default = array(); $default = array();
} } elseif (!is_array($default)) {
elseif (!is_array($default))
{
throw new \LogicException('A default value for an array option must be an array.'); throw new \LogicException('A default value for an array option must be an array.');
} }
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Input; namespace Symfony\Component\Console\Input;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -18,8 +18,6 @@ namespace Symfony\Component\Console\Input;
* *
* $input = new StringInput('foo --bar="foobar"'); * $input = new StringInput('foo --bar="foobar"');
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class StringInput extends ArgvInput class StringInput extends ArgvInput
@ -40,6 +38,9 @@ class StringInput extends ArgvInput
$this->tokens = $this->tokenize($input); $this->tokens = $this->tokenize($input);
} }
/**
* @throws \InvalidArgumentException When unable to parse input (should never happen)
*/
protected function tokenize($input) protected function tokenize($input)
{ {
$input = preg_replace('/(\r\n|\r|\n|\t)/', ' ', $input); $input = preg_replace('/(\r\n|\r|\n|\t)/', ' ', $input);
@ -47,25 +48,15 @@ class StringInput extends ArgvInput
$tokens = array(); $tokens = array();
$length = strlen($input); $length = strlen($input);
$cursor = 0; $cursor = 0;
while ($cursor < $length) while ($cursor < $length) {
{ if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
if (preg_match('/\s+/A', $input, $match, null, $cursor)) } elseif (preg_match('/([^="\' ]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
{
}
elseif (preg_match('/([^="\' ]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor))
{
$tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2))); $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
} } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor))
{
$tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2)); $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
} } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor))
{
$tokens[] = stripcslashes($match[1]); $tokens[] = stripcslashes($match[1]);
} } else {
else
{
// should never happen // should never happen
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10))); throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Output; namespace Symfony\Component\Console\Output;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -22,8 +22,6 @@ namespace Symfony\Component\Console\Output;
* *
* $output = new StreamOutput(fopen('php://stdout', 'w')); * $output = new StreamOutput(fopen('php://stdout', 'w'));
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class ConsoleOutput extends StreamOutput class ConsoleOutput extends StreamOutput

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Output; namespace Symfony\Component\Console\Output;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -16,8 +16,6 @@ namespace Symfony\Component\Console\Output;
* *
* $output = new NullOutput(); * $output = new NullOutput();
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class NullOutput extends Output class NullOutput extends Output

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Output; namespace Symfony\Component\Console\Output;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -20,8 +20,6 @@ namespace Symfony\Component\Console\Output;
* * verbose: -v (more output - debug) * * verbose: -v (more output - debug)
* * quiet: -q (no output) * * quiet: -q (no output)
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
abstract class Output implements OutputInterface abstract class Output implements OutputInterface
@ -127,23 +125,21 @@ abstract class Output implements OutputInterface
* @param string|array $messages The message as an array of lines of a single string * @param string|array $messages The message as an array of lines of a single string
* @param Boolean $newline Whether to add a newline or not * @param Boolean $newline Whether to add a newline or not
* @param integer $type The type of output * @param integer $type The type of output
*
* @throws \InvalidArgumentException When unknown output type is given
*/ */
public function write($messages, $newline = false, $type = 0) public function write($messages, $newline = false, $type = 0)
{ {
if (self::VERBOSITY_QUIET === $this->verbosity) if (self::VERBOSITY_QUIET === $this->verbosity) {
{
return; return;
} }
if (!is_array($messages)) if (!is_array($messages)) {
{
$messages = array($messages); $messages = array($messages);
} }
foreach ($messages as $message) foreach ($messages as $message) {
{ switch ($type) {
switch ($type)
{
case Output::OUTPUT_NORMAL: case Output::OUTPUT_NORMAL:
$message = $this->format($message); $message = $this->format($message);
break; break;
@ -177,51 +173,56 @@ abstract class Output implements OutputInterface
*/ */
protected function format($message) protected function format($message)
{ {
$message = preg_replace_callback('#<([a-z][a-z0-9\-_]+)>#i', array($this, 'replaceStartStyle'), $message); $message = preg_replace_callback('#<([a-z][a-z0-9\-_=;]+)>#i', array($this, 'replaceStartStyle'), $message);
return preg_replace_callback('#</([a-z][a-z0-9\-_]+)>#i', array($this, 'replaceEndStyle'), $message); return preg_replace_callback('#</([a-z][a-z0-9\-_]*)?>#i', array($this, 'replaceEndStyle'), $message);
} }
/**
* @throws \InvalidArgumentException When style is unknown
*/
protected function replaceStartStyle($match) protected function replaceStartStyle($match)
{ {
if (!$this->decorated) if (!$this->decorated) {
{
return ''; return '';
} }
if (!isset(static::$styles[strtolower($match[1])])) if (isset(static::$styles[strtolower($match[1])])) {
{ $parameters = static::$styles[strtolower($match[1])];
} else {
// bg=blue;fg=red
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($match[1]), $matches, PREG_SET_ORDER)) {
throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1])); throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
} }
$parameters = static::$styles[strtolower($match[1])]; $parameters = array();
foreach ($matches as $match) {
$parameters[$match[1]] = $match[2];
}
}
$codes = array(); $codes = array();
if (isset($parameters['fg'])) if (isset($parameters['fg'])) {
{
$codes[] = static::$foreground[$parameters['fg']]; $codes[] = static::$foreground[$parameters['fg']];
} }
if (isset($parameters['bg'])) if (isset($parameters['bg'])) {
{
$codes[] = static::$background[$parameters['bg']]; $codes[] = static::$background[$parameters['bg']];
} }
foreach (static::$options as $option => $value) foreach (static::$options as $option => $value) {
{ if (isset($parameters[$option]) && $parameters[$option]) {
if (isset($parameters[$option]) && $parameters[$option])
{
$codes[] = $value; $codes[] = $value;
} }
} }
return "\033[".implode(';', $codes)."m"; return "\033[".implode(';', $codes).'m';
} }
protected function replaceEndStyle($match) protected function replaceEndStyle($match)
{ {
if (!$this->decorated) if (!$this->decorated) {
{
return ''; return '';
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Output; namespace Symfony\Component\Console\Output;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Output;
/** /**
* OutputInterface is the interface implemented by all Output classes. * OutputInterface is the interface implemented by all Output classes.
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
interface OutputInterface interface OutputInterface
@ -24,21 +22,24 @@ interface OutputInterface
* Writes a message to the output. * Writes a message to the output.
* *
* @param string|array $messages The message as an array of lines of a single string * @param string|array $messages The message as an array of lines of a single string
* @param Boolean $newline Whether to add a newline or not
* @param integer $type The type of output * @param integer $type The type of output
*
* @throws \InvalidArgumentException When unknown output type is given
*/ */
public function write($messages, $type = 0); function write($messages, $newline = false, $type = 0);
/** /**
* Sets the verbosity of the output. * Sets the verbosity of the output.
* *
* @param integer $level The level of verbosity * @param integer $level The level of verbosity
*/ */
public function setVerbosity($level); function setVerbosity($level);
/** /**
* Sets the decorated flag. * Sets the decorated flag.
* *
* @param Boolean $decorated Whether to decorated the messages or not * @param Boolean $decorated Whether to decorated the messages or not
*/ */
public function setDecorated($decorated); function setDecorated($decorated);
} }

View File

@ -3,7 +3,7 @@
namespace Symfony\Component\Console\Output; namespace Symfony\Component\Console\Output;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -22,8 +22,6 @@ namespace Symfony\Component\Console\Output;
* *
* $output = new StreamOutput(fopen('/path/to/output.log', 'a', false)); * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
* *
* @package symfony
* @subpackage console
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class StreamOutput extends Output class StreamOutput extends Output
@ -36,18 +34,18 @@ class StreamOutput extends Output
* @param mixed $stream A stream resource * @param mixed $stream A stream resource
* @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE) * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
* @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing) * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
*
* @throws \InvalidArgumentException When first argument is not a real stream
*/ */
public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null) public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null)
{ {
if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
{
throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.'); throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
} }
$this->stream = $stream; $this->stream = $stream;
if (null === $decorated) if (null === $decorated) {
{
$decorated = $this->hasColorSupport($decorated); $decorated = $this->hasColorSupport($decorated);
} }
@ -69,11 +67,12 @@ class StreamOutput extends Output
* *
* @param string $message A message to write to the output * @param string $message A message to write to the output
* @param Boolean $newline Whether to add a newline or not * @param Boolean $newline Whether to add a newline or not
*
* @throws \RuntimeException When unable to write output (should never happen)
*/ */
public function doWrite($message, $newline) public function doWrite($message, $newline)
{ {
if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
{
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
// should never happen // should never happen
throw new \RuntimeException('Unable to write output.'); throw new \RuntimeException('Unable to write output.');
@ -96,12 +95,9 @@ class StreamOutput extends Output
protected function hasColorSupport() protected function hasColorSupport()
{ {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
if (DIRECTORY_SEPARATOR == '\\') if (DIRECTORY_SEPARATOR == '\\') {
{
return false !== getenv('ANSICON'); return false !== getenv('ANSICON');
} } else {
else
{
return function_exists('posix_isatty') && @posix_isatty($this->stream); return function_exists('posix_isatty') && @posix_isatty($this->stream);
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd

View File

@ -7,7 +7,7 @@ use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\ConsoleOutput;
/* /*
* This file is part of the symfony framework. * This file is part of the Symfony framework.
* *
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* *
@ -21,8 +21,6 @@ use Symfony\Component\Console\Output\ConsoleOutput;
* This class only works with a PHP compiled with readline support * This class only works with a PHP compiled with readline support
* (either --with-readline or --with-libedit) * (either --with-readline or --with-libedit)
* *
* @package symfony
* @subpackage cli
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class Shell class Shell
@ -38,11 +36,12 @@ class Shell
* a \RuntimeException exception is thrown. * a \RuntimeException exception is thrown.
* *
* @param Application $application An application instance * @param Application $application An application instance
*
* @throws \RuntimeException When Readline extension is not enabled
*/ */
public function __construct(Application $application) public function __construct(Application $application)
{ {
if (!function_exists('readline')) if (!function_exists('readline')) {
{
throw new \RuntimeException('Unable to start the shell as the Readline extension is not enabled.'); throw new \RuntimeException('Unable to start the shell as the Readline extension is not enabled.');
} }
@ -63,12 +62,10 @@ class Shell
readline_completion_function(array($this, 'autocompleter')); readline_completion_function(array($this, 'autocompleter'));
$this->output->writeln($this->getHeader()); $this->output->writeln($this->getHeader());
while (true) while (true) {
{
$command = readline($this->application->getName().' > '); $command = readline($this->application->getName().' > ');
if (false === $command) if (false === $command) {
{
$this->output->writeln("\n"); $this->output->writeln("\n");
break; break;
@ -77,8 +74,7 @@ class Shell
readline_add_history($command); readline_add_history($command);
readline_write_history($this->history); readline_write_history($this->history);
if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) {
{
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret)); $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
} }
} }
@ -95,30 +91,24 @@ class Shell
$info = readline_info(); $info = readline_info();
$text = substr($info['line_buffer'], 0, $info['end']); $text = substr($info['line_buffer'], 0, $info['end']);
if ($info['point'] !== $info['end']) if ($info['point'] !== $info['end']) {
{
return true; return true;
} }
// task name? // task name?
if (false === strpos($text, ' ') || !$text) if (false === strpos($text, ' ') || !$text) {
{
return array_keys($this->application->getCommands()); return array_keys($this->application->getCommands());
} }
// options and arguments? // options and arguments?
try try {
{
$command = $this->application->findCommand(substr($text, 0, strpos($text, ' '))); $command = $this->application->findCommand(substr($text, 0, strpos($text, ' ')));
} } catch (\Exception $e) {
catch (\Exception $e)
{
return true; return true;
} }
$list = array('--help'); $list = array('--help');
foreach ($command->getDefinition()->getOptions() as $option) foreach ($command->getDefinition()->getOptions() as $option) {
{
$list[] = '--'.$option->getName(); $list[] = '--'.$option->getName();
} }

View File

@ -6,6 +6,18 @@ use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Output\StreamOutput;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class ApplicationTester class ApplicationTester
{ {
protected $application; protected $application;
@ -38,18 +50,15 @@ class ApplicationTester
public function run(array $input, $options = array()) public function run(array $input, $options = array())
{ {
$this->input = new ArrayInput($input); $this->input = new ArrayInput($input);
if (isset($options['interactive'])) if (isset($options['interactive'])) {
{
$this->input->setInteractive($options['interactive']); $this->input->setInteractive($options['interactive']);
} }
$this->output = new StreamOutput(fopen('php://memory', 'w', false)); $this->output = new StreamOutput(fopen('php://memory', 'w', false));
if (isset($options['decorated'])) if (isset($options['decorated'])) {
{
$this->output->setDecorated($options['decorated']); $this->output->setDecorated($options['decorated']);
} }
if (isset($options['verbosity'])) if (isset($options['verbosity'])) {
{
$this->output->setVerbosity($options['verbosity']); $this->output->setVerbosity($options['verbosity']);
} }

View File

@ -6,6 +6,18 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Output\StreamOutput;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class CommandTester class CommandTester
{ {
protected $command; protected $command;
@ -37,19 +49,16 @@ class CommandTester
*/ */
public function execute(array $input, array $options = array()) public function execute(array $input, array $options = array())
{ {
$this->input = new ArrayInput(array_merge($input, array('command' => $this->command->getFullName()))); $this->input = new ArrayInput($input);
if (isset($options['interactive'])) if (isset($options['interactive'])) {
{
$this->input->setInteractive($options['interactive']); $this->input->setInteractive($options['interactive']);
} }
$this->output = new StreamOutput(fopen('php://memory', 'w', false)); $this->output = new StreamOutput(fopen('php://memory', 'w', false));
if (isset($options['decorated'])) if (isset($options['decorated'])) {
{
$this->output->setDecorated($options['decorated']); $this->output->setDecorated($options['decorated']);
} }
if (isset($options['verbosity'])) if (isset($options['verbosity'])) {
{
$this->output->setVerbosity($options['verbosity']); $this->output->setVerbosity($options['verbosity']);
} }