5
5
*/
6
6
namespace Magento \Framework \Code ;
7
7
8
- use Magento \Framework \Config \Data \ConfigData ;
9
8
use Magento \Framework \App \Filesystem \DirectoryList ;
10
9
use Magento \Framework \Config \File \ConfigFilePool ;
11
10
use Magento \Framework \Filesystem \Directory \WriteFactory ;
@@ -45,54 +44,70 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
45
44
}
46
45
47
46
/**
48
- * Clean generated code and DI configuration
47
+ * Clean var/generation, var/di and var/cache
49
48
*
50
49
* @return void
51
50
*/
52
- public function regenerate ()
51
+ public function cleanGeneratedFiles ()
53
52
{
54
53
if ($ this ->write ->isExist (self ::REGENERATE_FLAG )) {
54
+
55
55
//TODO: to be removed in scope of MAGETWO-53476
56
- //clean cache
57
56
$ deploymentConfig = $ this ->directoryList ->getPath (DirectoryList::CONFIG );
58
57
$ configPool = new ConfigFilePool ();
59
58
$ envPath = $ deploymentConfig . '/ ' . $ configPool ->getPath (ConfigFilePool::APP_ENV );
60
59
if ($ this ->write ->isExist ($ this ->write ->getRelativePath ($ envPath ))) {
61
- $ this ->saveCacheStatus ($ envPath );
60
+ $ this ->saveCacheConfiguration ($ envPath );
61
+ $ this ->disableAllCacheTypes ($ envPath );
62
62
}
63
63
//TODO: Till here
64
+
64
65
$ cachePath = $ this ->write ->getRelativePath ($ this ->directoryList ->getPath (DirectoryList::CACHE ));
65
66
$ generationPath = $ this ->write ->getRelativePath ($ this ->directoryList ->getPath (DirectoryList::GENERATION ));
66
67
$ diPath = $ this ->write ->getRelativePath ($ this ->directoryList ->getPath (DirectoryList::DI ));
67
68
69
+ // Clean var/generation dir
68
70
if ($ this ->write ->isDirectory ($ generationPath )) {
69
71
$ this ->write ->delete ($ generationPath );
70
72
}
73
+
74
+ // Clean var/di
71
75
if ($ this ->write ->isDirectory ($ diPath )) {
72
76
$ this ->write ->delete ($ diPath );
73
77
}
78
+
79
+ // Clean var/cache
74
80
if ($ this ->write ->isDirectory ($ cachePath )) {
75
81
$ this ->write ->delete ($ cachePath );
76
82
}
77
- //add to queue
78
-
79
83
$ this ->write ->delete (self ::REGENERATE_FLAG );
84
+ $ this ->reEnableCacheTypes ($ envPath );
80
85
}
81
86
}
82
87
83
88
/**
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.
85
101
*
86
102
* @param string $envPath
87
103
* @return void
88
104
*/
89
- private function saveCacheStatus ($ envPath )
105
+ private function saveCacheConfiguration ($ envPath )
90
106
{
91
- $ cacheData = include $ envPath ;
107
+ $ envData = include $ envPath ;
92
108
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 ) {
96
111
return $ value ;
97
112
});
98
113
if (!empty ($ enabledCacheTypes )) {
@@ -101,32 +116,69 @@ private function saveCacheStatus($envPath)
101
116
$ this ->write ->getRelativePath ($ varDir ) . '/.cachestates.json ' ,
102
117
json_encode ($ enabledCacheTypes )
103
118
);
104
- $ cacheTypes = array_keys ($ cacheData ['cache_types ' ]);
119
+ }
120
+ }
121
+ }
105
122
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 ;
109
132
110
- $ formatter = new PhpFormatter ();
111
- $ contents = $ formatter -> format ( $ cacheData );
133
+ if ( isset ( $ envData [ ' cache_types ' ])) {
134
+ $ cacheTypes = array_keys ( $ envData [ ' cache_types ' ] );
112
135
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
+ );
119
148
}
120
149
}
121
150
}
122
151
123
152
/**
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
125
155
*
126
156
* @return void
127
157
*/
128
- public function requestRegeneration ( )
158
+ private function reEnableCacheTypes ( $ envPath )
129
159
{
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
+ }
131
183
}
132
184
}
0 commit comments