*/
class Command
{
+ /**
+ * @var string|null The default command name
+ */
+ protected static $defaultName;
+
private $application;
private $name;
private $processTitle;
private $helperSet;
/**
- * Constructor.
- *
+ * @return string|null The default command name or null when no default name is set
+ */
+ public static function getDefaultName()
+ {
+ $class = get_called_class();
+ $r = new \ReflectionProperty($class, 'defaultName');
+
+ return $class === $r->class ? static::$defaultName : null;
+ }
+
+ /**
* @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
{
$this->definition = new InputDefinition();
- if (null !== $name) {
+ if (null !== $name || null !== $name = static::getDefaultName()) {
$this->setName($name);
}
$this->configure();
-
- if (!$this->name) {
- throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
- }
}
/**
$this->ignoreValidationErrors = true;
}
- /**
- * Sets the application instance for this command.
- *
- * @param Application $application An Application instance
- */
public function setApplication(Application $application = null)
{
$this->application = $application;
}
}
- /**
- * Sets the helper set.
- *
- * @param HelperSet $helperSet A HelperSet instance
- */
public function setHelperSet(HelperSet $helperSet)
{
$this->helperSet = $helperSet;
* execute() method, you set the code to execute by passing
* a Closure to the setCode() method.
*
- * @param InputInterface $input An InputInterface instance
- * @param OutputInterface $output An OutputInterface instance
- *
* @return null|int null or 0 if everything went fine, or an error code
*
* @throws LogicException When this abstract method is not implemented
* This method is executed before the InputDefinition is validated.
* This means that this is the only place where the command can
* interactively ask for values of missing required arguments.
- *
- * @param InputInterface $input An InputInterface instance
- * @param OutputInterface $output An OutputInterface instance
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
*
* This is mainly useful when a lot of commands extends one main command
* where some things need to be initialized based on the input arguments and options.
- *
- * @param InputInterface $input An InputInterface instance
- * @param OutputInterface $output An OutputInterface instance
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
* setCode() method or by overriding the execute() method
* in a sub-class.
*
- * @param InputInterface $input An InputInterface instance
- * @param OutputInterface $output An OutputInterface instance
- *
* @return int The command exit code
*
* @throws \Exception When binding input fails. Bypass this by calling {@link ignoreValidationErrors()}.
if (null !== $this->processTitle) {
if (function_exists('cli_set_process_title')) {
- if (false === @cli_set_process_title($this->processTitle)) {
+ if (!@cli_set_process_title($this->processTitle)) {
if ('Darwin' === PHP_OS) {
$output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>');
} else {
- $error = error_get_last();
- trigger_error($error['message'], E_USER_WARNING);
+ cli_set_process_title($this->processTitle);
}
}
} elseif (function_exists('setproctitle')) {
}
/**
- * @return bool Whether the command should be publicly shown or not.
+ * @return bool whether the command should be publicly shown or not
*/
public function isHidden()
{