1111
1212namespace Symfony \UX \Toolkit \Command ;
1313
14- use Composer \InstalledVersions ;
1514use Symfony \Component \Console \Attribute \AsCommand ;
1615use Symfony \Component \Console \Command \Command ;
1716use Symfony \Component \Console \Input \InputArgument ;
1817use Symfony \Component \Console \Input \InputInterface ;
1918use Symfony \Component \Console \Input \InputOption ;
2019use Symfony \Component \Console \Output \OutputInterface ;
2120use Symfony \Component \Console \Style \SymfonyStyle ;
21+ use Symfony \Component \Filesystem \Filesystem ;
2222use Symfony \Component \Filesystem \Path ;
23- use Symfony \UX \Toolkit \Component \Component ;
24- use Symfony \UX \Toolkit \Component \ComponentInstaller ;
25- use Symfony \UX \Toolkit \Dependency \ComponentDependency ;
26- use Symfony \UX \Toolkit \Dependency \PhpPackageDependency ;
27- use Symfony \UX \Toolkit \Exception \ComponentAlreadyExistsException ;
23+ use Symfony \UX \Toolkit \Asset \Component ;
24+ use Symfony \UX \Toolkit \File \File ;
25+ use Symfony \UX \Toolkit \Installer \Installer ;
2826use Symfony \UX \Toolkit \Kit \Kit ;
2927use Symfony \UX \Toolkit \Registry \RegistryFactory ;
3028
@@ -46,7 +44,7 @@ class InstallComponentCommand extends Command
4644 public function __construct (
4745 private readonly string $ kitName ,
4846 private readonly RegistryFactory $ registryFactory ,
49- private readonly ComponentInstaller $ componentInstaller ,
47+ private readonly Filesystem $ filesystem ,
5048 ) {
5149 parent ::__construct ();
5250 }
@@ -87,7 +85,6 @@ protected function configure(): void
8785 protected function initialize (InputInterface $ input , OutputInterface $ output ): void
8886 {
8987 $ this ->io = new SymfonyStyle ($ input , $ output );
90- $ this ->isInteractive = $ input ->isInteractive ();
9188 }
9289
9390 protected function execute (InputInterface $ input , OutputInterface $ output ): int
@@ -123,29 +120,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123120 }
124121 }
125122
126- // Install the component and dependencies
127- $ destination = $ input ->getOption ('destination ' );
123+ $ this ->io ->text (\sprintf ('Installing component <info>%s</>... ' , $ component ->name ));
128124
129- if (!$ this ->installComponent ($ kit , $ component , $ destination )) {
130- return Command::FAILURE ;
131- }
125+ $ installer = new Installer ($ this ->filesystem , fn (string $ question ) => $ this ->io ->confirm ($ question , $ input ->isInteractive ()));
126+ $ installationReport = $ installer ->installComponent ($ kit , $ component , $ destinationPath = $ input ->getOption ('destination ' ), $ input ->getOption ('force ' ));
132127
133- // Iterate over the component's dependencies
134- $ phpDependenciesToInstall = [];
135- foreach ($ component ->getDependencies () as $ dependency ) {
136- if ($ dependency instanceof ComponentDependency) {
137- if (!$ this ->installComponent ($ kit , $ kit ->getComponent ($ dependency ->name ), $ destination )) {
138- return Command::FAILURE ;
139- }
140- } elseif ($ dependency instanceof PhpPackageDependency && !InstalledVersions::isInstalled ($ dependency ->name )) {
141- $ phpDependenciesToInstall [] = $ dependency ;
142- }
128+ if ([] === $ installationReport ->newFiles ) {
129+ $ this ->io ->warning ('The component has not been installed. ' );
130+
131+ return Command::SUCCESS ;
143132 }
144133
145- $ this ->io ->success (\sprintf ('The component "%s" has been installed. ' , $ component ->name ));
134+ $ this ->io ->success ('The component has been installed. ' );
135+ $ this ->io ->writeln ('The following file(s) have been added to your project: ' );
136+ $ this ->io ->listing (array_map (fn (File $ file ) => Path::join ($ destinationPath , $ file ->relativePathName ), $ installationReport ->newFiles ));
146137
147- if ([] !== $ phpDependenciesToInstall ) {
148- $ this ->io ->writeln (\sprintf ('Run <info>composer require %s</info > to install the required PHP dependencies. ' , implode (' ' , $ phpDependenciesToInstall )));
138+ if ([] !== $ installationReport -> suggestedPhpPackages ) {
139+ $ this ->io ->writeln (\sprintf ('Run <info>composer require %s</> to install the required PHP dependencies. ' , implode (' ' , $ installationReport -> suggestedPhpPackages )));
149140 $ this ->io ->newLine ();
150141 }
151142
@@ -168,33 +159,4 @@ private function getAlternativeComponents(Kit $kit, string $componentName): arra
168159
169160 return $ alternative ;
170161 }
171-
172- private function installComponent (Kit $ kit , Component $ component , string $ destination , bool $ force = false ): bool
173- {
174- try {
175- $ this ->io ->text (\sprintf ('<info>Installing component "%s"...</> ' , $ component ->name ));
176-
177- $ this ->componentInstaller ->install ($ kit , $ component , $ destination );
178- } catch (ComponentAlreadyExistsException ) {
179- if ($ force ) {
180- $ this ->componentInstaller ->install ($ kit , $ component , $ destination , true );
181-
182- return true ;
183- }
184-
185- $ this ->io ->warning (\sprintf ('The component "%s" already exists. ' , $ component ->name ));
186-
187- if ($ this ->isInteractive ) {
188- if ($ this ->io ->confirm ('Do you want to overwrite it? ' )) {
189- $ this ->componentInstaller ->install ($ kit , $ component , $ destination , true );
190-
191- return true ;
192- }
193- } else {
194- return false ;
195- }
196- }
197-
198- return true ;
199- }
200162}
0 commit comments