Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bf0806

Browse files
included changes requested by @chalasr
1 parent 8df45ae commit 3bf0806

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

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

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

1414
use Symfony\Component\Console\ConsoleEvents;
1515
use Symfony\Component\Console\Event\ConsoleErrorEvent;
16-
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
16+
use Symfony\Component\Console\Exception\CommandNotFoundException;
1717
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1818

1919
/**
@@ -24,46 +24,57 @@
2424
*/
2525
final class SuggestMissingPackageSubscriber implements EventSubscriberInterface
2626
{
27-
/**
28-
* Mappings between namespaces of Symfony commands and packages required to run these commands.
29-
*
30-
* @var array
31-
*/
32-
private static $suggestedPackages = [
27+
private const PACKAGES = [
3328
'doctrine' => [
34-
'Doctrine ORM',
35-
'symfony/orm-pack',
29+
'fixtures' => ['DoctrineFixturesBundle', 'doctrine/doctrine-fixtures-bundle --dev'],
30+
'mongodb' => ['DoctrineMongoDBBundle', 'doctrine/mongodb-odm-bundle'],
31+
'_default' => ['Doctrine ORM', 'symfony/orm-pack'],
3632
],
3733
'generate' => [
38-
'SensioGeneratorBundle',
39-
'sensio/generator-bundle',
34+
'_default' => ['SensioGeneratorBundle', 'sensio/generator-bundle'],
4035
],
4136
'make' => [
42-
'MakerBundle',
43-
'symfony/maker-bundle --dev',
37+
'_default' => ['MakerBundle', 'symfony/maker-bundle --dev'],
4438
],
4539
'server' => [
46-
'Symfony Web Server',
47-
'symfony/web-server-bundle --dev',
40+
'dump' => ['VarDumper Component', 'symfony/var-dumper --dev'],
41+
'_default' => ['WebServerBundle', 'symfony/web-server-bundle --dev'],
4842
],
4943
];
5044

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

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-
}
59+
if (isset(self::PACKAGES[$namespace][$command]) && '' !== $command) {
60+
$suggestion = self::PACKAGES[$namespace][$command];
61+
$exact = true;
62+
} else {
63+
$suggestion = self::PACKAGES[$namespace]['_default'];
64+
$exact = false;
6365
}
66+
67+
$error = $event->getError();
68+
69+
if (!empty($error->getAlternatives()) && !$exact) {
70+
return;
71+
}
72+
73+
$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]);
74+
$event->setError(new CommandNotFoundException($message));
6475
}
6576

66-
public static function getSubscribedEvents()
77+
public static function getSubscribedEvents(): array
6778
{
6879
return [
6980
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)