Last active
September 27, 2022 10:10
-
-
Save hannesvdvreken/4e20b9f9569f3d5c6245 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) { | |
$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
Thank you!