-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImportCommand.php
More file actions
223 lines (186 loc) · 8.4 KB
/
ImportCommand.php
File metadata and controls
223 lines (186 loc) · 8.4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace Mathielen\ImportEngineBundle\Command;
use Ddeboer\DataImport\Filter\OffsetFilter;
use Mathielen\DataImport\Event\ImportItemEvent;
use Mathielen\ImportEngine\Event\ImportConfigureEvent;
use Mathielen\ImportEngine\Event\ImportRequestEvent;
use Mathielen\ImportEngine\Import\ImportBuilder;
use Mathielen\ImportEngine\Import\Run\ImportRunner;
use Mathielen\ImportEngine\ValueObject\ImportRequest;
use Mathielen\ImportEngine\ValueObject\ImportRun;
use Mathielen\ImportEngineBundle\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Validator\ConstraintViolation;
class ImportCommand extends Command
{
const MAX_VIOLATION_ERRORS = 10;
/**
* @var ImportBuilder
*/
private $importBuilder;
/**
* @var ImportRunner
*/
private $importRunner;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct(
ImportBuilder $importBuilder,
ImportRunner $importRunner,
EventDispatcherInterface $eventDispatcher)
{
parent::__construct('importengine:import');
$this->importBuilder = $importBuilder;
$this->importRunner = $importRunner;
$this->eventDispatcher = $eventDispatcher;
}
protected function configure()
{
$this
->setDescription('Imports data with a definied importer')
->addArgument('source_id', InputArgument::OPTIONAL, "id of source. Different StorageProviders need different id data.\n- upload, directory: \"<path/to/file>\"\n- doctrine: \"<id of query>\"\n- service: \"<service>.<method>[?arguments_like_url_query]\"")
->addArgument('source_provider', InputArgument::OPTIONAL, 'id of source provider', 'default')
->addOption('importer', 'i', InputOption::VALUE_REQUIRED, 'id/name of importer')
->addOption('context', 'c', InputOption::VALUE_REQUIRED, 'Supply optional context information to import. Supply key-value data in query style: key=value&otherkey=othervalue&...')
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit imported rows')
->addOption('dryrun', 'd', InputOption::VALUE_NONE, 'Do not import - Validation only')
->addOption('validate-and-run', null, InputOption::VALUE_NONE, 'Validate and run if no error')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$importerId = $input->getOption('importer');
$sourceProviderId = $input->getArgument('source_provider');
$sourceId = $input->getArgument('source_id');
$isDryrun = $input->getOption('dryrun');
$isValidateAndRun = $input->getOption('validate-and-run');
if ($isDryrun && $isValidateAndRun) {
throw new \InvalidArgumentException("Cannot invoke with dryrun and validate-and-run");
}
$runMode = $isDryrun ? 'dryrun' : ($isValidateAndRun ? 'validate_and_run' : 'run');
if ($context = $input->getOption('context')) {
//parse key=value&key=value string to array
if (strpos($context, '=') !== false) {
parse_str($input->getOption('context'), $context);
}
}
$limit = $input->getOption('limit');
if (empty($importerId) && empty($sourceId)) {
throw new \InvalidArgumentException('There must be at least an importerId with a configured source-definition given or a sourceId which can be automatically recognized by pre-conditions.');
}
$this->import($output, $importerId, $sourceProviderId, $sourceId, $context, $limit, $runMode);
}
protected function import(OutputInterface $output, $importerId, $sourceProviderId, $sourceId, $context = null, $limit = null, $runMode = 'run')
{
$output->writeln("Commencing import with mode <comment>$runMode</comment> using importer ".(empty($importerId) ? '<comment>unknown</comment>' : "<info>$importerId</info>")." with source provider <info>$sourceProviderId</info> and source id <info>$sourceId</info>");
$sourceId = Utils::parseSourceId($sourceId);
$progress = new ProgressBar($output);
//set limit
if ($limit) {
$output->writeln("Limiting import to <info>$limit</info> rows.");
$this->eventDispatcher->addListener(ImportConfigureEvent::AFTER_BUILD, function (ImportConfigureEvent $event) use ($limit) {
$event->getImport()->importer()->filters()->add(new OffsetFilter(0, $limit));
});
}
//show discovered importer id
if (empty($importerId)) {
$this->eventDispatcher->addListener(ImportRequestEvent::DISCOVERED, function (ImportRequestEvent $event) use ($output) {
$importerId = $event->getImportRequest()->getImporterId();
$output->writeln("Importer discovered: <info>$importerId</info>");
});
}
$importRequest = new ImportRequest($sourceId, $sourceProviderId, $importerId, Utils::whoAmI().'@CLI', $context);
$import = $this->importBuilder->buildFromRequest($importRequest);
//apply context info from commandline
$importRun = $import->getRun();
//status callback
$this->eventDispatcher->addListener(ImportItemEvent::AFTER_READ, function (ImportItemEvent $event) use ($output, &$progress) {
/** @var ImportRun $importRun */
$importRun = $event->getContext()->getRun();
$stats = $importRun->getStatistics();
$processed = isset($stats['processed']) ? $stats['processed'] : 0;
$max = $importRun->getInfo()['count'];
if ($progress->getMaxSteps() != $max) {
$progress = new ProgressBar($output, $max);
$progress->start();
}
$progress->setProgress($processed);
});
if ($runMode === 'dryrun') {
$this->importRunner->dryRun($import);
} elseif ($runMode === 'validate_and_run') {
$this->importRunner->dryRun($import);
$this->importRunner->run($import);
} else {
$this->importRunner->run($import);
}
$progress->finish();
$output->writeln('');
$output->writeln('<info>Import done</info>');
$output->writeln('');
$this->writeStatistics($importRun->getStatistics(), new Table($output));
$this->writeValidationViolations(
$import
->importer()
->validation()
->getViolations(),
new Table($output));
$output->writeln('');
}
protected function writeValidationViolations(array $violations, Table $table)
{
if (empty($violations)) {
return;
}
$violations = $violations['source'] + $violations['target'];
$table
->setHeaders(array('Constraint', 'Occurrences (lines)'))
;
$tree = [];
foreach ($violations as $line => $validations) {
/** @var ConstraintViolation $validation */
foreach ($validations as $validation) {
$key = $validation->__toString();
if (!isset($tree[$key])) {
$tree[$key] = [];
}
$tree[$key][] = $line;
}
}
$i = 0;
foreach ($tree as $violation => $lines) {
$table->addRow([$violation, implode(', ', Utils::numbersToRangeText($lines))]);
++$i;
if ($i === self::MAX_VIOLATION_ERRORS) {
$table->addRow(new TableSeparator());
$table->addRow(array(null, 'There are more errors...'));
break;
}
}
if ($i > 0) {
$table->render();
}
}
protected function writeStatistics(array $statistics, Table $table)
{
$rows = [];
foreach ($statistics as $k => $v) {
$rows[] = [$k, $v];
}
$table
->setHeaders(array('Statistics'))
->setRows($rows)
;
$table->render();
}
}