22
22
use GuzzleHttp \Subscriber \Progress \Progress ;
23
23
use Symfony \Component \Console \Command \Command ;
24
24
use Symfony \Component \Console \Helper \ProgressBar ;
25
+ use Symfony \Component \Console \Input \InputInterface ;
26
+ use Symfony \Component \Console \Output \OutputInterface ;
27
+ use Symfony \Component \Filesystem \Filesystem ;
25
28
26
29
/**
27
30
* Abstract command used by commands which download and extract compressed Symfony files.
31
34
*/
32
35
abstract class DownloadCommand extends Command
33
36
{
37
+ /** @var Filesystem */
38
+ protected $ fs ;
39
+ /** @var OutputInterface */
40
+ protected $ output ;
41
+
42
+ /**
43
+ * Returns the type of the downloaded application in a human readable format.
44
+ * It's mainly used to display readable error messages.
45
+ * @return string
46
+ */
47
+ abstract protected function getDownloadedApplicationType ();
48
+
49
+ /**
50
+ * Returns the absolute URL of the remote file downloaded by the command.
51
+ */
52
+ abstract protected function getRemoteFileUrl ();
53
+
54
+ protected function initialize (InputInterface $ input , OutputInterface $ output )
55
+ {
56
+ $ this ->output = $ output ;
57
+ $ this ->fs = new Filesystem ();
58
+ }
59
+
34
60
/**
35
61
* Chooses the best compressed file format to download (ZIP or TGZ) depending upon the
36
62
* available operating system uncompressing commands and the enabled PHP extensions
@@ -42,14 +68,14 @@ abstract class DownloadCommand extends Command
42
68
*/
43
69
protected function download ()
44
70
{
45
- $ this ->output ->writeln (sprintf ("\n Downloading %s... \n" , $ this ->getDownloadedFileName ()));
71
+ $ this ->output ->writeln (sprintf ("\n Downloading %s... \n" , $ this ->getDownloadedApplicationType ()));
46
72
47
73
// decide which is the best compressed version to download
48
74
$ distill = new Distill ();
49
75
$ symfonyArchiveFile = $ distill
50
76
->getChooser ()
51
77
->setStrategy (new MinimumSize ())
52
- ->addFilesWithDifferentExtensions ($ this ->remoteFileUrl , ['tgz ' , 'zip ' ])
78
+ ->addFilesWithDifferentExtensions ($ this ->getRemoteFileUrl () , ['tgz ' , 'zip ' ])
53
79
->getPreferredFile ()
54
80
;
55
81
@@ -107,7 +133,7 @@ protected function download()
107
133
} else {
108
134
throw new \RuntimeException (sprintf (
109
135
"There was an error downloading %s from symfony.com server: \n%s " ,
110
- $ this ->getDownloadedFileName (),
136
+ $ this ->getDownloadedApplicationType (),
111
137
$ e ->getMessage ()
112
138
));
113
139
}
@@ -142,21 +168,21 @@ protected function extract()
142
168
throw new \RuntimeException (sprintf (
143
169
"%s can't be installed because the downloaded package is corrupted. \n" .
144
170
"To solve this issue, try executing this command again: \n%s " ,
145
- $ this ->getDownloadedFileName (), $ this ->getExecutedCommand ()
171
+ $ this ->getDownloadedApplicationType (), $ this ->getExecutedCommand ()
146
172
));
147
173
} catch (FileEmptyException $ e ) {
148
174
throw new \RuntimeException (sprintf (
149
175
"%s can't be installed because the downloaded package is empty. \n" .
150
176
"To solve this issue, try executing this command again: \n%s " ,
151
- $ this ->getDownloadedFileName (), $ this ->getExecutedCommand ()
177
+ $ this ->getDownloadedApplicationType (), $ this ->getExecutedCommand ()
152
178
));
153
179
} catch (TargetDirectoryNotWritableException $ e ) {
154
180
throw new \RuntimeException (sprintf (
155
181
"%s can't be installed because the installer doesn't have enough \n" .
156
182
"permissions to uncompress and rename the package contents. \n" .
157
183
"To solve this issue, check the permissions of the %s directory and \n" .
158
184
"try executing this command again: \n%s " ,
159
- $ this ->getDownloadedFileName (), getcwd (), $ this ->getExecutedCommand ()
185
+ $ this ->getDownloadedApplicationType (), getcwd (), $ this ->getExecutedCommand ()
160
186
));
161
187
} catch (\Exception $ e ) {
162
188
throw new \RuntimeException (sprintf (
@@ -165,15 +191,15 @@ protected function extract()
165
191
"rename the package contents. \n" .
166
192
"To solve this issue, check the permissions of the %s directory and \n" .
167
193
"try executing this command again: \n%s " ,
168
- $ this ->getDownloadedFileName (), getcwd (), $ this ->getExecutedCommand ()
194
+ $ this ->getDownloadedApplicationType (), getcwd (), $ this ->getExecutedCommand ()
169
195
));
170
196
}
171
197
172
198
if (!$ extractionSucceeded ) {
173
199
throw new \RuntimeException (sprintf (
174
200
"%s can't be installed because the downloaded package is corrupted \n" .
175
201
"or because the uncompress commands of your operating system didn't work. " ,
176
- $ this ->getDownloadedFileName ()
202
+ $ this ->getDownloadedApplicationType ()
177
203
));
178
204
}
179
205
@@ -323,23 +349,4 @@ protected function isEmptyDirectory($dir)
323
349
// scandir() returns '.' and '..' for an empty dir
324
350
return 2 === count (scandir ($ dir .'/ ' ));
325
351
}
326
-
327
- /**
328
- * Returns the name of the downloaded file in a human readable format.
329
- * @return string
330
- */
331
- protected function getDownloadedFileName ()
332
- {
333
- $ commandName = $ this ->getName ();
334
-
335
- if ('new ' === $ commandName ) {
336
- return 'Symfony ' ;
337
- }
338
-
339
- if ('demo ' === $ commandName ) {
340
- return 'Symfony Demo Application ' ;
341
- }
342
-
343
- return '' ;
344
- }
345
352
}
0 commit comments