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 all commits
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
21 changes: 12 additions & 9 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 @@ -22,15 +21,10 @@
*/
class DemoCommand extends DownloadCommand
{
/** @var Filesystem */
protected $fs;
protected $projectName;
protected $projectDir;
protected $remoteFileUrl;
protected $downloadedFilePath;
protected $requirementsErrors = array();
/** @var OutputInterface */
protected $output;

protected function configure()
{
Expand All @@ -42,9 +36,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 +121,14 @@ private function displayInstallationResult()

return $this;
}

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

protected function getRemoteFileUrl()
{
return 'http://symfony.com/download?v=Symfony_Demo';
}
}
61 changes: 34 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,29 @@
*/
abstract class DownloadCommand extends Command
{
/** @var Filesystem */
protected $fs;
/** @var OutputInterface */
protected $output;

/**
* Returns the type of the downloaded application in a human readable format.
* It's mainly used to display readable error messages.
* @return string
*/
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 +68,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 +133,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 +168,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 +191,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 +349,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 '';
}
}
23 changes: 7 additions & 16 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 All @@ -25,15 +24,11 @@
*/
class NewCommand extends DownloadCommand
{
/** @var Filesystem */
protected $fs;
protected $projectName;
protected $projectDir;
protected $version;
protected $downloadedFilePath;
protected $requirementsErrors = array();
/** @var OutputInterface */
protected $output;

protected function configure()
{
Expand All @@ -47,8 +42,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 +56,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this
->checkProjectName()
->checkSymfonyVersionIsInstallable()
->initializeRemoteFileUrl()
->download()
->extract()
->cleanUp()
Expand Down Expand Up @@ -384,15 +377,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;
}
}