1 | <?php |
||||
2 | |||||
3 | namespace wapmorgan\UnifiedArchive\Commands; |
||||
4 | |||||
5 | use Symfony\Component\Console\Helper\Table; |
||||
6 | use Symfony\Component\Console\Input\InputArgument; |
||||
7 | use Symfony\Component\Console\Input\InputInterface; |
||||
8 | use Symfony\Component\Console\Input\InputOption; |
||||
9 | use Symfony\Component\Console\Output\OutputInterface; |
||||
10 | |||||
11 | class TableCommand extends BaseArchiveCommand |
||||
12 | { |
||||
13 | protected static $defaultName = 'files:table'; |
||||
14 | |||||
15 | protected function configure() |
||||
16 | { |
||||
17 | parent::configure(); |
||||
18 | $this |
||||
19 | ->setDescription('Lists archive entries as table') |
||||
20 | ->setHelp('Lists archive entries as table.') |
||||
21 | ->addArgument('filter', InputArgument::OPTIONAL, 'Files filter (as for fnmatch). If no * passed in filter, it will be added at the end of filter') |
||||
22 | ->addOption('human-readable-size', null, InputOption::VALUE_NONE, 'Use human-readable size') |
||||
23 | ->addOption('detect-mimetype', null, InputOption::VALUE_NONE, 'Detect mimetype for entries by its raw content') |
||||
24 | ->addOption('sort', 's', InputOption::VALUE_REQUIRED, 'Sort files in table by one of fields: filename/size/xsize/ratio/date/mimetype/crc/comment. By default is stored') |
||||
25 | ->addOption('sort-desc', null, InputOption::VALUE_NONE, 'Set sort order to desc. By default it is asc') |
||||
26 | ; |
||||
27 | } |
||||
28 | |||||
29 | /** |
||||
30 | * @throws \wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException |
||||
31 | */ |
||||
32 | public function execute(InputInterface $input, OutputInterface $output) |
||||
33 | { |
||||
34 | $archive = $this->getArchive($input, $output); |
||||
35 | $filter = $input->getArgument('filter'); |
||||
36 | $human_readable_size = $input->getOption('human-readable-size'); |
||||
37 | $detect_mimetype = $input->getOption('detect-mimetype'); |
||||
38 | $sort = $input->getOption('sort'); |
||||
39 | $sort_desc = $input->getOption('sort-desc'); |
||||
40 | |||||
41 | $headers = [ |
||||
42 | 'Filename', |
||||
43 | 'Size', |
||||
44 | 'xSize', |
||||
45 | 'Ratio', |
||||
46 | 'Date', |
||||
47 | // 'Crc', |
||||
48 | 'Comment' |
||||
49 | ]; |
||||
50 | if ($detect_mimetype) { |
||||
51 | array_splice($headers, 4, 0, ['Mimetype']); |
||||
52 | } |
||||
53 | |||||
54 | $sort_field = array_search($sort, array_map('strtolower', $headers)); |
||||
55 | |||||
56 | if (!empty($filter) && strpos($filter, '*') === false) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
57 | $filter .= '*'; |
||||
58 | } |
||||
59 | |||||
60 | $table = new Table($output); |
||||
61 | $table->setHeaders($headers); |
||||
62 | |||||
63 | $rows = []; |
||||
64 | |||||
65 | foreach ($archive->getFiles($filter) as $i => $file) { |
||||
0 ignored issues
–
show
It seems like
$filter can also be of type string[] ; however, parameter $filter of wapmorgan\UnifiedArchive...fiedArchive::getFiles() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
66 | $details = $archive->getFileData($file); |
||||
67 | |||||
68 | if ($human_readable_size) { |
||||
69 | $un_comp_size = implode($this->formatSize($details->uncompressedSize)); |
||||
70 | $comp_size = implode($this->formatSize($details->compressedSize)); |
||||
71 | } else { |
||||
72 | $un_comp_size = $details->uncompressedSize; |
||||
73 | $comp_size = $details->compressedSize; |
||||
74 | } |
||||
75 | |||||
76 | $row = [ |
||||
77 | $details->path, |
||||
78 | $un_comp_size, |
||||
79 | $comp_size, |
||||
80 | $details->uncompressedSize > 0 |
||||
81 | ? round($details->compressedSize / $details->uncompressedSize, 1) |
||||
82 | : '-', |
||||
83 | $this->formatDate($details->modificationTime), |
||||
84 | // $details->crc32, |
||||
85 | $details->comment, |
||||
86 | ]; |
||||
87 | if ($detect_mimetype) { |
||||
88 | // @todo May be a bug in future. Need to review |
||||
89 | array_splice($row, 4, 0, [ |
||||
90 | $this->getMimeTypeByStream($archive->getFileStream($file)) |
||||
91 | ]); |
||||
92 | } |
||||
93 | $rows[] = $row; |
||||
94 | // $table->setRow($i, $row); |
||||
95 | |||||
96 | // $len = strlen($un_comp_size); |
||||
97 | // if ($len > $uncomp_size_length) { |
||||
98 | // $uncomp_size_length = $len; |
||||
99 | // } |
||||
100 | // $len = strlen($comp_size); |
||||
101 | // if ($len > $comp_size_length) { |
||||
102 | // $comp_size_length = $len; |
||||
103 | // } |
||||
104 | } |
||||
105 | |||||
106 | if ($sort !== null) { |
||||
107 | usort($rows, function (array $a, array $b) use ($sort_field) { |
||||
108 | if ($a[$sort_field] > $b[$sort_field]) { |
||||
109 | return 1; |
||||
110 | } |
||||
111 | if ($a[$sort_field] < $b[$sort_field]) { |
||||
112 | return -1; |
||||
113 | } |
||||
114 | return 0; |
||||
115 | }); |
||||
116 | if ($sort_desc) { |
||||
117 | $rows = array_reverse($rows); |
||||
118 | } |
||||
119 | } |
||||
120 | |||||
121 | $table->setRows($rows); |
||||
122 | |||||
123 | // $table->setColumnWidth(0, 0); |
||||
124 | // $table->setColumnWidth(1, $uncomp_size_length); |
||||
125 | // $table->setColumnWidth(2, $comp_size_length); |
||||
126 | // $table->setColumnWidth(3, 18); |
||||
127 | $table->render(); |
||||
128 | |||||
129 | return 0; |
||||
130 | } |
||||
131 | } |
||||
132 |