Skip to content
This repository was archived by the owner on Nov 14, 2019. It is now read-only.

Commit 265d4e6

Browse files
committed
minor #121 Improved the Download abstract command (javiereguiluz)
This PR was squashed before being merged into the 1.0-dev branch (closes #121). Discussion ---------- Improved the Download abstract command This PR addresses the comments made by @stof in #118 and tries to improve the emergency solution provided by @xabbuh in #120. Commits ------- 7cf9611 Improved the Download abstract command
2 parents cd92205 + 7cf9611 commit 265d4e6

File tree

3 files changed

+53
-52
lines changed

3 files changed

+53
-52
lines changed

src/Symfony/Installer/DemoCommand.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
16-
use Symfony\Component\Filesystem\Filesystem;
1716

1817
/**
1918
* This command creates a full-featured Symfony demo application.
@@ -22,15 +21,10 @@
2221
*/
2322
class DemoCommand extends DownloadCommand
2423
{
25-
/** @var Filesystem */
26-
protected $fs;
2724
protected $projectName;
2825
protected $projectDir;
29-
protected $remoteFileUrl;
3026
protected $downloadedFilePath;
3127
protected $requirementsErrors = array();
32-
/** @var OutputInterface */
33-
protected $output;
3428

3529
protected function configure()
3630
{
@@ -42,9 +36,8 @@ protected function configure()
4236

4337
protected function initialize(InputInterface $input, OutputInterface $output)
4438
{
45-
$this->remoteFileUrl = 'http://symfony.com/download?v=Symfony_Demo';
46-
$this->output = $output;
47-
$this->fs = new Filesystem();
39+
parent::initialize($input, $output);
40+
4841
$this->projectDir = getcwd();
4942

5043
$i = 1;
@@ -128,4 +121,14 @@ private function displayInstallationResult()
128121

129122
return $this;
130123
}
124+
125+
protected function getDownloadedApplicationType()
126+
{
127+
return 'Symfony Demo Application';
128+
}
129+
130+
protected function getRemoteFileUrl()
131+
{
132+
return 'http://symfony.com/download?v=Symfony_Demo';
133+
}
131134
}

src/Symfony/Installer/DownloadCommand.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
use GuzzleHttp\Subscriber\Progress\Progress;
2323
use Symfony\Component\Console\Command\Command;
2424
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;
2528

2629
/**
2730
* Abstract command used by commands which download and extract compressed Symfony files.
@@ -31,6 +34,29 @@
3134
*/
3235
abstract 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
}

src/Symfony/Installer/NewCommand.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Console\Input\InputArgument;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
18-
use Symfony\Component\Filesystem\Filesystem;
1918

2019
/**
2120
* This command creates new Symfony projects for the given Symfony version.
@@ -25,15 +24,11 @@
2524
*/
2625
class NewCommand extends DownloadCommand
2726
{
28-
/** @var Filesystem */
29-
protected $fs;
3027
protected $projectName;
3128
protected $projectDir;
3229
protected $version;
3330
protected $downloadedFilePath;
3431
protected $requirementsErrors = array();
35-
/** @var OutputInterface */
36-
protected $output;
3732

3833
protected function configure()
3934
{
@@ -47,8 +42,7 @@ protected function configure()
4742

4843
protected function initialize(InputInterface $input, OutputInterface $output)
4944
{
50-
$this->output = $output;
51-
$this->fs = new Filesystem();
45+
parent::initialize($input, $output);
5246

5347
$directory = rtrim(trim($input->getArgument('directory')), DIRECTORY_SEPARATOR);
5448
$this->projectDir = $this->fs->isAbsolutePath($directory) ? $directory : getcwd().DIRECTORY_SEPARATOR.$directory;
@@ -62,7 +56,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6256
$this
6357
->checkProjectName()
6458
->checkSymfonyVersionIsInstallable()
65-
->initializeRemoteFileUrl()
6659
->download()
6760
->extract()
6861
->cleanUp()
@@ -384,15 +377,13 @@ protected function generateComposerProjectName()
384377
return $name;
385378
}
386379

387-
/**
388-
* Builds the URL of the archive to download based on the validated version number.
389-
*
390-
* @return NewCommand
391-
*/
392-
private function initializeRemoteFileUrl()
380+
protected function getDownloadedApplicationType()
393381
{
394-
$this->remoteFileUrl = 'http://symfony.com/download?v=Symfony_Standard_Vendors_'.$this->version;
382+
return 'Symfony';
383+
}
395384

396-
return $this;
385+
protected function getRemoteFileUrl()
386+
{
387+
return 'http://symfony.com/download?v=Symfony_Standard_Vendors_'.$this->version;
397388
}
398389
}

0 commit comments

Comments
 (0)