Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] Do not leak hidden console commands #33412

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 1 commit into from
Sep 28, 2019
Merged
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
[Console] Deprecate abbreviating hidden command names using Applicati…
…on->find()
  • Loading branch information
m-vo authored and chalasr committed Sep 28, 2019
commit f3406338e65182e5c6d096e83593877163efad69
5 changes: 5 additions & 0 deletions 5 UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Cache

* Added argument `$prefix` to `AdapterInterface::clear()`

Console
-------

* Deprecated finding hidden commands using an abbreviation, use the full name instead

Debug
-----

Expand Down
1 change: 1 addition & 0 deletions 1 UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Config
Console
-------

* Removed support for finding hidden commands using an abbreviation, use the full name instead
* Removed the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.
* Removed the `setHorizontalBorderChar()` method in favor of the `setDefaultCrossingChars()` method in `TableStyle`.
* Removed the `getHorizontalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
Expand Down
19 changes: 15 additions & 4 deletions 19 src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,25 +692,36 @@ public function find($name)
foreach ($abbrevs as $abbrev) {
$maxLen = max(Helper::strlen($abbrev), $maxLen);
}
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen, &$commands) {
if (!$commandList[$cmd] instanceof Command) {
$commandList[$cmd] = $this->commandLoader->get($cmd);
}

if ($commandList[$cmd]->isHidden()) {
unset($commands[array_search($cmd, $commands)]);

return false;
}

$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();

return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
}, array_values($commands));
$suggestions = $this->getAbbreviationSuggestions(array_filter($abbrevs));

throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
if (\count($commands) > 1) {
$suggestions = $this->getAbbreviationSuggestions(array_filter($abbrevs));

throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
}
}

return $this->get(reset($commands));
$command = $this->get(reset($commands));

if ($command->isHidden()) {
@trigger_error(sprintf('Command "%s" is hidden, finding it using an abbreviation is deprecated since Symfony 4.4, use its full name instead.', $command->getName()), E_USER_DEPRECATED);
}

return $command;
}

/**
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* deprecated finding hidden commands using an abbreviation, use the full name instead
* added `Question::setTrimmable` default to true to allow the answer to be trimmed
* added method `preventRedrawFasterThan()` and `forceRedrawSlowerThan()` on `ProgressBar`
* `Application` implements `ResetInterface`
Expand Down
54 changes: 54 additions & 0 deletions 54 src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static function setUpBeforeClass(): void
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
require_once self::$fixturesPath.'/FooHiddenCommand.php';
require_once self::$fixturesPath.'/BarHiddenCommand.php';
}

protected function normalizeLineBreaks($text)
Expand Down Expand Up @@ -441,6 +442,16 @@ public function provideAmbiguousAbbreviations()
];
}

public function testFindWithAmbiguousAbbreviationsFindsCommandIfAlternativesAreHidden()
{
$application = new Application();

$application->add(new \FooCommand());
$application->add(new \FooHiddenCommand());

$this->assertInstanceOf('FooCommand', $application->find('foo:'));
}

public function testFindCommandEqualNamespace()
{
$application = new Application();
Expand Down Expand Up @@ -708,6 +719,49 @@ public function testFindWithDoubleColonInNameThrowsException()
$application->find('foo::bar');
}

public function testFindHiddenWithExactName()
{
$application = new Application();
$application->add(new \FooHiddenCommand());

$this->assertInstanceOf('FooHiddenCommand', $application->find('foo:hidden'));
$this->assertInstanceOf('FooHiddenCommand', $application->find('afoohidden'));
}

/**
* @group legacy
* @expectedDeprecation Command "%s:hidden" is hidden, finding it using an abbreviation is deprecated since Symfony 4.4, use its full name instead.
* @dataProvider provideAbbreviationsForHiddenCommands
*/
public function testFindHiddenWithAbbreviatedName($name)
{
$application = new Application();

$application->add(new \FooHiddenCommand());
$application->add(new \BarHiddenCommand());

$application->find($name);
}

public function provideAbbreviationsForHiddenCommands()
{
return [
['foo:hidde'],
['afoohidd'],
['bar:hidde'],
];
}

public function testFindAmbiguousCommandsIfAllAlternativesAreHidden()
{
$application = new Application();

$application->add(new \FooCommand());
$application->add(new \FooHiddenCommand());

$this->assertInstanceOf('FooCommand', $application->find('foo:'));
}

public function testSetCatchExceptions()
{
$application = new Application();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class BarHiddenCommand extends Command
{
protected function configure()
{
$this
->setName('bar:hidden')
->setAliases(['abarhidden'])
->setHidden(true)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.