Skip to content

Hide commands from ApplicationDescriptor, but allow invoking #20029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 5, 2016
21 changes: 21 additions & 0 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Command
private $processTitle;
private $aliases = array();
private $definition;
private $public = true;
private $help;
private $description;
private $ignoreValidationErrors = false;
Expand Down Expand Up @@ -446,6 +447,26 @@ public function getName()
return $this->name;
}

/**
* @param bool $public Whether the command should be publicly shown or not.
*
* @return Command The current instance
*/
public function setPublic($public)
Copy link
Member

Choose a reason for hiding this comment

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

What about setPrivate() without an argument? I don't see a need to switch from private to public.

Copy link
Member

Choose a reason for hiding this comment

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

I suggested changing it to setPublic(false) to mimic Symfony services ... where you must set public: false for private services. If we use opposing techniques to do the same (public: false for services, private: true for commands) we're increasing the learning curve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we could meet in the middle at setPublicity as it's not favoring one way or another :)

{
$this->public = (bool) $public;

return $this;
}

/**
* @return bool Whether the command should be publicly shown or not.
*/
public function isPublic()
{
return $this->public;
}

/**
* Sets the description for the command.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function inspectApplication()

/** @var Command $command */
foreach ($commands as $name => $command) {
if (!$command->getName()) {
if (!$command->getName() || !$command->isPublic()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public function __construct()
parent::__construct('My Symfony application', 'v1.0');
$this->add(new DescriptorCommand1());
$this->add(new DescriptorCommand2());
$this->add(new DescriptorCommand3());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Tests\Fixtures;

use Symfony\Component\Console\Command\Command;

class DescriptorCommand3 extends Command
{
protected function configure()
{
$this
->setName('descriptor:command3')
->setDescription('command 3 description')
->setHelp('command 3 help')
->setPublic(false)
;
}
}