2222use GuzzleHttp \Subscriber \Progress \Progress ;
2323use Symfony \Component \Console \Command \Command ;
2424use 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 ;
2528
2629/**
2730 * Abstract command used by commands which download and extract compressed Symfony files.
3134 */
3235abstract class DownloadCommand extends Command
3336{
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+
3460 /**
3561 * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the
3662 * available operating system uncompressing commands and the enabled PHP extensions
@@ -42,14 +68,14 @@ abstract class DownloadCommand extends Command
4268 */
4369 protected function download ()
4470 {
45- $ this ->output ->writeln (sprintf ("\n Downloading %s... \n" , $ this ->getDownloadedFileName ()));
71+ $ this ->output ->writeln (sprintf ("\n Downloading %s... \n" , $ this ->getDownloadedApplicationType ()));
4672
4773 // decide which is the best compressed version to download
4874 $ distill = new Distill ();
4975 $ symfonyArchiveFile = $ distill
5076 ->getChooser ()
5177 ->setStrategy (new MinimumSize ())
52- ->addFilesWithDifferentExtensions ($ this ->remoteFileUrl , ['tgz ' , 'zip ' ])
78+ ->addFilesWithDifferentExtensions ($ this ->getRemoteFileUrl () , ['tgz ' , 'zip ' ])
5379 ->getPreferredFile ()
5480 ;
5581
@@ -107,7 +133,7 @@ protected function download()
107133 } else {
108134 throw new \RuntimeException (sprintf (
109135 "There was an error downloading %s from symfony.com server: \n%s " ,
110- $ this ->getDownloadedFileName (),
136+ $ this ->getDownloadedApplicationType (),
111137 $ e ->getMessage ()
112138 ));
113139 }
@@ -142,21 +168,21 @@ protected function extract()
142168 throw new \RuntimeException (sprintf (
143169 "%s can't be installed because the downloaded package is corrupted. \n" .
144170 "To solve this issue, try executing this command again: \n%s " ,
145- $ this ->getDownloadedFileName (), $ this ->getExecutedCommand ()
171+ $ this ->getDownloadedApplicationType (), $ this ->getExecutedCommand ()
146172 ));
147173 } catch (FileEmptyException $ e ) {
148174 throw new \RuntimeException (sprintf (
149175 "%s can't be installed because the downloaded package is empty. \n" .
150176 "To solve this issue, try executing this command again: \n%s " ,
151- $ this ->getDownloadedFileName (), $ this ->getExecutedCommand ()
177+ $ this ->getDownloadedApplicationType (), $ this ->getExecutedCommand ()
152178 ));
153179 } catch (TargetDirectoryNotWritableException $ e ) {
154180 throw new \RuntimeException (sprintf (
155181 "%s can't be installed because the installer doesn't have enough \n" .
156182 "permissions to uncompress and rename the package contents. \n" .
157183 "To solve this issue, check the permissions of the %s directory and \n" .
158184 "try executing this command again: \n%s " ,
159- $ this ->getDownloadedFileName (), getcwd (), $ this ->getExecutedCommand ()
185+ $ this ->getDownloadedApplicationType (), getcwd (), $ this ->getExecutedCommand ()
160186 ));
161187 } catch (\Exception $ e ) {
162188 throw new \RuntimeException (sprintf (
@@ -165,15 +191,15 @@ protected function extract()
165191 "rename the package contents. \n" .
166192 "To solve this issue, check the permissions of the %s directory and \n" .
167193 "try executing this command again: \n%s " ,
168- $ this ->getDownloadedFileName (), getcwd (), $ this ->getExecutedCommand ()
194+ $ this ->getDownloadedApplicationType (), getcwd (), $ this ->getExecutedCommand ()
169195 ));
170196 }
171197
172198 if (!$ extractionSucceeded ) {
173199 throw new \RuntimeException (sprintf (
174200 "%s can't be installed because the downloaded package is corrupted \n" .
175201 "or because the uncompress commands of your operating system didn't work. " ,
176- $ this ->getDownloadedFileName ()
202+ $ this ->getDownloadedApplicationType ()
177203 ));
178204 }
179205
@@ -323,23 +349,4 @@ protected function isEmptyDirectory($dir)
323349 // scandir() returns '.' and '..' for an empty dir
324350 return 2 === count (scandir ($ dir .'/ ' ));
325351 }
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- }
345352}
0 commit comments