1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Notamedia\ConsoleJedi\Iblock\Command;
|
4
|
|
|
|
5
|
|
|
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
|
6
|
|
|
use Notamedia\ConsoleJedi\Iblock\Exception\IblockException;
|
7
|
|
|
use Notamedia\ConsoleJedi\Iblock\Exporter;
|
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 export information block(s) in xml file(s)
|
17
|
|
|
*/
|
18
|
|
|
class ExportCommand extends BitrixCommand
|
19
|
|
|
{
|
20
|
|
|
use MigrationCommandTrait;
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* {@inheritdoc}
|
24
|
|
|
*/
|
25
|
|
View Code Duplication |
protected function configure()
|
|
|
|
|
26
|
|
|
{
|
27
|
|
|
$this
|
28
|
|
|
->setName('iblock:export')
|
29
|
|
|
->setDescription('Export information block(s) to xml')
|
30
|
|
|
->addArgument(
|
31
|
|
|
'type',
|
32
|
|
|
InputArgument::REQUIRED,
|
33
|
|
|
'Information block type'
|
34
|
|
|
)
|
35
|
|
|
->addArgument(
|
36
|
|
|
'code',
|
37
|
|
|
InputArgument::REQUIRED,
|
38
|
|
|
'Information block code'
|
39
|
|
|
)
|
40
|
|
|
->addArgument(
|
41
|
|
|
'dir',
|
42
|
|
|
InputArgument::OPTIONAL,
|
43
|
|
|
'Directory to export'
|
44
|
|
|
)
|
45
|
|
|
->addOption(
|
46
|
|
|
'sections',
|
47
|
|
|
's',
|
48
|
|
|
InputOption::VALUE_OPTIONAL,
|
49
|
|
|
'Export sections [ "active", "all", "none" ]',
|
50
|
|
|
'none'
|
51
|
|
|
)
|
52
|
|
|
->addOption(
|
53
|
|
|
'elements',
|
54
|
|
|
'e',
|
55
|
|
|
InputOption::VALUE_OPTIONAL,
|
56
|
|
|
'Export elements [ "active", "all", "none" ]',
|
57
|
|
|
'none'
|
58
|
|
|
);
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* {@inheritdoc}
|
63
|
|
|
*/
|
64
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output)
|
65
|
|
|
{
|
66
|
|
|
Loader::includeModule('iblock');
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* {@inheritdoc}
|
71
|
|
|
*/
|
72
|
|
|
protected function interact(InputInterface $input, OutputInterface $output)
|
73
|
|
|
{
|
74
|
|
|
$this->setDir($input);
|
75
|
|
|
$this->setIblocks($input);
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* {@inheritdoc}
|
80
|
|
|
*/
|
81
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
82
|
|
|
{
|
83
|
|
|
$formatter = new FormatterHelper();
|
84
|
|
|
|
85
|
|
View Code Duplication |
if (count($this->errors) > 0) {
|
|
|
|
|
86
|
|
|
$output->writeln($formatter->formatBlock($this->errors, 'error'));
|
87
|
|
|
return false;
|
|
|
|
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
$exporter = new Exporter();
|
91
|
|
|
$exporter
|
92
|
|
|
->setSections($input->getOption('sections'))
|
93
|
|
|
->setElements($input->getOption('elements'));
|
94
|
|
|
|
95
|
|
|
foreach ($this->iblocks as $iblock) {
|
96
|
|
|
|
97
|
|
|
try {
|
98
|
|
|
$xml_id = \CIBlockCMLExport::GetIBlockXML_ID($iblock['ID']);
|
99
|
|
|
$path = implode(DIRECTORY_SEPARATOR, [$this->dir, $xml_id]) . $this->extension;
|
100
|
|
|
|
101
|
|
|
$exporter
|
102
|
|
|
->setPath($path)
|
103
|
|
|
->setId($iblock['ID'])
|
104
|
|
|
->execute();
|
105
|
|
|
|
106
|
|
|
$output->writeln(sprintf('<info>%s</info> iblock %s %s', 'success', $iblock['CODE'], $path));
|
107
|
|
|
|
108
|
|
|
} catch (IblockException $e) {
|
109
|
|
|
$output->writeln(sprintf('<error>%s</error> iblock %s', 'fail', $iblock['CODE']));
|
110
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
111
|
|
|
$output->writeln($e->getMessage());
|
112
|
|
|
}
|
113
|
|
|
}
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
return true;
|
|
|
|
|
117
|
|
|
}
|
118
|
|
|
} |
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.