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

Improved the Download abstract command #121

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Symfony/Installer/DemoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* This command creates a full-featured Symfony demo application.
Expand All @@ -26,7 +25,6 @@ class DemoCommand extends DownloadCommand
protected $fs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property should be moved to the DownloadCommand class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I moved the initialization of the variables but not their declaration. Done. Thanks.

protected $projectName;
protected $projectDir;
protected $remoteFileUrl;
protected $downloadedFilePath;
protected $requirementsErrors = array();
/** @var OutputInterface */
Expand All @@ -42,9 +40,8 @@ protected function configure()

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->remoteFileUrl = 'http://symfony.com/download?v=Symfony_Demo';
$this->output = $output;
$this->fs = new Filesystem();
parent::initialize($input, $output);

$this->projectDir = getcwd();

$i = 1;
Expand Down Expand Up @@ -128,4 +125,14 @@ private function displayInstallationResult()

return $this;
}

protected function getDownloadedApplicationType()
{
return 'Symfony Demo Application';
}

protected function getRemoteFileUrl()
{
return 'http://symfony.com/download?v=Symfony_Demo';
}
}
56 changes: 29 additions & 27 deletions src/Symfony/Installer/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use GuzzleHttp\Subscriber\Progress\Progress;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* Abstract command used by commands which download and extract compressed Symfony files.
Expand All @@ -31,6 +34,24 @@
*/
abstract class DownloadCommand extends Command
{
/**
* Returns the type of the downloaded application in a human readable format.
* It's mainly used to display readable error messages.
* @return string
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you need one space in front of each *.

abstract protected function getDownloadedApplicationType();

/**
* Returns the absolute URL of the remote file downloaded by the command.
*/
abstract protected function getRemoteFileUrl();

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->fs = new Filesystem();
}

/**
* Chooses the best compressed file format to download (ZIP or TGZ) depending upon the
* available operating system uncompressing commands and the enabled PHP extensions
Expand All @@ -42,14 +63,14 @@ abstract class DownloadCommand extends Command
*/
protected function download()
{
$this->output->writeln(sprintf("\n Downloading %s...\n", $this->getDownloadedFileName()));
$this->output->writeln(sprintf("\n Downloading %s...\n", $this->getDownloadedApplicationType()));

// decide which is the best compressed version to download
$distill = new Distill();
$symfonyArchiveFile = $distill
->getChooser()
->setStrategy(new MinimumSize())
->addFilesWithDifferentExtensions($this->remoteFileUrl, ['tgz', 'zip'])
->addFilesWithDifferentExtensions($this->getRemoteFileUrl(), ['tgz', 'zip'])
->getPreferredFile()
;

Expand Down Expand Up @@ -107,7 +128,7 @@ protected function download()
} else {
throw new \RuntimeException(sprintf(
"There was an error downloading %s from symfony.com server:\n%s",
$this->getDownloadedFileName(),
$this->getDownloadedApplicationType(),
$e->getMessage()
));
}
Expand Down Expand Up @@ -142,21 +163,21 @@ protected function extract()
throw new \RuntimeException(sprintf(
"%s can't be installed because the downloaded package is corrupted.\n".
"To solve this issue, try executing this command again:\n%s",
$this->getDownloadedFileName(), $this->getExecutedCommand()
$this->getDownloadedApplicationType(), $this->getExecutedCommand()
));
} catch (FileEmptyException $e) {
throw new \RuntimeException(sprintf(
"%s can't be installed because the downloaded package is empty.\n".
"To solve this issue, try executing this command again:\n%s",
$this->getDownloadedFileName(), $this->getExecutedCommand()
$this->getDownloadedApplicationType(), $this->getExecutedCommand()
));
} catch (TargetDirectoryNotWritableException $e) {
throw new \RuntimeException(sprintf(
"%s can't be installed because the installer doesn't have enough\n".
"permissions to uncompress and rename the package contents.\n".
"To solve this issue, check the permissions of the %s directory and\n".
"try executing this command again:\n%s",
$this->getDownloadedFileName(), getcwd(), $this->getExecutedCommand()
$this->getDownloadedApplicationType(), getcwd(), $this->getExecutedCommand()
));
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
Expand All @@ -165,15 +186,15 @@ protected function extract()
"rename the package contents.\n".
"To solve this issue, check the permissions of the %s directory and\n".
"try executing this command again:\n%s",
$this->getDownloadedFileName(), getcwd(), $this->getExecutedCommand()
$this->getDownloadedApplicationType(), getcwd(), $this->getExecutedCommand()
));
}

if (!$extractionSucceeded) {
throw new \RuntimeException(sprintf(
"%s can't be installed because the downloaded package is corrupted\n".
"or because the uncompress commands of your operating system didn't work.",
$this->getDownloadedFileName()
$this->getDownloadedApplicationType()
));
}

Expand Down Expand Up @@ -323,23 +344,4 @@ protected function isEmptyDirectory($dir)
// scandir() returns '.' and '..' for an empty dir
return 2 === count(scandir($dir.'/'));
}

/**
* Returns the name of the downloaded file in a human readable format.
* @return string
*/
protected function getDownloadedFileName()
{
$commandName = $this->getName();

if ('new' === $commandName) {
return 'Symfony';
}

if ('demo' === $commandName) {
return 'Symfony Demo Application';
}

return '';
}
}
19 changes: 7 additions & 12 deletions src/Symfony/Installer/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* This command creates new Symfony projects for the given Symfony version.
Expand Down Expand Up @@ -47,8 +46,7 @@ protected function configure()

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->fs = new Filesystem();
parent::initialize($input, $output);

$directory = rtrim(trim($input->getArgument('directory')), DIRECTORY_SEPARATOR);
$this->projectDir = $this->fs->isAbsolutePath($directory) ? $directory : getcwd().DIRECTORY_SEPARATOR.$directory;
Expand All @@ -62,7 +60,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this
->checkProjectName()
->checkSymfonyVersionIsInstallable()
->initializeRemoteFileUrl()
->download()
->extract()
->cleanUp()
Expand Down Expand Up @@ -384,15 +381,13 @@ protected function generateComposerProjectName()
return $name;
}

/**
* Builds the URL of the archive to download based on the validated version number.
*
* @return NewCommand
*/
private function initializeRemoteFileUrl()
protected function getDownloadedApplicationType()
{
$this->remoteFileUrl = 'http://symfony.com/download?v=Symfony_Standard_Vendors_'.$this->version;
return 'Symfony';
}

return $this;
protected function getRemoteFileUrl()
{
return 'http://symfony.com/download?v=Symfony_Standard_Vendors_'.$this->version;
}
}