Skip to content

Commit 56f0342

Browse files
included changes requested by @chalasr
1 parent e9def99 commit 56f0342

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

src/Symfony/Bundle/FrameworkBundle/EventListener/SuggestMissingPackageSubscriber.php

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\ConsoleEvents;
1515
use Symfony\Component\Console\Event\ConsoleErrorEvent;
16+
use Symfony\Component\Console\Exception\CommandNotFoundException;
1617
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
1718
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1819

@@ -24,46 +25,57 @@
2425
*/
2526
final class SuggestMissingPackageSubscriber implements EventSubscriberInterface
2627
{
27-
/**
28-
* Mappings between namespaces of Symfony commands and packages required to run these commands.
29-
*
30-
* @var array
31-
*/
32-
private static $suggestedPackages = [
28+
const PACKAGES = [
3329
'doctrine' => [
34-
'Doctrine ORM',
35-
'symfony/orm-pack',
30+
'fixtures' => ['DoctrineFixturesBundle', 'doctrine/doctrine-fixtures-bundle --dev'],
31+
'mongodb' => ['DoctrineMongoDBBundle', 'doctrine/mongodb-odm-bundle'],
32+
'_default' => ['Doctrine ORM', 'symfony/orm-pack'],
3633
],
3734
'generate' => [
38-
'SensioGeneratorBundle',
39-
'sensio/generator-bundle',
35+
'_default' => ['SensioGeneratorBundle', 'sensio/generator-bundle'],
4036
],
4137
'make' => [
42-
'MakerBundle',
43-
'symfony/maker-bundle --dev',
38+
'_default' => ['MakerBundle', 'symfony/maker-bundle --dev'],
4439
],
4540
'server' => [
46-
'Symfony Web Server',
47-
'symfony/web-server-bundle --dev',
41+
'dump' => ['VarDumper Component', 'symfony/var-dumper --dev'],
42+
'_default' => ['WebServerBundle', 'symfony/web-server-bundle --dev'],
4843
],
4944
];
5045

51-
public function onConsoleError(ConsoleErrorEvent $event)
46+
public function onConsoleError(ConsoleErrorEvent $event) : void
5247
{
53-
if ($event->getError() instanceof NamespaceNotFoundException) {
54-
if (null !== $event->getInput()->getFirstArgument()) {
55-
$namespace = explode(':', $event->getInput()->getFirstArgument())[0];
48+
if (!$event->getError() instanceof CommandNotFoundException || empty($input = $event->getInput()->getFirstArgument())) {
49+
return;
50+
}
51+
52+
$input = explode(':', $input);
53+
$namespace = $input[0];
54+
$command = $input[1] ?? '';
55+
56+
if (!isset(self::PACKAGES[$namespace])) {
57+
return;
58+
}
5659

57-
if (isset(self::$suggestedPackages[$namespace])) {
58-
$suggestion = self::$suggestedPackages[$namespace];
59-
$message = sprintf("It seems that you are trying to run a command from '%s' bundle/package, but you do not have it installed. You may try installing the missing bundle/package by running:\n\n composer require %s", $suggestion[0], $suggestion[1]);
60-
$event->setError(new NamespaceNotFoundException($message));
61-
}
62-
}
60+
if (isset(self::PACKAGES[$namespace][$command]) && '' !== $command) {
61+
$suggestion = self::PACKAGES[$namespace][$command];
62+
$exact = true;
63+
} else {
64+
$suggestion = self::PACKAGES[$namespace]['_default'];
65+
$exact = false;
6366
}
67+
68+
$error = $event->getError();
69+
70+
if (!empty($error->getAlternatives()) && !$exact) {
71+
return;
72+
}
73+
74+
$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]);
75+
$event->setError(new CommandNotFoundException($message));
6476
}
6577

66-
public static function getSubscribedEvents()
78+
public static function getSubscribedEvents() : array
6779
{
6880
return [
6981
ConsoleEvents::ERROR => ['onConsoleError', 0],

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
<tag name="monolog.logger" channel="console" />
1414
</service>
1515

16-
<service id="console.suggest_missing_package_subscriber"
17-
class="Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber">
16+
<service id="console.suggest_missing_package_subscriber" class="Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber">
1817
<tag name="kernel.event_subscriber" />
1918
</service>
2019

0 commit comments

Comments
 (0)