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 ImportCommand extends BitrixCommand
|
16
|
|
|
{
|
17
|
|
|
use CommandTrait;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* {@inheritdoc}
|
21
|
|
|
*/
|
22
|
|
View Code Duplication |
protected function configure()
|
|
|
|
|
23
|
|
|
{
|
24
|
|
|
$this
|
25
|
|
|
->setName('schema:import')
|
26
|
|
|
->setDescription('Import information block(s) from xml')
|
27
|
|
|
->addArgument(
|
28
|
|
|
'type',
|
29
|
|
|
InputArgument::REQUIRED,
|
30
|
|
|
'Information iblock type'
|
31
|
|
|
)
|
32
|
|
|
->addArgument(
|
33
|
|
|
'sites',
|
34
|
|
|
InputArgument::REQUIRED,
|
35
|
|
|
'Sites to which the information iblock will be bound (if it is to be created)'
|
36
|
|
|
)
|
37
|
|
|
->addArgument(
|
38
|
|
|
'dir',
|
39
|
|
|
InputArgument::OPTIONAL,
|
40
|
|
|
'Directory to import'
|
41
|
|
|
)
|
42
|
|
|
->addOption(
|
43
|
|
|
'sections',
|
44
|
|
|
's',
|
45
|
|
|
InputOption::VALUE_OPTIONAL,
|
46
|
|
|
'If an existing section is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
|
47
|
|
|
'A'
|
48
|
|
|
)
|
49
|
|
|
->addOption(
|
50
|
|
|
'elements',
|
51
|
|
|
'e',
|
52
|
|
|
InputOption::VALUE_OPTIONAL,
|
53
|
|
|
'If an existing element is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
|
54
|
|
|
'A'
|
55
|
|
|
);
|
56
|
|
|
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* {@inheritdoc}
|
61
|
|
|
*/
|
62
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output)
|
63
|
|
|
{
|
64
|
|
|
Loader::includeModule('iblock');
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* {@inheritdoc}
|
70
|
|
|
*/
|
71
|
|
|
protected function interact(InputInterface $input, OutputInterface $output)
|
72
|
|
|
{
|
73
|
|
|
$this->setDir($input);
|
74
|
|
|
$this->setType($input);
|
75
|
|
|
$this->setSites($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
|
|
|
$import = Schema::import()
|
91
|
|
|
->setType($this->type)
|
92
|
|
|
->setSites($this->sites)
|
93
|
|
|
->setActionSection($input->getOption('sections'))
|
94
|
|
|
->setActionElement($input->getOption('elements'));;
|
95
|
|
|
|
96
|
|
|
foreach (glob(implode(DIRECTORY_SEPARATOR . '*', [$this->dir, $this->extension])) as $file) {
|
97
|
|
|
|
98
|
|
|
try {
|
99
|
|
|
$import
|
100
|
|
|
->setPath($file)
|
101
|
|
|
->execute();
|
102
|
|
|
|
103
|
|
|
$output->writeln(sprintf('<info>%s</info> file %s', 'success', $file));
|
104
|
|
|
|
105
|
|
|
} catch (SchemaException $e) {
|
106
|
|
|
$output->writeln(sprintf('<error>%s</error> file %s', 'fail', $file));
|
107
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
108
|
|
|
$output->writeln($e->getMessage());
|
109
|
|
|
}
|
110
|
|
|
}
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
} |
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.