1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Notamedia\ConsoleJedi\Iblock\Command;
|
4
|
|
|
|
5
|
|
|
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
|
6
|
|
|
use Notamedia\ConsoleJedi\Iblock\Exception\SchemaException;
|
7
|
|
|
use Notamedia\ConsoleJedi\Iblock\Importer;
|
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\FormatterHelper;
|
13
|
|
|
use Bitrix\Main\Loader;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* Command import information block(s) from xml file(s)
|
17
|
|
|
*/
|
18
|
|
|
class ImportCommand extends BitrixCommand
|
19
|
|
|
{
|
20
|
|
|
use MigrationCommandTrait;
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* {@inheritdoc}
|
24
|
|
|
*/
|
25
|
|
View Code Duplication |
protected function configure()
|
|
|
|
|
26
|
|
|
{
|
27
|
|
|
$this
|
28
|
|
|
->setName('iblock:import')
|
29
|
|
|
->setDescription('Import information block(s) from xml')
|
30
|
|
|
->addArgument(
|
31
|
|
|
'type',
|
32
|
|
|
InputArgument::REQUIRED,
|
33
|
|
|
'Information block type'
|
34
|
|
|
)
|
35
|
|
|
->addArgument(
|
36
|
|
|
'sites',
|
37
|
|
|
InputArgument::REQUIRED,
|
38
|
|
|
'Sites to which the information block will be bound (if it is to be created)'
|
39
|
|
|
)
|
40
|
|
|
->addArgument(
|
41
|
|
|
'dir',
|
42
|
|
|
InputArgument::OPTIONAL,
|
43
|
|
|
'Directory to import'
|
44
|
|
|
)
|
45
|
|
|
->addOption(
|
46
|
|
|
'sections',
|
47
|
|
|
's',
|
48
|
|
|
InputOption::VALUE_OPTIONAL,
|
49
|
|
|
'If an existing section is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
|
50
|
|
|
'A'
|
51
|
|
|
)
|
52
|
|
|
->addOption(
|
53
|
|
|
'elements',
|
54
|
|
|
'e',
|
55
|
|
|
InputOption::VALUE_OPTIONAL,
|
56
|
|
|
'If an existing element is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
|
57
|
|
|
'A'
|
58
|
|
|
);
|
59
|
|
|
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* {@inheritdoc}
|
64
|
|
|
*/
|
65
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output)
|
66
|
|
|
{
|
67
|
|
|
Loader::includeModule('iblock');
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* {@inheritdoc}
|
73
|
|
|
*/
|
74
|
|
|
protected function interact(InputInterface $input, OutputInterface $output)
|
75
|
|
|
{
|
76
|
|
|
$this->setDir($input);
|
77
|
|
|
$this->setType($input);
|
78
|
|
|
$this->setSites($input);
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* {@inheritdoc}
|
83
|
|
|
*/
|
84
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
85
|
|
|
{
|
86
|
|
|
$formatter = new FormatterHelper();
|
87
|
|
|
|
88
|
|
View Code Duplication |
if (count($this->errors) > 0) {
|
|
|
|
|
89
|
|
|
$output->writeln($formatter->formatBlock($this->errors, 'error'));
|
90
|
|
|
return false;
|
|
|
|
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
$importer = new Importer();
|
94
|
|
|
$importer
|
95
|
|
|
->setType($this->type)
|
96
|
|
|
->setSites($this->sites)
|
97
|
|
|
->setActionSection($input->getOption('sections'))
|
98
|
|
|
->setActionElement($input->getOption('elements'));;
|
99
|
|
|
|
100
|
|
|
foreach (glob(implode(DIRECTORY_SEPARATOR . '*', [$this->dir, $this->extension])) as $file) {
|
101
|
|
|
|
102
|
|
|
try {
|
103
|
|
|
$importer
|
104
|
|
|
->setPath($file)
|
105
|
|
|
->execute();
|
106
|
|
|
|
107
|
|
|
$output->writeln(sprintf('<info>%s</info> file %s', 'success', $file));
|
108
|
|
|
|
109
|
|
|
} catch (SchemaException $e) {
|
110
|
|
|
$output->writeln(sprintf('<error>%s</error> file %s', 'fail', $file));
|
111
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
112
|
|
|
$output->writeln($e->getMessage());
|
113
|
|
|
}
|
114
|
|
|
}
|
115
|
|
|
}
|
116
|
|
|
}
|
117
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.