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