Skip to content

Instantly share code, notes, and snippets.

@wouterds
Forked from hannesvdvreken/DownloadCommand.php
Last active October 27, 2015 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterds/df2d5bab557adf31b519 to your computer and use it in GitHub Desktop.
Save wouterds/df2d5bab557adf31b519 to your computer and use it in GitHub Desktop.
<?php
namespace Demo;
use League\Flysystem\Filesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DownloadCommand extends Command
{
/**
* @var \Symfony\Component\Console\Helper\ProgressBar
*/
private $progressBar;
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $output;
/**
* @var \League\Flysystem\Filesystem
*/
private $files;
/**
* @param \League\Flysystem\Filesystem $files
*/
public function __construct(Filesystem $files)
{
$this->files = $files;
parent::__construct();
}
/**
* Configure method.
*/
public function configure()
{
$this->setName('download:file');
$this->setDescription('Give a source file name and a destination file name and we\'ll beam it over');
$this->addArgument('source', InputArgument::REQUIRED, 'Source file');
$this->addArgument('destination', InputArgument::REQUIRED, 'Destination file name');
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int|null
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
// Create stream context.
$context = stream_context_create([], ['notification' => [$this, 'progress']]);
// Pipe file.
$resource = fopen($input->getArgument('source'), 'r', null, $context);
$this->files->putStream($input->getArgument('destination'), $resource);
// End output.
$this->progressBar->finish();
$this->output->writeln('finished');
}
/**
* @param int $notificationCode
* @param int $severity
* @param string $message
* @param int $messageCode
* @param int $bytesTransferred
* @param int $bytesMax
*/
public function progress($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
{
if (STREAM_NOTIFY_REDIRECTED === $notificationCode) {
if ($this->progressBar) {
$this->progressBar->clear();
}
$this->progressBar = null;
return;
}
if (STREAM_NOTIFY_FILE_SIZE_IS === $notificationCode) {
if ($this->progressBar) {
$this->progressBar->clear();
}
$this->progressBar = new ProgressBar($this->output, $bytesMax);
}
if (STREAM_NOTIFY_PROGRESS === $notificationCode) {
if (is_null($this->progressBar)) {
$this->progressBar = new ProgressBar($this->output);
}
$this->progressBar->setProgress($bytesTransferred);
}
if (STREAM_NOTIFY_COMPLETED === $notificationCode) {
$this->finish($bytesTransferred);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment