CollectGarbageCommand.php 1.77 KiB
<?php
/*
 * Copyright (C) 2015 IRSTEA
 * All rights reserved.
 */
namespace Irstea\FileUploadBundle\Command;
use Irstea\FileUploadBundle\Model\FileManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
 * Description of GarbageCollectorCommand
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
class CollectGarbageCommand extends ContainerAwareCommand
    protected function configure()
        $this
            ->setName('irstea:file-upload:collect-garbage')
            ->setDescription("Nettoie les fichiers orphelins et partiels.")
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Affiche ce qui devrait être fait, sans le faire')
    protected function execute(InputInterface $input, OutputInterface $output)
        /* @var $manager FileManagerInterface */
        $manager = $this->getContainer()->get('irstea_file_upload.file_manager');
        $files = $manager->findGarbage();
        if(count($files) == 0) {
            $output->writeln('Aucun fichier à supprimer.');
            return;
        if($input->getOption('dry-run')) {
            $output->writeln(sprintf('%d fichier(s) à supprimer.', count($files)));
            return;
        $output->writeln("Suppression des fichiers");
        $progress = new ProgressBar($output, count($files));
        $progress->start();
        foreach($files as $file){
            $manager->delete($file);
            $progress->advance();
        $progress->finish();
        $output->writeln('');