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

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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 <[email protected]>
  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. '_default' => ['Debug Bundle', 'symfony/debug-bundle --dev'],
  39. ],
  40. ];
  41. public function onConsoleError(ConsoleErrorEvent $event): void
  42. {
  43. if (!$event->getError() instanceof CommandNotFoundException) {
  44. return;
  45. }
  46. [$namespace, $command] = explode(':', $event->getInput()->getFirstArgument()) + [1 => ''];
  47. if (!isset(self::PACKAGES[$namespace])) {
  48. return;
  49. }
  50. if (isset(self::PACKAGES[$namespace][$command])) {
  51. $suggestion = self::PACKAGES[$namespace][$command];
  52. $exact = true;
  53. } else {
  54. $suggestion = self::PACKAGES[$namespace]['_default'];
  55. $exact = false;
  56. }
  57. $error = $event->getError();
  58. if ($error->getAlternatives() && !$exact) {
  59. return;
  60. }
  61. $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]);
  62. $event->setError(new CommandNotFoundException($message));
  63. }
  64. public static function getSubscribedEvents(): array
  65. {
  66. return [
  67. ConsoleEvents::ERROR => ['onConsoleError', 0],
  68. ];
  69. }
  70. }