|
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 ListCommand extends BaseArchiveCommand |
|
11
|
|
|
{ |
|
12
|
|
|
protected static $defaultName = 'files:list'; |
|
13
|
|
|
|
|
14
|
|
|
protected function configure() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::configure(); |
|
17
|
|
|
$this |
|
18
|
|
|
->setDescription('Lists archive entries') |
|
19
|
|
|
->setHelp('Lists archive entries.') |
|
20
|
|
|
->addArgument('filter', InputArgument::OPTIONAL, 'Files filter (as for fnmatch). If no * passed in filter, it will be added at the end of filter') |
|
21
|
|
|
->addOption('longFormat', 'l', InputOption::VALUE_NONE, 'Use long format') |
|
22
|
|
|
->addOption('human-readable-size', null, InputOption::VALUE_NONE, 'Use human-readable size') |
|
23
|
|
|
; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
27
|
|
|
{ |
|
28
|
|
|
$archive = $this->getArchive($input, $output); |
|
29
|
|
|
$filter = $input->getArgument('filter'); |
|
30
|
|
|
$long_format = $input->getOption('longFormat'); |
|
31
|
|
|
$human_readable_size = $input->getOption('human-readable-size'); |
|
32
|
|
|
|
|
33
|
|
|
if (!empty($filter) && strpos($filter, '*') === false) { |
|
|
|
|
|
|
34
|
|
|
$filter .= '*'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($long_format) { |
|
38
|
|
|
$uncomp_size_length = $comp_size_length = 0; |
|
39
|
|
|
foreach ($archive->getFileNames($filter) as $file) { |
|
|
|
|
|
|
40
|
|
|
$details = $archive->getFileData($file); |
|
41
|
|
|
|
|
42
|
|
|
if ($human_readable_size) { |
|
43
|
|
|
$un_comp_size = implode($this->formatSize($details->uncompressedSize)); |
|
44
|
|
|
$comp_size = implode($this->formatSize($details->compressedSize)); |
|
45
|
|
|
} else { |
|
46
|
|
|
$un_comp_size = $details->uncompressedSize; |
|
47
|
|
|
$comp_size = $details->compressedSize; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$len = strlen($un_comp_size); |
|
51
|
|
|
if ($len > $uncomp_size_length) { |
|
52
|
|
|
$uncomp_size_length = $len; |
|
53
|
|
|
} |
|
54
|
|
|
$len = strlen($comp_size); |
|
55
|
|
|
if ($len > $comp_size_length) { |
|
56
|
|
|
$comp_size_length = $len; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($archive->getFileNames($filter) as $file) { |
|
61
|
|
|
$details = $archive->getFileData($file); |
|
62
|
|
|
$output->writeln(($details->isCompressed && $details->uncompressedSize > 0 ? 'x' : '-') |
|
63
|
|
|
. ' ' . str_pad( |
|
64
|
|
|
$human_readable_size ? implode($this->formatSize($details->uncompressedSize)) : $details->uncompressedSize, |
|
65
|
|
|
$uncomp_size_length, |
|
66
|
|
|
' ', |
|
67
|
|
|
STR_PAD_LEFT) |
|
68
|
|
|
. ' ' . str_pad( |
|
69
|
|
|
$human_readable_size ? implode($this->formatSize($details->compressedSize)) : $details->compressedSize, |
|
70
|
|
|
$comp_size_length, |
|
71
|
|
|
' ', |
|
72
|
|
|
STR_PAD_LEFT) |
|
73
|
|
|
. ' ' . $this->formatDateShort($details->modificationTime) . ' ' . $details->path); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
foreach ($archive->getFileNames($filter) as $file) { |
|
77
|
|
|
$output->writeln($file); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function formatDateShort($timestamp) |
|
85
|
|
|
{ |
|
86
|
|
|
static $current_year; |
|
87
|
|
|
if ($current_year === null) { |
|
88
|
|
|
$current_year = strtotime('1 january'); |
|
89
|
|
|
} |
|
90
|
|
|
if ($timestamp < $current_year) { |
|
91
|
|
|
return strtolower(date('M d o', $timestamp)); |
|
92
|
|
|
} else { |
|
93
|
|
|
return strtolower(date('M d H:i', $timestamp)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|