protected function download() { $this->output->writeln(sprintf("\n Downloading %s...\n", $this->projectName)); $distill = new Distill(); $componentFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFile($this->remoteFileUrl)->getPreferredFile(); $downloadCallback = function ($expected, $total, $client, $request, $response) { // Don't initialize the progress bar for redirects as the size is much smaller if ($response->getStatusCode() >= 300) { return; } printf(" Download: %d %% \r", 100 * ($total / $expected)); }; $client = new Client(); $client->getEmitter()->attach(new Progress(null, $downloadCallback)); // store the file in a temporary hidden directory with a random name $this->downloadedFilePath = getcwd() . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . $this->projectName . '.' . ltrim(strstr($componentFile, '.'), '.'); //pathinfo($symfonyArchiveFile, PATHINFO_EXTENSION); try { $response = $client->get($componentFile); } catch (ClientException $e) { throw new \RuntimeException(sprintf("There was an error downloading %s from server:\n%s", $this->getDownloadedFileName(), $e->getMessage())); } $this->fs->dumpFile($this->downloadedFilePath, $response->getBody()); $this->output->writeln("\n"); return $this; }
/** * 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. * * @return Command * * @throws \RuntimeException if the Symfony archive could not be downloaded */ 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 $this->formatSize($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getStep()), 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; }
/** * Downloads the project archive. */ protected function download(OutputInterface $output) { $distill = new Distill(); $archiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFilesWithDifferentExtensions($this->getRemoteFileUrl($this->version), ['zip'])->getPreferredFile(); $temporaryDownloadedFilePath = rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . '.' . pathinfo($archiveFile, PATHINFO_EXTENSION); $this->majoraDownloader = new Downloader($archiveFile, $temporaryDownloadedFilePath, $output); try { $this->majoraDownloader->download(); } catch (RequestException $requestException) { throw new \RuntimeException('Majora Standard Edition can not be downloaded'); } if (!$this->majoraDownloader->isDownloaded()) { $this->clean(true); throw new \RuntimeException('Majora Standard Edition can not be downloaded'); } }
<?php require __DIR__ . '/../vendor/autoload.php'; use Distill\Distill; use Distill\Strategy\MinimumSize; $distill = new Distill(); $strategy = new MinimumSize(); $preferredFile = $distill->getChooser()->setStrategy($strategy)->addFile('http://get.symfony.com/Symfony_Standard_Vendors_2.5.3.tgz')->addFile('http://get.symfony.com/Symfony_Standard_Vendors_2.5.3.zip')->getPreferredFile(); echo $preferredFile . \PHP_EOL; $preferredFile = $distill->getChooser()->setStrategy($strategy)->addFilesWithDifferentExtensions('http://get.symfony.com/Symfony_Standard_Vendors_2.5.3', ['zip', 'tgz'])->getPreferredFile(); echo $preferredFile . \PHP_EOL;
/** * 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. * * @throws \RuntimeException if the ProcessWire archive could not be downloaded */ private function download() { $this->output->writeln("\n Downloading ProcessWire Version " . $this->branch['version'] . "..."); $distill = new Distill(); $pwArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFile($this->branch['zipURL'])->getPreferredFile(); /** @var ProgressBar|null $progressBar */ $progressBar = null; $downloadCallback = function ($size, $downloaded, $client, $request, Response $response) use(&$progressBar) { // Don't initialize the progress bar for redirects as the size is much smaller if ($response->getStatusCode() >= 300) { return; } if (null === $progressBar) { ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return $this->formatSize($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getStep()), 11, ' ', STR_PAD_LEFT); }); $progressBar = new ProgressBar($this->output, $size); $progressBar->setFormat('%current%/%max% %bar% %percent:3s%%'); $progressBar->setRedrawFrequency(max(1, floor($size / 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 = new Client(); $client->getEmitter()->attach(new Progress(null, $downloadCallback)); // store the file in a temporary hidden directory with a random name $this->compressedFilePath = getcwd() . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'pw.' . pathinfo($pwArchiveFile, PATHINFO_EXTENSION); try { $response = $client->get($pwArchiveFile); } catch (ClientException $e) { if ($e->getCode() === 403 || $e->getCode() === 404) { throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Try the special \"latest\" version to install the latest stable ProcessWire release:\n" . '%s %s %s latest', $this->version, $_SERVER['PHP_SELF'], $this->getName(), $this->projectDir)); } else { throw new \RuntimeException(sprintf("The selected version (%s) couldn't be downloaded because of the following error:\n%s", $this->version, $e->getMessage())); } } $this->fs->dumpFile($this->compressedFilePath, $response->getBody()); if (null !== $progressBar) { $progressBar->finish(); $this->output->writeln("\n"); } return $this; }
public function testChooserIsCreatedProperly() { $this->assertInstanceOf('\\Distill\\Chooser', $this->distill->getChooser()); }
/** * 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. * * @param string $url * @param string $module * @param OutputInterface $output * @return NewCommand * * @throws \RuntimeException if the ProcessWire archive could not be downloaded */ public function downloadModule($url, $module, $output) { $output->writeln(" Downloading module {$module}..."); $distill = new Distill(); $pwArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFile($url)->getPreferredFile(); /** @var ProgressBar|null $progressBar */ $progressBar = null; $downloadCallback = function ($size, $downloaded, $client, $request, Response $response) use(&$progressBar, &$output) { // Don't initialize the progress bar for redirects as the size is much smaller if ($response->getStatusCode() >= 300) { return; } if (null === $progressBar) { ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return $this->formatSize($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getStep()), 11, ' ', STR_PAD_LEFT); }); $progressBar = new ProgressBar($output, $size); $progressBar->setFormat('%current%/%max% %bar% %percent:3s%%'); $progressBar->setRedrawFrequency(max(1, floor($size / 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 = new Client(); $client->getEmitter()->attach(new Progress(null, $downloadCallback)); // store the file in a temporary hidden directory with a random name $this->compressedFilePath = \ProcessWire\wire('config')->paths->siteModules . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . $module . '.' . pathinfo($pwArchiveFile, PATHINFO_EXTENSION); try { $response = $client->get($pwArchiveFile); } catch (ClientException $e) { if ($e->getCode() === 403 || $e->getCode() === 404) { throw new \RuntimeException("The selected module {$module} cannot be downloaded because it does not exist.\n"); } else { throw new \RuntimeException(sprintf("The selected module (%s) couldn't be downloaded because of the following error:\n%s", $module, $e->getMessage())); } } $fs = new Filesystem(); $fs->dumpFile($this->compressedFilePath, $response->getBody()); if (null !== $progressBar) { $progressBar->finish(); $output->writeln("\n"); } return $this; }