@@ -43,6 +43,46 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
43
43
$ this ->write = $ writeFactory ->create (BP );
44
44
}
45
45
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
+
46
86
/**
47
87
* Clean var/generation, var/di and var/cache
48
88
*
@@ -52,12 +92,14 @@ public function cleanGeneratedFiles()
52
92
{
53
93
if ($ this ->write ->isExist (self ::REGENERATE_FLAG )) {
54
94
95
+ $ cacheStatus = [];
96
+
55
97
//TODO: to be removed in scope of MAGETWO-53476
56
98
$ deploymentConfig = $ this ->directoryList ->getPath (DirectoryList::CONFIG );
57
99
$ configPool = new ConfigFilePool ();
58
100
$ envPath = $ deploymentConfig . '/ ' . $ configPool ->getPath (ConfigFilePool::APP_ENV );
59
101
if ($ this ->write ->isExist ($ this ->write ->getRelativePath ($ envPath ))) {
60
- $ this ->saveCacheConfiguration ();
102
+ $ cacheStatus = $ this ->getCacheStatus ();
61
103
$ this ->disableAllCacheTypes ();
62
104
}
63
105
//TODO: Till here
@@ -81,7 +123,7 @@ public function cleanGeneratedFiles()
81
123
$ this ->write ->delete ($ cachePath );
82
124
}
83
125
$ this ->write ->delete (self ::REGENERATE_FLAG );
84
- $ this ->reEnableCacheTypes ( );
126
+ $ this ->restoreCacheStatus ( $ cacheStatus );
85
127
}
86
128
}
87
129
@@ -97,17 +139,19 @@ public function requestRegeneration()
97
139
}
98
140
99
141
/**
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.
101
143
*
144
+ * @param string $envPath
102
145
* @return void
146
+ *
103
147
*/
104
- public function saveCacheConfiguration ( )
148
+ private function saveCacheStatus ( $ envPath )
105
149
{
106
- $ envPath = $ this ->GetEnvPath ();
107
- $ envData = include $ envPath ;
150
+ $ cacheData = include $ envPath ;
108
151
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 ) {
111
155
return $ value ;
112
156
});
113
157
if (!empty ($ enabledCacheTypes )) {
@@ -116,17 +160,56 @@ public function saveCacheConfiguration()
116
160
$ this ->write ->getRelativePath ($ varDir ) . '/.cachestates.json ' ,
117
161
json_encode ($ enabledCacheTypes )
118
162
);
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
+ }
119
178
}
120
179
}
121
180
}
122
181
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
+
123
206
/**
124
207
* Returns path to env.php file
125
208
*
126
209
* @return string
127
210
* @throws \Exception
128
211
*/
129
- private function GetEnvPath ()
212
+ private function getEnvPath ()
130
213
{
131
214
$ deploymentConfig = $ this ->directoryList ->getPath (DirectoryList::CONFIG );
132
215
$ configPool = new ConfigFilePool ();
@@ -141,7 +224,7 @@ private function GetEnvPath()
141
224
*/
142
225
private function disableAllCacheTypes ()
143
226
{
144
- $ envPath = $ this ->GetEnvPath ();
227
+ $ envPath = $ this ->getEnvPath ();
145
228
$ envData = include $ envPath ;
146
229
147
230
if (isset ($ envData ['cache_types ' ])) {
@@ -164,34 +247,34 @@ private function disableAllCacheTypes()
164
247
}
165
248
166
249
/**
167
- * Enables appropriate cache types based on var/.cachestates file settings
250
+ * restore the cacache setting in env.php
168
251
* TODO: to be removed in scope of MAGETWO-53476
169
252
*
253
+ * @param array
254
+ *
170
255
* @return void
171
256
*/
172
- private function reEnableCacheTypes ( )
257
+ private function restoreCacheStatus ( $ cacheStatus )
173
258
{
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 )))) {
182
265
$ 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
+ }
185
270
}
186
271
187
272
$ formatter = new PhpFormatter ();
188
273
$ contents = $ formatter ->format ($ envData );
189
274
190
275
$ this ->write ->writeFile ($ this ->write ->getRelativePath ($ envPath ), $ contents );
191
276
if (function_exists ('opcache_invalidate ' )) {
192
- opcache_invalidate (
193
- $ this ->write ->getAbsolutePath ($ envPath )
194
- );
277
+ opcache_invalidate ($ this ->write ->getAbsolutePath ($ envPath ));
195
278
}
196
279
}
197
280
}
0 commit comments