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