Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
* Copyright (C) 2015 IRSTEA
* All rights reserved.
*/
namespace Irstea\FileUploadBundle\Command;
use Doctrine\ORM\EntityManager;
use Irstea\FileUploadBundle\Model\FileManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
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 ReadCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('irstea:file-upload:read')
->setDescription("Récupère le contenu d'un fichier.")
->addOption('append', 'a', InputOption::VALUE_NONE, "Ajoute au fichier de destination au lieu de l'écraser.")
->addOption('overwrite', 'y', InputOption::VALUE_NONE, "Ecrase le fichier de destination s'il existe.")
->addArgument('id', InputArgument::REQUIRED, "Identifiant du fichier à récupérer.")
->addArgument('filepath', InputArgument::OPTIONAL, "Chemin du fichier dans lequel écrire.")
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var $manager FileManagerInterface */
$manager = $this->getContainer()->get('irstea_file_upload.file_manager');
$file = $manager->get($input->getArgument('id'));
$path = $input->getArgument('filepath');
if(null == $path) {
$fh = fopen('php://stdout', 'wb');
} else {
if($input->getOption('append')) {
$fh = fopen($path, 'ab');
} elseif($input->getOption('overwrite')) {
$fh = fopen($path, 'wb');
} else {
$fh = fopen($path, 'xb');
}
}
$written = $file->copyTo($fh);
fclose($fh);
if($output->isVerbose() && null !== $path) {
$output->writeln(sprintf("%d octets écrits.", $written));
}
}
}