11
11
12
12
namespace Symfony \UX \Toolkit \Command ;
13
13
14
- use Composer \InstalledVersions ;
15
14
use Symfony \Component \Console \Attribute \AsCommand ;
16
15
use Symfony \Component \Console \Command \Command ;
17
16
use Symfony \Component \Console \Input \InputArgument ;
18
17
use Symfony \Component \Console \Input \InputInterface ;
19
18
use Symfony \Component \Console \Input \InputOption ;
20
19
use Symfony \Component \Console \Output \OutputInterface ;
21
20
use Symfony \Component \Console \Style \SymfonyStyle ;
21
+ use Symfony \Component \Filesystem \Filesystem ;
22
22
use 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 ;
28
26
use Symfony \UX \Toolkit \Kit \Kit ;
29
27
use Symfony \UX \Toolkit \Registry \RegistryFactory ;
30
28
@@ -46,7 +44,7 @@ class InstallComponentCommand extends Command
46
44
public function __construct (
47
45
private readonly string $ kitName ,
48
46
private readonly RegistryFactory $ registryFactory ,
49
- private readonly ComponentInstaller $ componentInstaller ,
47
+ private readonly Filesystem $ filesystem ,
50
48
) {
51
49
parent ::__construct ();
52
50
}
@@ -87,7 +85,6 @@ protected function configure(): void
87
85
protected function initialize (InputInterface $ input , OutputInterface $ output ): void
88
86
{
89
87
$ this ->io = new SymfonyStyle ($ input , $ output );
90
- $ this ->isInteractive = $ input ->isInteractive ();
91
88
}
92
89
93
90
protected function execute (InputInterface $ input , OutputInterface $ output ): int
@@ -123,29 +120,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123
120
}
124
121
}
125
122
126
- // Install the component and dependencies
127
- $ destination = $ input ->getOption ('destination ' );
123
+ $ this ->io ->text (\sprintf ('Installing component <info>%s</>... ' , $ component ->name ));
128
124
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 ' ));
132
127
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 ;
143
132
}
144
133
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 ));
146
137
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 )));
149
140
$ this ->io ->newLine ();
150
141
}
151
142
@@ -168,33 +159,4 @@ private function getAlternativeComponents(Kit $kit, string $componentName): arra
168
159
169
160
return $ alternative ;
170
161
}
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
- }
200
162
}
0 commit comments