Passed
Push — master ( 125b29...554a92 )
by f
27:06 queued 12:02
created

src/Commands/AddFileCommand.php (1 issue)

1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class AddFileCommand extends BaseArchiveCommand
11
{
12
    protected static $defaultName = 'file:add';
13
14
    protected function configure()
15
    {
16
        parent::configure();
17
        $this
18
            ->setDescription('Packs new file in archive')
19
            ->setHelp('Packs new file in archive. If used path = relative, then file will have relative name in archive with trimmed dots')
20
            ->addArgument('source', InputArgument::REQUIRED, 'Source file on disk or "-" for standard input')
21
            ->addArgument('destination', InputArgument::OPTIONAL, 'Destination filename in archive')
22
            ->addOption('path', NULL, InputOption::VALUE_OPTIONAL, 'Path resolving if destination is not passed. Variants: full, relative, basename', 'relative')
23
            ->addUsage('archive.zip - < LICENSE')
24
            ->addUsage('archive.zip LICENSE LICENSE')
25
            ->addUsage('archive.zip ../Morphos/.travis.yml --path=full')
26
            ->addUsage('archive.zip ../Morphos/.travis.yml --path=relative')
27
            ->addUsage('archive.zip ../Morphos/.travis.yml --path=basename')
28
        ;
29
    }
30
31
    public function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $source = $input->getArgument('source');
34
        $destination = $input->getArgument('destination');
35
        $path = $input->getOption('path');
36
        if (!in_array($path, ['full', 'relative', 'basename'], true)) {
37
            throw new \InvalidArgumentException('Path can not have this value');
38
        }
39
40
        if ($source === '-') {
41
            stream_set_blocking(STDIN, false);
42
            $data = stream_get_contents(STDIN);
43
            stream_set_blocking(STDIN, true);
44
            $data_size = strlen($data);
45
            if ($data_size === 0) {
46
                throw new \LogicException('Empty input!');
47
            }
48
            if (empty($destination)) {
49
                throw new \LogicException('Source and destination can not be empty');
50
            }
51
            $output->writeln('<info>Read ' . $data_size . ' from input</info>');
52
        } else {
53
            $data_size = filesize($source);
54
        }
55
56
        if (empty($destination)) {
57
            switch ($path) {
58
                case 'full':
59
                    $destination = ltrim(realpath($source), '/');
60
                    break;
61
                case 'relative':
62
                    $destination = ltrim($source, '/.');
63
                    break;
64
                case 'basename':
65
                    $destination = basename($source);
66
                    break;
67
            }
68
        }
69
70
        $archive = $this->getArchive($input, $output);
71
        if ($source === '-') {
72
            $added_files = $archive->addFileFromString($destination, $data) ? 1 : 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
73
        } else {
74
            $added_files = $archive->addFiles([$destination => $source]);
75
        }
76
        if ($added_files === 1) {
77
            $details = $archive->getFileData($destination);
78
            $output->writeln('Added <comment>' . $source . '</comment>('
79
                             . implode($this->formatSize($data_size)) . ') as '
80
                             . $destination . ' ('
81
                             . implode($this->formatSize($details->compressedSize))
82
                             . ')');
83
        }
84
85
        return 0;
86
    }
87
}
88