vendor/symfony/framework-bundle/EventListener/SuggestMissingPackageSubscriber.php line 46

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\EventListener;
  11. use Symfony\Component\Console\ConsoleEvents;
  12. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  13. use Symfony\Component\Console\Exception\CommandNotFoundException;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * Suggests a package, that should be installed (via composer),
  17.  * if the package is missing, and the input command namespace can be mapped to a Symfony bundle.
  18.  *
  19.  * @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
  20.  *
  21.  * @internal
  22.  */
  23. final class SuggestMissingPackageSubscriber implements EventSubscriberInterface
  24. {
  25.     private const PACKAGES = [
  26.         'doctrine' => [
  27.             'fixtures' => ['DoctrineFixturesBundle''doctrine/doctrine-fixtures-bundle --dev'],
  28.             'mongodb' => ['DoctrineMongoDBBundle''doctrine/mongodb-odm-bundle'],
  29.             '_default' => ['Doctrine ORM''symfony/orm-pack'],
  30.         ],
  31.         'make' => [
  32.             '_default' => ['MakerBundle''symfony/maker-bundle --dev'],
  33.         ],
  34.         'server' => [
  35.             '_default' => ['Debug Bundle''symfony/debug-bundle --dev'],
  36.         ],
  37.     ];
  38.     public function onConsoleError(ConsoleErrorEvent $event): void
  39.     {
  40.         if (!$event->getError() instanceof CommandNotFoundException) {
  41.             return;
  42.         }
  43.         [$namespace$command] = explode(':'$event->getInput()->getFirstArgument()) + [=> ''];
  44.         if (!isset(self::PACKAGES[$namespace])) {
  45.             return;
  46.         }
  47.         if (isset(self::PACKAGES[$namespace][$command])) {
  48.             $suggestion self::PACKAGES[$namespace][$command];
  49.             $exact true;
  50.         } else {
  51.             $suggestion self::PACKAGES[$namespace]['_default'];
  52.             $exact false;
  53.         }
  54.         $error $event->getError();
  55.         if ($error->getAlternatives() && !$exact) {
  56.             return;
  57.         }
  58.         $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]);
  59.         $event->setError(new CommandNotFoundException($message));
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             ConsoleEvents::ERROR => ['onConsoleError'0],
  65.         ];
  66.     }
  67. }