|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Wowstack\Dbc\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
14
|
|
|
use Wowstack\Dbc\DBC; |
|
15
|
|
|
use Wowstack\Dbc\Mapping; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @codeCoverageIgnore |
|
19
|
|
|
*/ |
|
20
|
|
|
class ViewCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function configure() |
|
26
|
|
|
{ |
|
27
|
|
|
$this |
|
28
|
|
|
->setName('dbc:view') |
|
29
|
|
|
->setDescription('View the contents of a DBC file.') |
|
30
|
|
|
->setHelp('This command allows you to view any DBC file using a file map.') |
|
31
|
|
|
; |
|
32
|
|
|
|
|
33
|
|
|
$this |
|
34
|
|
|
->addArgument('file', InputArgument::REQUIRED, 'Path to the DBC file') |
|
35
|
|
|
->addArgument('map', InputArgument::REQUIRED, 'Path to the YAML map') |
|
36
|
|
|
; |
|
37
|
|
|
|
|
38
|
|
|
$this |
|
39
|
|
|
->addOption( |
|
40
|
|
|
'rows', |
|
41
|
|
|
null, |
|
42
|
|
|
InputOption::VALUE_REQUIRED, |
|
43
|
|
|
'Print selected number of rows', |
|
44
|
|
|
false |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
52
|
|
|
{ |
|
53
|
|
|
$DBC = new DBC($input->getArgument('file'), Mapping::fromYAML($input->getArgument('map'))); |
|
|
|
|
|
|
54
|
|
|
$io = new SymfonyStyle($input, $output); |
|
55
|
|
|
$table = new Table($output); |
|
56
|
|
|
|
|
57
|
|
|
$io->title('DBC Viewer'); |
|
58
|
|
|
$io->section('Stats'); |
|
59
|
|
|
$io->text([ |
|
60
|
|
|
sprintf( |
|
61
|
|
|
'The %s file contains %u rows at %u bytes per column, split into %u fields.', |
|
62
|
|
|
$DBC->getName(), |
|
63
|
|
|
$DBC->getRecordCount(), |
|
64
|
|
|
$DBC->getRecordSize(), |
|
65
|
|
|
$DBC->getFieldCount() |
|
66
|
|
|
), |
|
67
|
|
|
'', |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
if ($DBC->hasStrings()) { |
|
71
|
|
|
$string_block = $DBC->getStringBlock(); |
|
|
|
|
|
|
72
|
|
|
$io->section('Strings'); |
|
73
|
|
|
$io->text([ |
|
74
|
|
|
sprintf( |
|
75
|
|
|
'The %s file contains %u strings.', |
|
76
|
|
|
$DBC->getName(), |
|
77
|
|
|
count($string_block) |
|
|
|
|
|
|
78
|
|
|
), |
|
79
|
|
|
'', |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
$io->newLine(); |
|
83
|
|
|
|
|
84
|
|
|
$rows = $input->getOption('rows'); |
|
85
|
|
|
$table->setHeaders($DBC->getMap()->getFieldNames()); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($DBC as $index => $record) { |
|
88
|
|
|
$table->addRow($record->read()); |
|
89
|
|
|
|
|
90
|
|
|
if (false !== $rows) { |
|
91
|
|
|
--$rows; |
|
92
|
|
|
if (0 === $rows) { |
|
93
|
|
|
break; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$table->render(); |
|
99
|
|
|
$io->newLine(); |
|
100
|
|
|
|
|
101
|
|
|
$errors = $DBC->getErrors(); |
|
102
|
|
|
if (count($errors) > 0) { |
|
103
|
|
|
$io->section('Errors'); |
|
104
|
|
|
foreach ($DBC->getErrors() as $error) { |
|
105
|
|
|
$io->text([ |
|
106
|
|
|
'#'.$error['record'].' ('.$error['type'].'/'.$error['field'].'): '.$error['hint'], |
|
|
|
|
|
|
107
|
|
|
]); |
|
108
|
|
|
} |
|
109
|
|
|
$io->newLine(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|