Skip to content

Commit f7f929f

Browse files
committed
Merge branch 'MAGETWO-95595' of github.com:magento-arcticfoxes/magento2ce into MAGETWO-95595
2 parents a809498 + e17662c commit f7f929f

File tree

20 files changed

+839
-383
lines changed

20 files changed

+839
-383
lines changed

app/code/Magento/Developer/Console/Command/TablesWhitelistGenerateCommand.php

Lines changed: 11 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77

88
namespace Magento\Developer\Console\Command;
99

10-
use Magento\Framework\Component\ComponentRegistrar;
10+
use Magento\Developer\Model\Setup\Declaration\Schema\WhitelistGenerator;
1111
use Magento\Framework\Config\FileResolverByModule;
12-
use Magento\Framework\Module\Dir;
13-
use Magento\Framework\Setup\Declaration\Schema\Diff\Diff;
14-
use Magento\Framework\Setup\JsonPersistor;
15-
use Magento\Framework\Setup\Declaration\Schema\Declaration\ReaderComposite;
12+
use Magento\Framework\Exception\ConfigurationMismatchException;
1613
use Symfony\Component\Console\Command\Command;
1714
use Symfony\Component\Console\Input\InputInterface;
1815
use Symfony\Component\Console\Input\InputOption;
@@ -31,41 +28,20 @@ class TablesWhitelistGenerateCommand extends Command
3128
const MODULE_NAME_KEY = 'module-name';
3229

3330
/**
34-
* @var ComponentRegistrar
31+
* @var WhitelistGenerator
3532
*/
36-
private $componentRegistrar;
33+
private $whitelistGenerator;
3734

3835
/**
39-
* @var ReaderComposite
40-
*/
41-
private $readerComposite;
42-
43-
/**
44-
* @var JsonPersistor
45-
*/
46-
private $jsonPersistor;
47-
48-
/**
49-
* @var array
50-
*/
51-
private $primaryDbSchema;
52-
53-
/**
54-
* @param ComponentRegistrar $componentRegistrar
55-
* @param ReaderComposite $readerComposite
56-
* @param JsonPersistor $jsonPersistor
36+
* @param WhitelistGenerator $whitelistGenerator
5737
* @param string|null $name
5838
*/
5939
public function __construct(
60-
ComponentRegistrar $componentRegistrar,
61-
ReaderComposite $readerComposite,
62-
JsonPersistor $jsonPersistor,
40+
WhitelistGenerator $whitelistGenerator,
6341
$name = null
6442
) {
43+
$this->whitelistGenerator = $whitelistGenerator;
6544
parent::__construct($name);
66-
$this->componentRegistrar = $componentRegistrar;
67-
$this->readerComposite = $readerComposite;
68-
$this->jsonPersistor = $jsonPersistor;
6945
}
7046

7147
/**
@@ -93,38 +69,6 @@ protected function configure()
9369
parent::configure();
9470
}
9571

96-
/**
97-
* Update whitelist tables for all modules that are enabled on the moment.
98-
*
99-
* @param string $moduleName
100-
* @return void
101-
*/
102-
private function persistModule($moduleName)
103-
{
104-
$content = [];
105-
$modulePath = $this->componentRegistrar->getPath('module', $moduleName);
106-
$whiteListFileName = $modulePath
107-
. DIRECTORY_SEPARATOR
108-
. Dir::MODULE_ETC_DIR
109-
. DIRECTORY_SEPARATOR
110-
. Diff::GENERATED_WHITELIST_FILE_NAME;
111-
//We need to load whitelist file and update it with new revision of code.
112-
if (file_exists($whiteListFileName)) {
113-
$content = json_decode(file_get_contents($whiteListFileName), true);
114-
}
115-
116-
$newContent = $this->filterPrimaryTables($this->readerComposite->read($moduleName));
117-
118-
//Do merge between what we have before, and what we have now and filter to only certain attributes.
119-
$content = array_replace_recursive(
120-
$content,
121-
$this->filterAttributeNames($newContent)
122-
);
123-
if (!empty($content)) {
124-
$this->jsonPersistor->persist($content, $whiteListFileName);
125-
}
126-
}
127-
12872
/**
12973
* @inheritdoc
13074
*/
@@ -133,77 +77,14 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
13377
$moduleName = $input->getOption(self::MODULE_NAME_KEY);
13478

13579
try {
136-
if ($moduleName === FileResolverByModule::ALL_MODULES) {
137-
foreach (array_keys($this->componentRegistrar->getPaths('module')) as $moduleName) {
138-
$this->persistModule($moduleName);
139-
}
140-
} else {
141-
$this->persistModule($moduleName);
142-
}
80+
$this->whitelistGenerator->generate($moduleName);
81+
} catch (ConfigurationMismatchException $e) {
82+
$output->writeln("<info>". $e . "</info>");
83+
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
14384
} catch (\Exception $e) {
14485
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
14586
}
14687

147-
//If script comes here, that we sucessfully write whitelist configuration
14888
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
14989
}
150-
151-
/**
152-
* Filter attribute names
153-
*
154-
* As for whitelist we do not need any specific attributes like nullable or indexType, we need to choose only names.
155-
*
156-
* @param array $content
157-
* @return array
158-
*/
159-
private function filterAttributeNames(array $content) : array
160-
{
161-
$names = [];
162-
$types = ['column', 'index', 'constraint'];
163-
164-
foreach ($content['table'] as $tableName => $tableContent) {
165-
foreach ($types as $type) {
166-
if (isset($tableContent[$type])) {
167-
//Add elements to whitelist
168-
foreach (array_keys($tableContent[$type]) as $elementName) {
169-
//Depends on flag column will be whitelisted or not
170-
$names[$tableName][$type][$elementName] = true;
171-
}
172-
}
173-
}
174-
}
175-
176-
return $names;
177-
}
178-
179-
/**
180-
* Load db_schema content from the primary scope app/etc/db_schema.xml.
181-
*
182-
* @return array
183-
*/
184-
private function getPrimaryDbSchema()
185-
{
186-
if (!$this->primaryDbSchema) {
187-
$this->primaryDbSchema = $this->readerComposite->read('primary');
188-
}
189-
return $this->primaryDbSchema;
190-
}
191-
192-
/**
193-
* Filter tables from module db_schema.xml as they should not contain the primary system tables.
194-
*
195-
* @param array $moduleDbSchema
196-
* @return array
197-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
198-
*/
199-
private function filterPrimaryTables(array $moduleDbSchema)
200-
{
201-
$primaryDbSchema = $this->getPrimaryDbSchema();
202-
if (isset($moduleDbSchema['table']) && isset($primaryDbSchema['table'])) {
203-
foreach ($primaryDbSchema['table'] as $tableNameKey => $tableContents) {
204-
unset($moduleDbSchema['table'][$tableNameKey]);
205-
}
206-
}
207-
return $moduleDbSchema;
208-
}
20990
}

0 commit comments

Comments
 (0)