-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImportCommand.php
More file actions
201 lines (166 loc) · 8.02 KB
/
ImportCommand.php
File metadata and controls
201 lines (166 loc) · 8.02 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
<?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\Exception\InvalidConfigurationException;
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\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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\Validator\ConstraintViolation;
class ImportCommand extends ContainerAwareCommand
{
const MAX_VIOLATION_ERRORS = 10;
protected function configure()
{
$this->setName('importengine:import')
->setDescription('Imports data with a definied importer')
->addArgument('source_id', InputArgument::REQUIRED, "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('offset', 'o', InputOption::VALUE_REQUIRED, 'Offset imported rows', 0)
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit imported rows')
->addOption('dryrun', 'd', InputOption::VALUE_NONE, 'Do not import - Validation only')
;
}
protected function validateInput(InputInterface $input)
{
if (!$this->getContainer()->has('mathielen_importengine.import.builder') ||
!$this->getContainer()->has('mathielen_importengine.import.runner')) {
throw new InvalidConfigurationException("No importengine services have been found. Did you register the bundle in AppKernel and configured at least one importer in config?");
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input);
$importerId = $input->getOption('importer');
$sourceProviderId = $input->getArgument('source_provider');
$sourceId = $input->getArgument('source_id');
$isDryrun = $input->getOption('dryrun');
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');
$offset = $input->getOption('offset');
$this->import($output, $importerId, $sourceProviderId, $sourceId, $context, $offset, $limit, $isDryrun);
}
protected function import(OutputInterface $output, $importerId, $sourceProviderId, $sourceId, $context=null, $offset=0, $limit=null, $isDryrun=false)
{
$output->writeln("Commencing ".($isDryrun?'<comment>dry-run</comment> ':'')."import 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->getContainer()->get('event_dispatcher')->addListener(ImportConfigureEvent::AFTER_BUILD, function (ImportConfigureEvent $event) use ($offset, $limit) {
$event->getImport()->importer()->filters()->add(new OffsetFilter($offset, $limit));
});
}
//show discovered importer id
if (empty($importerId)) {
$this->getContainer()->get('event_dispatcher')->addListener(ImportRequestEvent::DISCOVERED, function (ImportRequestEvent $event) use ($output) {
$importerId = $event->getImportRequest()->getImporterId();
$output->writeln("Importer discovered: <info>$importerId</info>");
});
}
/** @var ImportBuilder $importBuilder */
$importBuilder = $this->getContainer()->get('mathielen_importengine.import.builder');
$importRequest = new ImportRequest($sourceId, $sourceProviderId, $importerId, Utils::whoAmI().'@CLI', $context);
$import = $importBuilder->buildFromRequest($importRequest);
//apply context info from commandline
$importRun = $import->getRun();
//status callback
$this->getContainer()->get('event_dispatcher')->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);
});
/** @var ImportRunner $importRunner */
$importRunner = $this->getContainer()->get('mathielen_importengine.import.runner');
if ($isDryrun) {
$importRunner->dryRun($import);
} else {
$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, join(', ', 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();
}
}