Skip to content

Instantly share code, notes, and snippets.

@Muspi
Last active April 18, 2023 07:39
Show Gist options
  • Save Muspi/f3226d4c08585226fdd06aac43b12bcb to your computer and use it in GitHub Desktop.
Save Muspi/f3226d4c08585226fdd06aac43b12bcb to your computer and use it in GitHub Desktop.
Command for Symfony 3 & 4.Allow to download a remote file to server, and display ProgressBar.
<?php
/**
* Created by PhpStorm.
* User: Muspi
* Date: 29/05/2018
* Time: 16:19
*/
namespace AppBundle\Command;
use AppBundle\Service\Mail\MailProcessorService;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
use ZipArchive;
class ImportHealthDirectoryCommand extends ContainerAwareCommand {
/**
* @var MailProcessorService
*/
protected $mailer;
/**
* ImportCsvCommand constructor.
*
* @param null|string $name
* @param MailProcessorService $mailProcessor
*
*/
public function __construct(?string $name = null, MailProcessorService $mailProcessor)
{
$this->mailer = $mailProcessor;
parent::__construct($name);
}
/**
*
*/
protected function configure() {
$this
->setName('app:importMyFile')
->setDescription('Import Remote File with ProgressBar');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$downloadUrl = 'your-remote-url';
$archiveDestPath = $this->getContainer()->get('kernel')->getProjectDir().'/data';
//Disable temporary SSL verification (IMPROVE THAT)
$arrContextOptions = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
)
);
//Create ProgressBar
$progress = new ProgressBar($output);
//Create Stream Context with Callback to update ProgressBar
$context = stream_context_create($arrContextOptions, array('notification' => function ($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) use ($output, $progress) {
switch ($notification_code) {
case STREAM_NOTIFY_RESOLVE:
case STREAM_NOTIFY_AUTH_REQUIRED:
case STREAM_NOTIFY_COMPLETED:
case STREAM_NOTIFY_FAILURE:
case STREAM_NOTIFY_AUTH_RESULT:
/** Ignore That */
break;
case STREAM_NOTIFY_REDIRECTED:
$output->writeln("Redirect To : ", $message);
break;
case STREAM_NOTIFY_CONNECT:
$output->writeln("Connected...");
break;
case STREAM_NOTIFY_FILE_SIZE_IS:
/** @var $progress ProgressBar */
$output->writeln("Downloading...");
$progress->start($bytes_max);
break;
case STREAM_NOTIFY_PROGRESS:
$progress->setProgress($bytes_transferred);
break;
}
}));
//Download file
$streamContent = file_get_contents($downloadUrl,false,$context);
$progress->finish();
//Save File
file_put_contents($archiveDestPath.'/archive.zip', $streamContent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment