Symfony\Installer\DownloadCommand::download PHP Method

download() protected method

Chooses the best compressed file format to download (ZIP or TGZ) depending upon the available operating system uncompressing commands and the enabled PHP extensions and it downloads the file.
protected download ( )
    protected function download()
    {
        $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->getRemoteFileUrl(), ['tgz', 'zip'])->getPreferredFile();
        /** @var ProgressBar|null $progressBar */
        $progressBar = null;
        $downloadCallback = function (ProgressEvent $event) use(&$progressBar) {
            $downloadSize = $event->downloadSize;
            $downloaded = $event->downloaded;
            // progress bar is only displayed for files larger than 1MB
            if ($downloadSize < 1 * 1024 * 1024) {
                return;
            }
            if (null === $progressBar) {
                ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) {
                    return Helper::formatMemory($bar->getMaxSteps());
                });
                ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) {
                    return str_pad(Helper::formatMemory($bar->getProgress()), 11, ' ', STR_PAD_LEFT);
                });
                $progressBar = new ProgressBar($this->output, $downloadSize);
                $progressBar->setFormat('%current%/%max% %bar%  %percent:3s%%');
                $progressBar->setRedrawFrequency(max(1, floor($downloadSize / 1000)));
                $progressBar->setBarWidth(60);
                if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
                    $progressBar->setEmptyBarCharacter('░');
                    // light shade character \u2591
                    $progressBar->setProgressCharacter('');
                    $progressBar->setBarCharacter('▓');
                    // dark shade character \u2593
                }
                $progressBar->start();
            }
            $progressBar->setProgress($downloaded);
        };
        $client = $this->getGuzzleClient();
        // store the file in a temporary hidden directory with a random name
        $this->downloadedFilePath = rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'symfony.' . pathinfo($symfonyArchiveFile, PATHINFO_EXTENSION);
        try {
            $request = $client->createRequest('GET', $symfonyArchiveFile);
            $request->getEmitter()->on('progress', $downloadCallback);
            $response = $client->send($request);
        } catch (ClientException $e) {
            if ('new' === $this->getName() && ($e->getCode() === 403 || $e->getCode() === 404)) {
                throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Execute the following command to install the latest stable Symfony release:\n" . '%s new %s', $this->version, $_SERVER['PHP_SELF'], str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $this->projectDir)));
            } else {
                throw new \RuntimeException(sprintf("There was an error downloading %s from symfony.com server:\n%s", $this->getDownloadedApplicationType(), $e->getMessage()), null, $e);
            }
        }
        $this->fs->dumpFile($this->downloadedFilePath, $response->getBody());
        if (null !== $progressBar) {
            $progressBar->finish();
            $this->output->writeln("\n");
        }
        return $this;
    }