[Console] Added suggestions for missing packages
This commit is contained in:
parent
a617006e6c
commit
423a54f46e
@ -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],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,10 @@
|
|||||||
<tag name="monolog.logger" channel="console" />
|
<tag name="monolog.logger" channel="console" />
|
||||||
</service>
|
</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">
|
<service id="console.command.about" class="Symfony\Bundle\FrameworkBundle\Command\AboutCommand">
|
||||||
<tag name="console.command" command="about" />
|
<tag name="console.command" command="about" />
|
||||||
</service>
|
</service>
|
||||||
|
@ -12,8 +12,11 @@
|
|||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber;
|
||||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||||
use Symfony\Component\Console\Command\Command;
|
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\ArrayInput;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\NullOutput;
|
use Symfony\Component\Console\Output\NullOutput;
|
||||||
@ -226,6 +229,34 @@ class ApplicationTest extends TestCase
|
|||||||
$this->assertContains(trim('[WARNING] Some commands could not be registered:'), trim($display[1]));
|
$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)
|
private function getKernel(array $bundles, $useDispatcher = false)
|
||||||
{
|
{
|
||||||
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
|
||||||
|
Reference in New Issue
Block a user