Skip to content

Commit

Permalink
MAGECLOUD-1424: Re-factor automatic module enabling (#120)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JacobBrownAustin authored Dec 15, 2017
1 parent 9d0bcf1 commit cea8956
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 312 deletions.
39 changes: 37 additions & 2 deletions src/Config/Shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\MagentoCloud\Config;

use Magento\MagentoCloud\Config\Shared\Reader;
use Magento\MagentoCloud\Config\Shared\Writer;

/**
* Class Shared.
Expand All @@ -17,18 +18,26 @@ class Shared
*/
private $reader;

/**
* @var Writer
*/
private $writer;

/**
* @var array
*/
private $config;

/**
* @param Reader $reader
* @param Writer $writer
*/
public function __construct(
Reader $reader
Reader $reader,
Writer $writer
) {
$this->reader = $reader;
$this->writer = $writer;
}

/**
Expand All @@ -37,10 +46,36 @@ public function __construct(
* @return mixed|null
*/
public function get(string $key, $default = null)
{
return $this->read()[$key] ?? $default;
}

/**
* @return array
*/
public function read(): array
{
if ($this->config === null) {
$this->config = $this->reader->read();
}
return $this->config[$key] ?? $default;

return $this->config;
}

/**
* @param array $config
*/
public function update(array $config)
{
$this->reset();
$this->writer->update($config);
}

/**
* Resets cached data.
*/
public function reset()
{
$this->config = null;
}
}
66 changes: 66 additions & 0 deletions src/Config/Shared/Writer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\Config\Shared;

use Magento\MagentoCloud\Filesystem\Driver\File;
use Magento\MagentoCloud\Filesystem\FileList;
use Magento\MagentoCloud\Filesystem\Writer\WriterInterface;

/**
* @inheritdoc
*/
class Writer implements WriterInterface
{
/**
* @var Reader
*/
private $reader;

/**
* @var File
*/
private $file;

/**
* @var FileList
*/
private $fileList;

/**
* @param Reader $reader ,
* @param File $file
* @param FileList $fileList
*/
public function __construct(
Reader $reader,
File $file,
FileList $fileList
) {
$this->reader = $reader;
$this->file = $file;
$this->fileList = $fileList;
}

/**
* @inheritdoc
*/
public function create(array $config)
{
$updatedConfig = '<?php' . PHP_EOL . 'return ' . var_export($config, true) . ';';

$this->file->filePutContents($this->fileList->getConfig(), $updatedConfig);
}

/**
* @inheritdoc
*/
public function update(array $config)
{
$this->create(
array_replace_recursive($this->reader->read(), $config)
);
}
}
28 changes: 28 additions & 0 deletions src/Filesystem/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\Filesystem\Writer;

/**
* Write content of file.
*/
interface WriterInterface
{
/**
* Writes given configuration to file.
*
* @param array $config
* @return void
*/
public function create(array $config);

/**
* Updates existence configuration.
*
* @param array $config
* @return void
*/
public function update(array $config);
}
24 changes: 5 additions & 19 deletions src/Process/Build/PrepareModuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Magento\MagentoCloud\Shell\ShellInterface;
use Magento\MagentoCloud\Process\ProcessInterface;
use Magento\MagentoCloud\Config\Shared as SharedConfig;
use Magento\MagentoCloud\Util\ModuleInformation;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -26,11 +25,6 @@ class PrepareModuleConfig implements ProcessInterface
*/
private $shell;

/**
* @var moduleInformation
*/
private $moduleInformation;

/**
* @var LoggerInterface
*/
Expand All @@ -39,18 +33,15 @@ class PrepareModuleConfig implements ProcessInterface
/**
* @param SharedConfig $sharedConfig
* @param ShellInterface $shell
* @param moduleInformation $moduleInformation
* @param LoggerInterface $logger
*/
public function __construct(
SharedConfig $sharedConfig,
ShellInterface $shell,
moduleInformation $moduleInformation,
LoggerInterface $logger
) {
$this->sharedConfig = $sharedConfig;
$this->shell = $shell;
$this->moduleInformation = $moduleInformation;
$this->logger = $logger;
}

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

if (empty($moduleConfig)) {
if (!$moduleConfig) {
$this->logger->info('Shared config file is missing module section. Updating with all installed modules.');
$this->shell->execute('php bin/magento module:enable --all');
return;
}

$newModules = $this->moduleInformation->getNewModuleNames();
$this->sharedConfig->reset();

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

$this->logger->info('Enabling newly installed modules not found in shared config.');
$enableModules = join(" ", $newModules);
$this->shell->execute("php bin/magento module:enable $enableModules");
$actualConfig = $this->sharedConfig->read();
$this->shell->execute('php bin/magento module:enable --all');
$this->sharedConfig->update($actualConfig);
}
}
5 changes: 0 additions & 5 deletions src/Test/Integration/AcceptanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class AcceptanceTest extends TestCase
protected function setUp()
{
$this->bootstrap = Bootstrap::create();

$this->bootstrap->execute(sprintf(
'cd %s && php bin/magento module:enable --all',
$this->bootstrap->getSandboxDir()
));
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Test/Integration/UpgradeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ class UpgradeTest extends TestCase
protected function setUp()
{
$this->bootstrap = Bootstrap::create();

$this->bootstrap->execute(sprintf(
'cd %s && php bin/magento module:enable --all',
$this->bootstrap->getSandboxDir()
));
}

/**
Expand Down
Loading

0 comments on commit cea8956

Please sign in to comment.