diff --git a/src/Symfony/Installer/DemoCommand.php b/src/Symfony/Installer/DemoCommand.php index f67812c..c547317 100644 --- a/src/Symfony/Installer/DemoCommand.php +++ b/src/Symfony/Installer/DemoCommand.php @@ -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. @@ -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() { @@ -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; @@ -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'; + } } diff --git a/src/Symfony/Installer/DownloadCommand.php b/src/Symfony/Installer/DownloadCommand.php index 04a0b76..69d5617 100644 --- a/src/Symfony/Installer/DownloadCommand.php +++ b/src/Symfony/Installer/DownloadCommand.php @@ -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. @@ -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 @@ -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() ; @@ -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() )); } @@ -142,13 +168,13 @@ 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( @@ -156,7 +182,7 @@ protected function extract() "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( @@ -165,7 +191,7 @@ 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() )); } @@ -173,7 +199,7 @@ protected function extract() 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() )); } @@ -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 ''; - } } diff --git a/src/Symfony/Installer/NewCommand.php b/src/Symfony/Installer/NewCommand.php index 6435064..b62e4d6 100755 --- a/src/Symfony/Installer/NewCommand.php +++ b/src/Symfony/Installer/NewCommand.php @@ -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. @@ -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() { @@ -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; @@ -62,7 +56,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $this ->checkProjectName() ->checkSymfonyVersionIsInstallable() - ->initializeRemoteFileUrl() ->download() ->extract() ->cleanUp() @@ -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; } }