Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -392,7 +394,7 @@ public function add(Command $command)
}

if (null === $command->getDefinition()) {
throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
}

$this->commands[$command->getName()] = $command;
Expand All @@ -411,14 +413,14 @@ public function add(Command $command)
*
* @return Command A Command object
*
* @throws \InvalidArgumentException When command name given does not exist
* @throws CommandNotFoundException When command name given does not exist
*
* @api
*/
public function get($name)
{
if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
}

$command = $this->commands[$name];
Expand Down Expand Up @@ -477,7 +479,7 @@ public function getNamespaces()
*
* @return string A registered namespace
*
* @throws \InvalidArgumentException When namespace is incorrect or ambiguous
* @throws CommandNotFoundException When namespace is incorrect or ambiguous
*/
public function findNamespace($namespace)
{
Expand All @@ -498,12 +500,12 @@ public function findNamespace($namespace)
$message .= implode("\n ", $alternatives);
}

throw new \InvalidArgumentException($message);
throw new CommandNotFoundException($message, $alternatives);
}

$exact = in_array($namespace, $namespaces, true);
if (count($namespaces) > 1 && !$exact) {
throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))));
throw new CommandNotFoundException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces));
}

return $exact ? $namespace : reset($namespaces);
Expand All @@ -519,7 +521,7 @@ public function findNamespace($namespace)
*
* @return Command A Command instance
*
* @throws \InvalidArgumentException When command name is incorrect or ambiguous
* @throws CommandNotFoundException When command name is incorrect or ambiguous
*
* @api
*/
Expand All @@ -546,7 +548,7 @@ public function find($name)
$message .= implode("\n ", $alternatives);
}

throw new \InvalidArgumentException($message);
throw new CommandNotFoundException($message, $alternatives);
}

// filter out aliases for commands which are already on the list
Expand All @@ -563,7 +565,7 @@ public function find($name)
if (count($commands) > 1 && !$exact) {
$suggestions = $this->getAbbreviationSuggestions(array_values($commands));

throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));
throw new CommandNotFoundException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions), array_values($commands));
}

return $this->get($exact ? $name : reset($commands));
Expand Down
26 changes: 14 additions & 12 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;

/**
* Base class for all commands.
Expand Down Expand Up @@ -51,7 +53,7 @@ class Command
*
* @param string|null $name The name of the command; passing null means it must be set in configure()
*
* @throws \LogicException When the command name is empty
* @throws LogicException When the command name is empty
*
* @api
*/
Expand All @@ -66,7 +68,7 @@ public function __construct($name = null)
$this->configure();

if (!$this->name) {
throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
}
}

Expand Down Expand Up @@ -162,13 +164,13 @@ protected function configure()
*
* @return null|int null or 0 if everything went fine, or an error code
*
* @throws \LogicException When this abstract method is not implemented
* @throws LogicException When this abstract method is not implemented
*
* @see setCode()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
throw new \LogicException('You must override the execute() method in the concrete command class.');
throw new LogicException('You must override the execute() method in the concrete command class.');
}

/**
Expand Down Expand Up @@ -272,7 +274,7 @@ public function run(InputInterface $input, OutputInterface $output)
*
* @return Command The current instance
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*
* @see execute()
*
Expand All @@ -281,7 +283,7 @@ public function run(InputInterface $input, OutputInterface $output)
public function setCode($code)
{
if (!is_callable($code)) {
throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.');
throw new InvalidArgumentException('Invalid callable provided to Command::setCode.');
}

if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
Expand Down Expand Up @@ -423,7 +425,7 @@ public function addOption($name, $shortcut = null, $mode = null, $description =
*
* @return Command The current instance
*
* @throws \InvalidArgumentException When the name is invalid
* @throws InvalidArgumentException When the name is invalid
*
* @api
*/
Expand Down Expand Up @@ -552,14 +554,14 @@ public function getProcessedHelp()
*
* @return Command The current instance
*
* @throws \InvalidArgumentException When an alias is invalid
* @throws InvalidArgumentException When an alias is invalid
*
* @api
*/
public function setAliases($aliases)
{
if (!is_array($aliases) && !$aliases instanceof \Traversable) {
throw new \InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
}

foreach ($aliases as $alias) {
Expand Down Expand Up @@ -634,7 +636,7 @@ public function getUsages()
*
* @return mixed The helper value
*
* @throws \InvalidArgumentException if the helper is not defined
* @throws InvalidArgumentException if the helper is not defined
*
* @api
*/
Expand Down Expand Up @@ -693,12 +695,12 @@ public function asXml($asDom = false)
*
* @param string $name
*
* @throws \InvalidArgumentException When the name is invalid
* @throws InvalidArgumentException When the name is invalid
*/
private function validateName($name)
{
if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\CommandNotFoundException;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
Expand Down Expand Up @@ -89,12 +90,12 @@ public function getCommands()
*
* @return Command
*
* @throws \InvalidArgumentException
* @throws CommandNotFoundException
*/
public function getCommand($name)
{
if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
throw new \InvalidArgumentException(sprintf('Command %s does not exist.', $name));
throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name));
}

return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Descriptor/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
Expand Down Expand Up @@ -54,7 +55,7 @@ public function describe(OutputInterface $output, $object, array $options = arra
$this->describeApplication($object, $options);
break;
default:
throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
throw new InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Exception;

/**
* Represents an incorrect command name typed in the console.
*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class CommandNotFoundException extends \InvalidArgumentException implements ExceptionInterface
{
private $alternatives;

/**
* @param string $message Exception message to throw.
* @param array $alternatives List of similar defined names.
* @param int $code Exception code.
* @param Exception $previous previous exception used for the exception chaining.
*/
public function __construct($message, array $alternatives = array(), $code = 0, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);

$this->alternatives = $alternatives;
}

/**
* @return array A list of similar defined names.
*/
public function getAlternatives()
{
return $this->alternatives;
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Component/Console/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Exception;

/**
* ExceptionInterface.
*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*
* @api
*/
interface ExceptionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the interface? And do we need to tag it with as an @api interface given that the exception class are not tagged this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be consistent with the way we manage exception in the other components. We can indeed remove the @api tag but I'm not sure this will ever change.

{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Exception;

/**
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Console/Exception/InvalidOptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Exception;

/**
* Represents an incorrect option name typed in the console.
*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class InvalidOptionException extends \InvalidArgumentException implements ExceptionInterface
{
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Exception;

/**
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class LogicException extends \LogicException implements ExceptionInterface
{
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Exception;

/**
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
Loading