Skip to content

[Console] Added suggestions for missing packages #29865

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
Feb 21, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\Bundle\FrameworkBundle\EventListener;

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Suggests a package, that should be installed (via composer),
* if the package is missing, and the input command namespace can be mapped to a Symfony bundle.
*
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*
* @internal
*/
final class SuggestMissingPackageSubscriber implements EventSubscriberInterface
{
private const PACKAGES = [
'doctrine' => [
'fixtures' => ['DoctrineFixturesBundle', 'doctrine/doctrine-fixtures-bundle --dev'],
'mongodb' => ['DoctrineMongoDBBundle', 'doctrine/mongodb-odm-bundle'],
'_default' => ['Doctrine ORM', 'symfony/orm-pack'],
],
'generate' => [
'_default' => ['SensioGeneratorBundle', 'sensio/generator-bundle'],
],
'make' => [
'_default' => ['MakerBundle', 'symfony/maker-bundle --dev'],
],
'server' => [
'dump' => ['VarDumper Component', 'symfony/var-dumper --dev'],
'_default' => ['WebServerBundle', 'symfony/web-server-bundle --dev'],
],
];

public function onConsoleError(ConsoleErrorEvent $event): void
{
if (!$event->getError() instanceof CommandNotFoundException) {
return;
}

[$namespace, $command] = explode(':', $event->getInput()->getFirstArgument()) + [1 => ''];

if (!isset(self::PACKAGES[$namespace])) {
return;
}

if (isset(self::PACKAGES[$namespace][$command])) {
$suggestion = self::PACKAGES[$namespace][$command];
$exact = true;
} else {
$suggestion = self::PACKAGES[$namespace]['_default'];
$exact = false;
}

$error = $event->getError();

if ($error->getAlternatives() && !$exact) {
return;
}

$message = sprintf("%s\n\nYou may be looking for a command provided by the \"%s\" which is currently not installed. Try running \"composer require %s\".", $error->getMessage(), $suggestion[0], $suggestion[1]);
$event->setError(new CommandNotFoundException($message));
}

public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::ERROR => ['onConsoleError', 0],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<tag name="monolog.logger" channel="console" />
</service>

<service id="console.suggest_missing_package_subscriber" class="Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber">
<tag name="kernel.event_subscriber" />
</service>

<service id="console.command.about" class="Symfony\Bundle\FrameworkBundle\Command\AboutCommand">
<tag name="console.command" command="about" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Console;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
Expand Down Expand Up @@ -226,6 +229,34 @@ public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd()
$this->assertContains(trim('[WARNING] Some commands could not be registered:'), trim($display[1]));
}

public function testSuggestingPackagesWithExactMatch()
{
$result = $this->createEventForSuggestingPackages('server:dump', []);
$this->assertRegExp('/You may be looking for a command provided by/', $result);
}

public function testSuggestingPackagesWithPartialMatchAndNoAlternatives()
{
$result = $this->createEventForSuggestingPackages('server', []);
$this->assertRegExp('/You may be looking for a command provided by/', $result);
}

public function testSuggestingPackagesWithPartialMatchAndAlternatives()
{
$result = $this->createEventForSuggestingPackages('server', ['server:run']);
$this->assertNotRegExp('/You may be looking for a command provided by/', $result);
}

private function createEventForSuggestingPackages(string $command, array $alternatives = []): string
{
$error = new CommandNotFoundException('', $alternatives);
$event = new ConsoleErrorEvent(new ArrayInput([$command]), new NullOutput(), $error);
$subscriber = new SuggestMissingPackageSubscriber();
$subscriber->onConsoleError($event);

return $event->getError()->getMessage();
}

private function getKernel(array $bundles, $useDispatcher = false)
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
Expand Down