Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #29865 [Console] Added suggestions for missing packages (prze…
…myslaw-bogusz) This PR was squashed before being merged into the 4.3-dev branch (closes #29865). Discussion ---------- [Console] Added suggestions for missing packages | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Currently, when someone runs one of the most common commands, e.g. `server:run`, but does not have a required package installed, they will get a general **'There are no commands defined...'** message. This commit adds a more useful message, informing the user about a package that might be missing and suggesting a command that should be run in order to install it, e.g. `composer require symfony/web-server-bundle --dev`. Commits ------- 423a54f [Console] Added suggestions for missing packages
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/Symfony/Bundle/FrameworkBundle/EventListener/SuggestMissingPackageSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters