Skip to content

Commit cea8956

Browse files
MAGECLOUD-1424: Re-factor automatic module enabling (#120)
* MAGECLOUD-1424: Fixes issue with new modules not being enabled during di:compile by doing this: saves copy of config.php, enables all modules, then merges old copy back into config.php
1 parent 9d0bcf1 commit cea8956

File tree

11 files changed

+306
-312
lines changed

11 files changed

+306
-312
lines changed

src/Config/Shared.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\MagentoCloud\Config;
77

88
use Magento\MagentoCloud\Config\Shared\Reader;
9+
use Magento\MagentoCloud\Config\Shared\Writer;
910

1011
/**
1112
* Class Shared.
@@ -17,18 +18,26 @@ class Shared
1718
*/
1819
private $reader;
1920

21+
/**
22+
* @var Writer
23+
*/
24+
private $writer;
25+
2026
/**
2127
* @var array
2228
*/
2329
private $config;
2430

2531
/**
2632
* @param Reader $reader
33+
* @param Writer $writer
2734
*/
2835
public function __construct(
29-
Reader $reader
36+
Reader $reader,
37+
Writer $writer
3038
) {
3139
$this->reader = $reader;
40+
$this->writer = $writer;
3241
}
3342

3443
/**
@@ -37,10 +46,36 @@ public function __construct(
3746
* @return mixed|null
3847
*/
3948
public function get(string $key, $default = null)
49+
{
50+
return $this->read()[$key] ?? $default;
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function read(): array
4057
{
4158
if ($this->config === null) {
4259
$this->config = $this->reader->read();
4360
}
44-
return $this->config[$key] ?? $default;
61+
62+
return $this->config;
63+
}
64+
65+
/**
66+
* @param array $config
67+
*/
68+
public function update(array $config)
69+
{
70+
$this->reset();
71+
$this->writer->update($config);
72+
}
73+
74+
/**
75+
* Resets cached data.
76+
*/
77+
public function reset()
78+
{
79+
$this->config = null;
4580
}
4681
}

src/Config/Shared/Writer.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Config\Shared;
7+
8+
use Magento\MagentoCloud\Filesystem\Driver\File;
9+
use Magento\MagentoCloud\Filesystem\FileList;
10+
use Magento\MagentoCloud\Filesystem\Writer\WriterInterface;
11+
12+
/**
13+
* @inheritdoc
14+
*/
15+
class Writer implements WriterInterface
16+
{
17+
/**
18+
* @var Reader
19+
*/
20+
private $reader;
21+
22+
/**
23+
* @var File
24+
*/
25+
private $file;
26+
27+
/**
28+
* @var FileList
29+
*/
30+
private $fileList;
31+
32+
/**
33+
* @param Reader $reader ,
34+
* @param File $file
35+
* @param FileList $fileList
36+
*/
37+
public function __construct(
38+
Reader $reader,
39+
File $file,
40+
FileList $fileList
41+
) {
42+
$this->reader = $reader;
43+
$this->file = $file;
44+
$this->fileList = $fileList;
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function create(array $config)
51+
{
52+
$updatedConfig = '<?php' . PHP_EOL . 'return ' . var_export($config, true) . ';';
53+
54+
$this->file->filePutContents($this->fileList->getConfig(), $updatedConfig);
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function update(array $config)
61+
{
62+
$this->create(
63+
array_replace_recursive($this->reader->read(), $config)
64+
);
65+
}
66+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Filesystem\Writer;
7+
8+
/**
9+
* Write content of file.
10+
*/
11+
interface WriterInterface
12+
{
13+
/**
14+
* Writes given configuration to file.
15+
*
16+
* @param array $config
17+
* @return void
18+
*/
19+
public function create(array $config);
20+
21+
/**
22+
* Updates existence configuration.
23+
*
24+
* @param array $config
25+
* @return void
26+
*/
27+
public function update(array $config);
28+
}

src/Process/Build/PrepareModuleConfig.php

+5-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\MagentoCloud\Shell\ShellInterface;
99
use Magento\MagentoCloud\Process\ProcessInterface;
1010
use Magento\MagentoCloud\Config\Shared as SharedConfig;
11-
use Magento\MagentoCloud\Util\ModuleInformation;
1211
use Psr\Log\LoggerInterface;
1312

1413
/**
@@ -26,11 +25,6 @@ class PrepareModuleConfig implements ProcessInterface
2625
*/
2726
private $shell;
2827

29-
/**
30-
* @var moduleInformation
31-
*/
32-
private $moduleInformation;
33-
3428
/**
3529
* @var LoggerInterface
3630
*/
@@ -39,18 +33,15 @@ class PrepareModuleConfig implements ProcessInterface
3933
/**
4034
* @param SharedConfig $sharedConfig
4135
* @param ShellInterface $shell
42-
* @param moduleInformation $moduleInformation
4336
* @param LoggerInterface $logger
4437
*/
4538
public function __construct(
4639
SharedConfig $sharedConfig,
4740
ShellInterface $shell,
48-
moduleInformation $moduleInformation,
4941
LoggerInterface $logger
5042
) {
5143
$this->sharedConfig = $sharedConfig;
5244
$this->shell = $shell;
53-
$this->moduleInformation = $moduleInformation;
5445
$this->logger = $logger;
5546
}
5647

@@ -62,21 +53,16 @@ public function execute()
6253
$this->logger->info('Reconciling installed modules with shared config.');
6354
$moduleConfig = $this->sharedConfig->get('modules');
6455

65-
if (empty($moduleConfig)) {
56+
if (!$moduleConfig) {
6657
$this->logger->info('Shared config file is missing module section. Updating with all installed modules.');
6758
$this->shell->execute('php bin/magento module:enable --all');
68-
return;
69-
}
70-
71-
$newModules = $this->moduleInformation->getNewModuleNames();
59+
$this->sharedConfig->reset();
7260

73-
if (empty($newModules)) {
74-
$this->logger->info('All installed modules present in shared config.');
7561
return;
7662
}
7763

78-
$this->logger->info('Enabling newly installed modules not found in shared config.');
79-
$enableModules = join(" ", $newModules);
80-
$this->shell->execute("php bin/magento module:enable $enableModules");
64+
$actualConfig = $this->sharedConfig->read();
65+
$this->shell->execute('php bin/magento module:enable --all');
66+
$this->sharedConfig->update($actualConfig);
8167
}
8268
}

src/Test/Integration/AcceptanceTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class AcceptanceTest extends TestCase
2828
protected function setUp()
2929
{
3030
$this->bootstrap = Bootstrap::create();
31-
32-
$this->bootstrap->execute(sprintf(
33-
'cd %s && php bin/magento module:enable --all',
34-
$this->bootstrap->getSandboxDir()
35-
));
3631
}
3732

3833
/**

src/Test/Integration/UpgradeTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ class UpgradeTest extends TestCase
2626
protected function setUp()
2727
{
2828
$this->bootstrap = Bootstrap::create();
29-
30-
$this->bootstrap->execute(sprintf(
31-
'cd %s && php bin/magento module:enable --all',
32-
$this->bootstrap->getSandboxDir()
33-
));
3429
}
3530

3631
/**

0 commit comments

Comments
 (0)