Skip to content

Commit 402583e

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'falcons/MAGETWO-65208' into MAGETWO-65701
2 parents f1dc91b + 56d544f commit 402583e

File tree

24 files changed

+613
-240
lines changed

24 files changed

+613
-240
lines changed

app/code/Magento/Deploy/Console/Command/App/ConfigImport/Importer.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
99
use Magento\Framework\App\DeploymentConfig;
10-
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Exception\RuntimeException;
1111
use Psr\Log\LoggerInterface as Logger;
1212
use Magento\Deploy\Model\DeploymentConfig\Validator;
1313
use Magento\Deploy\Model\DeploymentConfig\ImporterPool;
1414
use Magento\Deploy\Model\DeploymentConfig\Hash;
1515
use Symfony\Component\Console\Output\OutputInterface;
16+
use Magento\Deploy\Model\DeploymentConfig\ImporterFactory;
1617

1718
/**
1819
* Runs importing of config data from deployment configuration files.
@@ -47,6 +48,13 @@ class Importer
4748
*/
4849
private $configHash;
4950

51+
/**
52+
* Factory for creation of importer instance.
53+
*
54+
* @var ImporterFactory
55+
*/
56+
private $importerFactory;
57+
5058
/**
5159
* Logger.
5260
*
@@ -57,19 +65,22 @@ class Importer
5765
/**
5866
* @param Validator $configValidator the manager of deployment configuration hash
5967
* @param ImporterPool $configImporterPool the pool of all deployment configuration importers
68+
* @param ImporterFactory $importerFactory the factory for creation of importer instance
6069
* @param DeploymentConfig $deploymentConfig the application deployment configuration
6170
* @param Hash $configHash the hash updater of config data
6271
* @param Logger $logger the logger
6372
*/
6473
public function __construct(
6574
Validator $configValidator,
6675
ImporterPool $configImporterPool,
76+
ImporterFactory $importerFactory,
6777
DeploymentConfig $deploymentConfig,
6878
Hash $configHash,
6979
Logger $logger
7080
) {
7181
$this->configValidator = $configValidator;
7282
$this->configImporterPool = $configImporterPool;
83+
$this->importerFactory = $importerFactory;
7384
$this->deploymentConfig = $deploymentConfig;
7485
$this->configHash = $configHash;
7586
$this->logger = $logger;
@@ -80,32 +91,35 @@ public function __construct(
8091
*
8192
* @param OutputInterface $output the CLI output
8293
* @return void
83-
* @throws LocalizedException
94+
* @throws RuntimeException is thrown when import has failed
8495
*/
8596
public function import(OutputInterface $output)
8697
{
87-
$output->writeln('<info>Start import:</info>');
88-
8998
try {
9099
$importers = $this->configImporterPool->getImporters();
91-
92100
if (!$importers || $this->configValidator->isValid()) {
93-
$output->writeln('<info>Nothing to import</info>');
101+
$output->writeln('<info>Nothing to import.</info>');
102+
return;
94103
} else {
95-
/**
96-
* @var string $namespace
97-
* @var ImporterInterface $importer
98-
*/
99-
foreach ($importers as $namespace => $importer) {
100-
$messages = $importer->import($this->deploymentConfig->getConfigData($namespace));
104+
$output->writeln('<info>Start import:</info>');
105+
}
106+
107+
/**
108+
* @var string $section
109+
* @var string $importerClassName
110+
*/
111+
foreach ($importers as $section => $importerClassName) {
112+
if (!$this->configValidator->isValid($section)) {
113+
/** @var ImporterInterface $importer */
114+
$importer = $this->importerFactory->create($importerClassName);
115+
$messages = $importer->import((array)$this->deploymentConfig->getConfigData($section));
101116
$output->writeln($messages);
117+
$this->configHash->regenerate($section);
102118
}
103-
104-
$this->configHash->regenerate();
105119
}
106-
} catch (LocalizedException $exception) {
120+
} catch (\Exception $exception) {
107121
$this->logger->error($exception);
108-
throw new LocalizedException(__('Import is failed'), $exception);
122+
throw new RuntimeException(__('Import is failed: %1', $exception->getMessage()), $exception);
109123
}
110124
}
111125
}

app/code/Magento/Deploy/Console/Command/App/ConfigImportCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Deploy\Console\Command\App;
77

8-
use Magento\Framework\Exception\LocalizedException;
8+
use Magento\Framework\Exception\RuntimeException;
99
use Symfony\Component\Console\Command\Command;
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
@@ -27,12 +27,14 @@ class ConfigImportCommand extends Command
2727
const COMMAND_NAME = 'app:config:import';
2828

2929
/**
30+
* Configuration importer.
31+
*
3032
* @var Importer
3133
*/
3234
private $importer;
3335

3436
/**
35-
* @param Importer $importer
37+
* @param Importer $importer the configuration importer
3638
*/
3739
public function __construct(Importer $importer)
3840
{
@@ -60,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6062
{
6163
try {
6264
$this->importer->import($output);
63-
} catch (LocalizedException $e) {
65+
} catch (RuntimeException $e) {
6466
$output->writeln('<error>' . $e->getMessage() . '</error>');
6567

6668
return Cli::RETURN_FAILURE;

app/code/Magento/Deploy/Model/DeploymentConfig/DataCollector.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* <type name="Magento\Deploy\Model\DeploymentConfig\ImporterPool">
1616
* <arguments>
1717
* <argument name="importers" xsi:type="array">
18-
* <item name="scopes" xsi:type="string">Magento\Store\Model\StoreImporter</item>
18+
* <item name="scopes" xsi:type="string">Magento\SomeModule\Model\SomeImporter</item>
1919
* </argument>
2020
* </arguments>
2121
* </type>
@@ -30,12 +30,11 @@
3030
* ]
3131
* ```
3232
*
33-
* In here we define section "scopes" and its importer Magento\Store\Model\StoreImporter.
33+
* In here we define section "scopes" and its importer Magento\SomeModule\Model\SomeImporter.
3434
* The data of this section will be collected then will be used in importing process from the shared configuration
3535
* files to appropriate application sources.
3636
*
3737
* @see \Magento\Deploy\Console\Command\App\ConfigImport\Importer::import()
38-
* @see \Magento\Deploy\Model\DeploymentConfig\Hash::regenerate()
3938
*/
4039
class DataCollector
4140
{
@@ -65,6 +64,7 @@ public function __construct(ImporterPool $configImporterPool, DeploymentConfig $
6564

6665
/**
6766
* Retrieves configuration data of specific section from deployment configuration files.
67+
* Or retrieves configuration data of specific sections by its name.
6868
*
6969
* E.g.
7070
* ```php
@@ -75,19 +75,30 @@ public function __construct(ImporterPool $configImporterPool, DeploymentConfig $
7575
* ...
7676
* ]
7777
* ```
78+
*
79+
* This method retrieves the same structure for the specific section with only its data.
80+
* ```php
81+
* [
82+
* 'scopes' => [...]
83+
* ]
84+
*
7885
* In this example key of the array is the section name, value of the array is configuration data of the section.
7986
*
87+
* @param string $sectionName the section name for retrieving its configuration data
8088
* @return array
8189
*/
82-
public function getConfig()
90+
public function getConfig($sectionName = null)
8391
{
8492
$result = [];
8593

86-
foreach ($this->configImporterPool->getSections() as $section) {
87-
$data = $this->deploymentConfig->getConfigData($section);
88-
if ($data) {
89-
$result[$section] = $data;
90-
}
94+
if ($sectionName) {
95+
$sections = [$sectionName];
96+
} else {
97+
$sections = $this->configImporterPool->getSections();
98+
}
99+
100+
foreach ($sections as $section) {
101+
$result[$section] = $this->deploymentConfig->getConfigData($section);
91102
}
92103

93104
return $result;

app/code/Magento/Deploy/Model/DeploymentConfig/Hash.php

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
*/
66
namespace Magento\Deploy\Model\DeploymentConfig;
77

8-
use Magento\Framework\Config\File\ConfigFilePool;
9-
use Magento\Framework\App\DeploymentConfig\Writer;
10-
use Magento\Framework\App\DeploymentConfig;
118
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\Exception\FileSystemException;
9+
use Magento\Framework\Flag\FlagResource;
10+
use Magento\Framework\Flag;
11+
use Magento\Framework\FlagFactory;
1312

1413
/**
1514
* Saves and Retrieves deployment configuration hash.
@@ -27,77 +26,106 @@ class Hash
2726
const CONFIG_KEY = 'config_hash';
2827

2928
/**
30-
* Application deployment configuration.
29+
* Hash generator.
3130
*
32-
* @var DeploymentConfig
31+
* @var Hash\Generator
3332
*/
34-
private $deploymentConfig;
33+
private $configHashGenerator;
3534

3635
/**
37-
* Deployment configuration writer to files.
36+
* Config data collector.
3837
*
39-
* @var Writer
38+
* @var DataCollector
4039
*/
41-
private $writer;
40+
private $dataConfigCollector;
4241

4342
/**
44-
* Hash generator.
43+
* Flag Resource model.
4544
*
46-
* @var Hash\Generator
45+
* @var FlagResource
4746
*/
48-
private $configHashGenerator;
47+
private $flagResource;
4948

5049
/**
51-
* Config data collector.
50+
* Factory class for \Magento\Framework\Flag
5251
*
53-
* @var DataCollector
52+
* @var FlagFactory
5453
*/
55-
private $dataConfigCollector;
54+
private $flagFactory;
5655

5756
/**
58-
* @param DeploymentConfig $deploymentConfig the application deployment configuration
59-
* @param Writer $writer the configuration writer that writes to files
6057
* @param Hash\Generator $configHashGenerator the hash generator
6158
* @param DataCollector $dataConfigCollector the config data collector
59+
* @param FlagResource $flagResource
60+
* @param FlagFactory $flagFactory
6261
*/
6362
public function __construct(
64-
DeploymentConfig $deploymentConfig,
65-
Writer $writer,
6663
Hash\Generator $configHashGenerator,
67-
DataCollector $dataConfigCollector
64+
DataCollector $dataConfigCollector,
65+
FlagResource $flagResource,
66+
FlagFactory $flagFactory
6867
) {
69-
$this->deploymentConfig = $deploymentConfig;
70-
$this->writer = $writer;
7168
$this->configHashGenerator = $configHashGenerator;
7269
$this->dataConfigCollector = $dataConfigCollector;
70+
$this->flagResource = $flagResource;
71+
$this->flagFactory = $flagFactory;
7372
}
7473

7574
/**
7675
* Updates hash in the storage.
7776
*
78-
* The hash is generated based on data from configuration files
77+
* If the specific section name is set, then hash will be updated only for this section,
78+
* in another case hash will be updated for all sections which defined in di.xml
79+
* The hash is generated based on data from configuration files.
7980
*
81+
* @param string $sectionName the specific section name
8082
* @return void
8183
* @throws LocalizedException is thrown when hash was not saved
8284
*/
83-
public function regenerate()
85+
public function regenerate($sectionName = null)
8486
{
8587
try {
86-
$config = $this->dataConfigCollector->getConfig();
87-
$hash = $this->configHashGenerator->generate($config);
88-
$this->writer->saveConfig([ConfigFilePool::APP_ENV => [self::CONFIG_KEY => $hash]]);
89-
} catch (FileSystemException $exception) {
90-
throw new LocalizedException(__('Hash has not been saved'), $exception);
88+
$hashes = $this->get();
89+
$configs = $this->dataConfigCollector->getConfig($sectionName);
90+
91+
foreach ($configs as $section => $config) {
92+
$hashes[$section] = $this->configHashGenerator->generate($config);
93+
}
94+
95+
/** @var Flag $flag */
96+
$flag = $this->getFlagObject();
97+
$flag->setFlagData($hashes);
98+
$this->flagResource->save($flag);
99+
} catch (\Exception $exception) {
100+
throw new LocalizedException(__('Hash has not been saved.'), $exception);
91101
}
92102
}
93103

94104
/**
95-
* Retrieves saved hash from storage.
105+
* Retrieves saved hashes from storage.
96106
*
97-
* @return string|null
107+
* @return array
98108
*/
99109
public function get()
100110
{
101-
return $this->deploymentConfig->getConfigData(self::CONFIG_KEY);
111+
/** @var Flag $flag */
112+
$flag = $this->getFlagObject();
113+
return (array) ($flag->getFlagData() ?: []);
114+
}
115+
116+
/**
117+
* Returns flag object.
118+
*
119+
* We use it for saving hashes of sections in the DB.
120+
*
121+
* @return Flag
122+
*/
123+
private function getFlagObject()
124+
{
125+
/** @var Flag $flag */
126+
$flag = $this->flagFactory
127+
->create(['data' => ['flag_code' => self::CONFIG_KEY]]);
128+
$this->flagResource->load($flag, self::CONFIG_KEY, 'flag_code');
129+
return $flag;
102130
}
103131
}

0 commit comments

Comments
 (0)