Skip to content

Commit 9e4fa33

Browse files
committed
MAGETWO-54205: After Disable/Enable Magento modules via CLI all cache types become disabled
1 parent 3591ce8 commit 9e4fa33

File tree

1 file changed

+108
-25
lines changed

1 file changed

+108
-25
lines changed

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

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
4343
$this->write = $writeFactory->create(BP);
4444
}
4545

46+
/**
47+
* Clean generated code and DI configuration
48+
*
49+
* @return void
50+
*
51+
* @deprecated
52+
* @see \Magento\Framework\Code\GeneratedFiles::cleanGeneratedFiles
53+
*/
54+
public function regenerate()
55+
{
56+
if ($this->write->isExist(self::REGENERATE_FLAG)) {
57+
//TODO: to be removed in scope of MAGETWO-53476
58+
//clean cache
59+
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
60+
$configPool = new ConfigFilePool();
61+
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
62+
if ($this->write->isExist($this->write->getRelativePath($envPath))) {
63+
$this->saveCacheStatus($envPath);
64+
}
65+
//TODO: Till here
66+
$cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
67+
$generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION));
68+
$diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI));
69+
70+
if ($this->write->isDirectory($generationPath)) {
71+
$this->write->delete($generationPath);
72+
}
73+
if ($this->write->isDirectory($diPath)) {
74+
$this->write->delete($diPath);
75+
}
76+
if ($this->write->isDirectory($cachePath)) {
77+
$this->write->delete($cachePath);
78+
}
79+
//add to queue
80+
81+
$this->write->delete(self::REGENERATE_FLAG);
82+
}
83+
}
84+
85+
4686
/**
4787
* Clean var/generation, var/di and var/cache
4888
*
@@ -52,12 +92,14 @@ public function cleanGeneratedFiles()
5292
{
5393
if ($this->write->isExist(self::REGENERATE_FLAG)) {
5494

95+
$cacheStatus = [];
96+
5597
//TODO: to be removed in scope of MAGETWO-53476
5698
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
5799
$configPool = new ConfigFilePool();
58100
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
59101
if ($this->write->isExist($this->write->getRelativePath($envPath))) {
60-
$this->saveCacheConfiguration();
102+
$cacheStatus = $this->getCacheStatus();
61103
$this->disableAllCacheTypes();
62104
}
63105
//TODO: Till here
@@ -81,7 +123,7 @@ public function cleanGeneratedFiles()
81123
$this->write->delete($cachePath);
82124
}
83125
$this->write->delete(self::REGENERATE_FLAG);
84-
$this->reEnableCacheTypes();
126+
$this->restoreCacheStatus($cacheStatus);
85127
}
86128
}
87129

@@ -97,17 +139,19 @@ public function requestRegeneration()
97139
}
98140

99141
/**
100-
* Reads Cache configuration from env.php and write to a json file.
142+
* Read Cache types from env.php and write to a json file.
101143
*
144+
* @param string $envPath
102145
* @return void
146+
*
103147
*/
104-
public function saveCacheConfiguration()
148+
private function saveCacheStatus($envPath)
105149
{
106-
$envPath = $this->GetEnvPath();
107-
$envData = include $envPath;
150+
$cacheData = include $envPath;
108151

109-
if (isset($envData['cache_types'])) {
110-
$enabledCacheTypes = array_filter($envData['cache_types'], function ($value) {
152+
if (isset($cacheData['cache_types'])) {
153+
$enabledCacheTypes = $cacheData['cache_types'];
154+
$enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
111155
return $value;
112156
});
113157
if (!empty($enabledCacheTypes)) {
@@ -116,17 +160,56 @@ public function saveCacheConfiguration()
116160
$this->write->getRelativePath($varDir) . '/.cachestates.json',
117161
json_encode($enabledCacheTypes)
118162
);
163+
$cacheTypes = array_keys($cacheData['cache_types']);
164+
165+
foreach ($cacheTypes as $cacheType) {
166+
$cacheData['cache_types'][$cacheType] = 0;
167+
}
168+
169+
$formatter = new PhpFormatter();
170+
$contents = $formatter->format($cacheData);
171+
172+
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
173+
if (function_exists('opcache_invalidate')) {
174+
opcache_invalidate(
175+
$this->write->getAbsolutePath($envPath)
176+
);
177+
}
119178
}
120179
}
121180
}
122181

182+
/**
183+
* Reads Cache configuration from env.php and returns the 'cache_types' key data which is the current
184+
* cache status.
185+
*
186+
* @return array
187+
*/
188+
private function getCacheStatus()
189+
{
190+
$cacheStatus = [];
191+
if (empty($envPath)) {
192+
$envPath = $this->getEnvPath();
193+
}
194+
195+
if ($this->write->isExist($this->write->getRelativePath($envPath) &&
196+
$this->write->isReadable($this->write->getRelativePath($envPath)))) {
197+
$envData = include $envPath;
198+
if (isset($envData['cache_types'])) {
199+
$cacheStatus = $envData['cache_types'];
200+
}
201+
}
202+
return $cacheStatus;
203+
}
204+
205+
123206
/**
124207
* Returns path to env.php file
125208
*
126209
* @return string
127210
* @throws \Exception
128211
*/
129-
private function GetEnvPath()
212+
private function getEnvPath()
130213
{
131214
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
132215
$configPool = new ConfigFilePool();
@@ -141,7 +224,7 @@ private function GetEnvPath()
141224
*/
142225
private function disableAllCacheTypes()
143226
{
144-
$envPath = $this->GetEnvPath();
227+
$envPath = $this->getEnvPath();
145228
$envData = include $envPath;
146229

147230
if (isset($envData['cache_types'])) {
@@ -164,34 +247,34 @@ private function disableAllCacheTypes()
164247
}
165248

166249
/**
167-
* Enables appropriate cache types based on var/.cachestates file settings
250+
* restore the cacache setting in env.php
168251
* TODO: to be removed in scope of MAGETWO-53476
169252
*
253+
* @param array
254+
*
170255
* @return void
171256
*/
172-
private function reEnableCacheTypes()
257+
private function restoreCacheStatus($cacheStatus)
173258
{
174-
$pathToCacheStatus = $this->write->getRelativePath(
175-
$this->directoryList->getPath(DirectoryList::VAR_DIR) . '/.cachestates.json'
176-
);
177-
178-
if ($this->write->isExist($pathToCacheStatus)) {
179-
$enabledCacheTypes = array_keys(json_decode($this->write->readFile($pathToCacheStatus), true));
180-
181-
$envPath = $this->GetEnvPath();
259+
if (empty($cacheStatus)) {
260+
return;
261+
}
262+
$envPath = $this->getEnvPath();
263+
if ($this->write->isExist($this->write->getRelativePath($envPath) &&
264+
$this->write->isReadable($this->write->getRelativePath($envPath)))) {
182265
$envData = include $envPath;
183-
foreach ($enabledCacheTypes as $cacheType) {
184-
$envData['cache_types'][$cacheType] = 1;
266+
foreach ($cacheStatus as $cacheType => $state) {
267+
if (isset($envData['cache_types'][$cacheType])) {
268+
$envData['cache_types'][$cacheType] = $state;
269+
}
185270
}
186271

187272
$formatter = new PhpFormatter();
188273
$contents = $formatter->format($envData);
189274

190275
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
191276
if (function_exists('opcache_invalidate')) {
192-
opcache_invalidate(
193-
$this->write->getAbsolutePath($envPath)
194-
);
277+
opcache_invalidate($this->write->getAbsolutePath($envPath));
195278
}
196279
}
197280
}

0 commit comments

Comments
 (0)