Skip to content

Commit 339609d

Browse files
committed
MAGETWO-54205: After Disable/Enable Magento modules via CLI all cache types become disabled
1 parent a2a87c1 commit 339609d

File tree

4 files changed

+90
-36
lines changed

4 files changed

+90
-36
lines changed

lib/internal/Magento/Framework/App/ObjectManagerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function create(array $arguments)
111111
{
112112
$writeFactory = new \Magento\Framework\Filesystem\Directory\WriteFactory($this->driverPool);
113113
$generatedFiles = new GeneratedFiles($this->directoryList, $writeFactory);
114-
$generatedFiles->regenerate();
114+
$generatedFiles->cleanGeneratedFiles();
115115

116116
$deploymentConfig = $this->createDeploymentConfig($this->directoryList, $this->configFilePool, $arguments);
117117
$arguments = array_merge($deploymentConfig->get(), $arguments);

lib/internal/Magento/Framework/Code/GeneratedFiles.php

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Framework\Code;
77

8-
use Magento\Framework\Config\Data\ConfigData;
98
use Magento\Framework\App\Filesystem\DirectoryList;
109
use Magento\Framework\Config\File\ConfigFilePool;
1110
use Magento\Framework\Filesystem\Directory\WriteFactory;
@@ -45,54 +44,70 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
4544
}
4645

4746
/**
48-
* Clean generated code and DI configuration
47+
* Clean var/generation, var/di and var/cache
4948
*
5049
* @return void
5150
*/
52-
public function regenerate()
51+
public function cleanGeneratedFiles()
5352
{
5453
if ($this->write->isExist(self::REGENERATE_FLAG)) {
54+
5555
//TODO: to be removed in scope of MAGETWO-53476
56-
//clean cache
5756
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
5857
$configPool = new ConfigFilePool();
5958
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
6059
if ($this->write->isExist($this->write->getRelativePath($envPath))) {
61-
$this->saveCacheStatus($envPath);
60+
$this->saveCacheConfiguration($envPath);
61+
$this->disableAllCacheTypes($envPath);
6262
}
6363
//TODO: Till here
64+
6465
$cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
6566
$generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION));
6667
$diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI));
6768

69+
// Clean var/generation dir
6870
if ($this->write->isDirectory($generationPath)) {
6971
$this->write->delete($generationPath);
7072
}
73+
74+
// Clean var/di
7175
if ($this->write->isDirectory($diPath)) {
7276
$this->write->delete($diPath);
7377
}
78+
79+
// Clean var/cache
7480
if ($this->write->isDirectory($cachePath)) {
7581
$this->write->delete($cachePath);
7682
}
77-
//add to queue
78-
7983
$this->write->delete(self::REGENERATE_FLAG);
84+
$this->reEnableCacheTypes($envPath);
8085
}
8186
}
8287

8388
/**
84-
* Read Cache types from env.php and write to a json file.
89+
* Create flag for cleaning up var/generation, var/di and var/cache directories for subsequent
90+
* regeneration of this content
91+
*
92+
* @return void
93+
*/
94+
public function requestRegeneration()
95+
{
96+
$this->write->touch(self::REGENERATE_FLAG);
97+
}
98+
99+
/**
100+
* Read Cache configuration from env.php and write to a json file.
85101
*
86102
* @param string $envPath
87103
* @return void
88104
*/
89-
private function saveCacheStatus($envPath)
105+
private function saveCacheConfiguration($envPath)
90106
{
91-
$cacheData = include $envPath;
107+
$envData = include $envPath;
92108

93-
if (isset($cacheData['cache_types'])) {
94-
$enabledCacheTypes = $cacheData['cache_types'];
95-
$enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
109+
if (isset($envData['cache_types'])) {
110+
$enabledCacheTypes = array_filter($envData['cache_types'], function ($value) {
96111
return $value;
97112
});
98113
if (!empty($enabledCacheTypes)) {
@@ -101,32 +116,69 @@ private function saveCacheStatus($envPath)
101116
$this->write->getRelativePath($varDir) . '/.cachestates.json',
102117
json_encode($enabledCacheTypes)
103118
);
104-
$cacheTypes = array_keys($cacheData['cache_types']);
119+
}
120+
}
121+
}
105122

106-
foreach ($cacheTypes as $cacheType) {
107-
$cacheData['cache_types'][$cacheType] = 0;
108-
}
123+
/**
124+
* Disable all cache types by updating env.php.
125+
*
126+
* @param string $envPath
127+
* @return void
128+
*/
129+
private function disableAllCacheTypes($envPath)
130+
{
131+
$envData = include $envPath;
109132

110-
$formatter = new PhpFormatter();
111-
$contents = $formatter->format($cacheData);
133+
if (isset($envData['cache_types'])) {
134+
$cacheTypes = array_keys($envData['cache_types']);
112135

113-
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
114-
if (function_exists('opcache_invalidate')) {
115-
opcache_invalidate(
116-
$this->write->getAbsolutePath($envPath)
117-
);
118-
}
136+
foreach ($cacheTypes as $cacheType) {
137+
$envData['cache_types'][$cacheType] = 0;
138+
}
139+
140+
$formatter = new PhpFormatter();
141+
$contents = $formatter->format($envData);
142+
143+
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
144+
if (function_exists('opcache_invalidate')) {
145+
opcache_invalidate(
146+
$this->write->getAbsolutePath($envPath)
147+
);
119148
}
120149
}
121150
}
122151

123152
/**
124-
* Create flag for regeneration of code and di
153+
* Enables appropriate cache types based on var/.cachestates file settings
154+
* TODO: to be removed in scope of MAGETWO-53476
125155
*
126156
* @return void
127157
*/
128-
public function requestRegeneration()
158+
private function reEnableCacheTypes($envPath)
129159
{
130-
$this->write->touch(self::REGENERATE_FLAG);
160+
$pathToCacheStatus = $this->write->getRelativePath(
161+
$this->directoryList->getPath(DirectoryList::VAR_DIR) . '/.cachestates.json'
162+
);
163+
164+
if ($this->write->isExist($pathToCacheStatus)) {
165+
$enabledCacheTypes = array_keys(json_decode($this->write->readFile($pathToCacheStatus), true));
166+
167+
$envData = include $envPath;
168+
foreach ($enabledCacheTypes as $cacheType) {
169+
$envData['cache_types'][$cacheType] = 1;
170+
}
171+
172+
$formatter = new PhpFormatter();
173+
$contents = $formatter->format($envData);
174+
175+
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
176+
if (function_exists('opcache_invalidate')) {
177+
opcache_invalidate(
178+
$this->write->getAbsolutePath($envPath)
179+
);
180+
}
181+
$this->write->delete($pathToCacheStatus);
182+
}
131183
}
132184
}

lib/internal/Magento/Framework/Code/Test/Unit/GeneratedFilesTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ protected function setUp()
4545
* @param array $getPathMap
4646
* @param array $isDirectoryMap
4747
* @param array $deleteMap
48-
* @dataProvider regenerateDataProvider
48+
* @dataProvider cleanGeneratedFilesDataProvider
4949
*/
50-
public function testRegenerate($getPathMap, $isDirectoryMap, $deleteMap)
50+
public function testCleanGeneratedFiles($getPathMap, $isDirectoryMap, $deleteMap)
5151
{
5252

5353
$this->writeInterface
@@ -62,20 +62,21 @@ public function testRegenerate($getPathMap, $isDirectoryMap, $deleteMap)
6262
$this->writeInterface->expects($this->any())->method('getRelativePath')->willReturnMap($getPathMap);
6363
$this->writeInterface->expects($this->any())->method('isDirectory')->willReturnMap($isDirectoryMap);
6464
$this->writeInterface->expects($this->exactly(1))->method('delete')->willReturnMap($deleteMap);
65-
$this->model->regenerate();
65+
$this->model->cleanGeneratedFiles();
6666
}
6767

6868
/**
6969
* @return array
7070
*/
71-
public function regenerateDataProvider()
71+
public function cleanGeneratedFilesDataProvider()
7272
{
7373
$pathToGeneration = 'path/to/generation';
7474
$pathToDi = 'path/to/di';
7575
$pathToCache = 'path/to/di';
7676
$pathToConfig = 'path/to/config';
7777

78-
$getPathMap = [[DirectoryList::GENERATION, $pathToGeneration],
78+
$getPathMap = [
79+
[DirectoryList::GENERATION, $pathToGeneration],
7980
[DirectoryList::DI, $pathToDi],
8081
[DirectoryList::CACHE, $pathToCache],
8182
[DirectoryList::CONFIG, $pathToConfig],
@@ -98,7 +99,7 @@ public function regenerateDataProvider()
9899
];
99100
}
100101

101-
public function testRegenerateWithNoFlag()
102+
public function testCleanGeneratedFilesWithNoFlag()
102103
{
103104
$this->writeInterface
104105
->expects($this->once())
@@ -108,7 +109,7 @@ public function testRegenerateWithNoFlag()
108109
$this->directoryList->expects($this->never())->method('getPath');
109110
$this->writeInterface->expects($this->never())->method('getPath');
110111
$this->writeInterface->expects($this->never())->method('delete');
111-
$this->model->regenerate();
112+
$this->model->cleanGeneratedFiles();
112113
}
113114

114115
public function testRequestRegeneration()

setup/src/Magento/Setup/Console/Command/UpgradeCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
112112
private function enableCaches($objectManager, $output)
113113
{
114114
$writeFactory = $objectManager->get('Magento\Framework\Filesystem\Directory\WriteFactory');
115+
/** @var \Magento\Framework\Filesystem\Directory\Write $write */
115116
$write = $writeFactory->create(BP);
116117
/** @var \Magento\Framework\App\Filesystem\DirectoryList $dirList */
117118
$dirList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList');

0 commit comments

Comments
 (0)