diff --git a/src/Maker/MakeTwigComponent.php b/src/Maker/MakeTwigComponent.php index 64390c4757..8c8bd17374 100644 --- a/src/Maker/MakeTwigComponent.php +++ b/src/Maker/MakeTwigComponent.php @@ -13,6 +13,7 @@ use Symfony\Bundle\MakerBundle\ConsoleStyle; use Symfony\Bundle\MakerBundle\DependencyBuilder; +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; use Symfony\Bundle\MakerBundle\Generator; use Symfony\Bundle\MakerBundle\InputConfiguration; use Symfony\Bundle\MakerBundle\Str; @@ -20,6 +21,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Process\Process; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; @@ -52,15 +54,41 @@ public function configureDependencies(DependencyBuilder $dependencies): void $dependencies->addClassDependency(AsTwigComponent::class, 'symfony/ux-twig-component'); } + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void + { + if (!$input->getOption('live')) { + $input->setOption('live', $io->confirm('Make this a live component?', false)); + } + + $isLive = $input->getOption('live'); + + if (!$isLive || ($isLive && class_exists(AsLiveComponent::class))) { + return; + } + + $io->caution('The Symfony UX Live Component is needed to make this a live component.'); + + if (!$io->confirm('Do you want us to run composer require symfony/ux-live-component for you?', false)) { + return; + } + + $process = Process::fromShellCommandline('composer require symfony/ux-live-component'); + if (Command::SUCCESS === $process->run()) { + $io->info('symfony/ux-live-component successfully installed!'); + + return; + } + + $io->block($process->getErrorOutput()); + + throw new RuntimeCommandException('Oops! There was a problem installing symfony/ux-live-component.'); + } + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void { $name = $input->getArgument('name'); $live = $input->getOption('live'); - if ($live && !class_exists(AsLiveComponent::class)) { - throw new \RuntimeException('You must install symfony/ux-live-component to create a live component (composer require symfony/ux-live-component)'); - } - $factory = $generator->createClassNameDetails( $name, 'Twig\\Components', @@ -87,11 +115,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $io->writeln(" To render the component, use ."); $io->newLine(); } - - public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void - { - if (!$input->getOption('live')) { - $input->setOption('live', $io->confirm('Make this a live component?', class_exists(AsLiveComponent::class))); - } - } }