vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/EventListener/SuggestMissingPackageSubscriber.php line 47

Open in your IDE?
  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.         'generate' => [
  32.             '_default' => ['SensioGeneratorBundle''sensio/generator-bundle'],
  33.         ],
  34.         'make' => [
  35.             '_default' => ['MakerBundle''symfony/maker-bundle --dev'],
  36.         ],
  37.         'server' => [
  38.             'dump' => ['Debug Bundle''symfony/debug-bundle --dev'],
  39.             '_default' => ['WebServerBundle''symfony/web-server-bundle --dev'],
  40.         ],
  41.     ];
  42.     public function onConsoleError(ConsoleErrorEvent $event): void
  43.     {
  44.         if (!$event->getError() instanceof CommandNotFoundException) {
  45.             return;
  46.         }
  47.         [$namespace$command] = explode(':'$event->getInput()->getFirstArgument()) + [=> ''];
  48.         if (!isset(self::PACKAGES[$namespace])) {
  49.             return;
  50.         }
  51.         if (isset(self::PACKAGES[$namespace][$command])) {
  52.             $suggestion self::PACKAGES[$namespace][$command];
  53.             $exact true;
  54.         } else {
  55.             $suggestion self::PACKAGES[$namespace]['_default'];
  56.             $exact false;
  57.         }
  58.         $error $event->getError();
  59.         if ($error->getAlternatives() && !$exact) {
  60.             return;
  61.         }
  62.         $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]);
  63.         $event->setError(new CommandNotFoundException($message));
  64.     }
  65.     public static function getSubscribedEvents(): array
  66.     {
  67.         return [
  68.             ConsoleEvents::ERROR => ['onConsoleError'0],
  69.         ];
  70.     }
  71. }