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 ;
@@ -48,67 +47,127 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
48
47
* Clean generated code and DI configuration
49
48
*
50
49
* @return void
50
+ *
51
+ * @deprecated
52
+ * @see \Magento\Framework\Code\GeneratedFiles::cleanGeneratedFiles
51
53
*/
52
54
public function regenerate ()
55
+ {
56
+ $ this ->cleanGeneratedFiles ();
57
+ }
58
+
59
+ /**
60
+ * Clean var/generation, var/di and var/cache
61
+ *
62
+ * @return void
63
+ */
64
+ public function cleanGeneratedFiles ()
53
65
{
54
66
if ($ this ->write ->isExist (self ::REGENERATE_FLAG )) {
67
+
68
+ $ enabledCacheTypes = [];
69
+
55
70
//TODO: to be removed in scope of MAGETWO-53476
56
- //clean cache
57
71
$ deploymentConfig = $ this ->directoryList ->getPath (DirectoryList::CONFIG );
58
72
$ configPool = new ConfigFilePool ();
59
73
$ envPath = $ deploymentConfig . '/ ' . $ configPool ->getPath (ConfigFilePool::APP_ENV );
60
74
if ($ this ->write ->isExist ($ this ->write ->getRelativePath ($ envPath ))) {
61
- $ this ->saveCacheStatus ($ envPath );
75
+ $ enabledCacheTypes = $ this ->getEnabledCacheTypes ();
76
+ $ this ->disableAllCacheTypes ();
62
77
}
63
78
//TODO: Till here
79
+
64
80
$ cachePath = $ this ->write ->getRelativePath ($ this ->directoryList ->getPath (DirectoryList::CACHE ));
65
81
$ generationPath = $ this ->write ->getRelativePath ($ this ->directoryList ->getPath (DirectoryList::GENERATION ));
66
82
$ diPath = $ this ->write ->getRelativePath ($ this ->directoryList ->getPath (DirectoryList::DI ));
67
83
84
+ // Clean var/generation dir
68
85
if ($ this ->write ->isDirectory ($ generationPath )) {
69
86
$ this ->write ->delete ($ generationPath );
70
87
}
88
+
89
+ // Clean var/di
71
90
if ($ this ->write ->isDirectory ($ diPath )) {
72
91
$ this ->write ->delete ($ diPath );
73
92
}
93
+
94
+ // Clean var/cache
74
95
if ($ this ->write ->isDirectory ($ cachePath )) {
75
96
$ this ->write ->delete ($ cachePath );
76
97
}
77
- //add to queue
78
-
79
98
$ this ->write ->delete (self ::REGENERATE_FLAG );
99
+ $ this ->enableCacheTypes ($ enabledCacheTypes );
80
100
}
81
101
}
82
102
83
103
/**
84
- * Read Cache types from env.php and write to a json file.
104
+ * Create flag for cleaning up var/generation, var/di and var/cache directories for subsequent
105
+ * regeneration of this content
85
106
*
86
- * @param string $envPath
87
107
* @return void
88
108
*/
89
- private function saveCacheStatus ($ envPath )
109
+ public function requestRegeneration ()
110
+ {
111
+ $ this ->write ->touch (self ::REGENERATE_FLAG );
112
+ }
113
+
114
+ /**
115
+ * Reads Cache configuration from env.php and returns indexed array containing all the enabled cache types.
116
+ *
117
+ * @return string[]
118
+ */
119
+ private function getEnabledCacheTypes ()
90
120
{
91
- $ cacheData = include $ envPath ;
92
-
93
- if (isset ($ cacheData ['cache_types ' ])) {
94
- $ enabledCacheTypes = $ cacheData ['cache_types ' ];
95
- $ enabledCacheTypes = array_filter ($ enabledCacheTypes , function ($ value ) {
96
- return $ value ;
97
- });
98
- if (!empty ($ enabledCacheTypes )) {
99
- $ varDir = $ this ->directoryList ->getPath (DirectoryList::VAR_DIR );
100
- $ this ->write ->writeFile (
101
- $ this ->write ->getRelativePath ($ varDir ) . '/.cachestates.json ' ,
102
- json_encode ($ enabledCacheTypes )
103
- );
104
- $ cacheTypes = array_keys ($ cacheData ['cache_types ' ]);
121
+ $ enabledCacheTypes = [];
122
+ $ envPath = $ this ->getEnvPath ();
123
+ if ($ this ->write ->isReadable ($ this ->write ->getRelativePath ($ envPath ))) {
124
+ $ envData = include $ envPath ;
125
+ if (isset ($ envData ['cache_types ' ])) {
126
+ $ cacheStatus = $ envData ['cache_types ' ];
127
+ $ enabledCacheTypes = array_filter ($ cacheStatus , function ($ value ) {
128
+ return $ value ;
129
+ });
130
+ $ enabledCacheTypes = array_keys ($ enabledCacheTypes );
131
+ }
132
+ }
133
+ return $ enabledCacheTypes ;
134
+ }
135
+
136
+
137
+ /**
138
+ * Returns path to env.php file
139
+ *
140
+ * @return string
141
+ * @throws \Exception
142
+ */
143
+ private function getEnvPath ()
144
+ {
145
+ $ deploymentConfig = $ this ->directoryList ->getPath (DirectoryList::CONFIG );
146
+ $ configPool = new ConfigFilePool ();
147
+ $ envPath = $ deploymentConfig . '/ ' . $ configPool ->getPath (ConfigFilePool::APP_ENV );
148
+ return $ envPath ;
149
+ }
150
+
151
+ /**
152
+ * Disables all cache types by updating env.php.
153
+ *
154
+ * @return void
155
+ */
156
+ private function disableAllCacheTypes ()
157
+ {
158
+ $ envPath = $ this ->getEnvPath ();
159
+ if ($ this ->write ->isWritable ($ this ->write ->getRelativePath ($ envPath ))) {
160
+ $ envData = include $ envPath ;
161
+
162
+ if (isset ($ envData ['cache_types ' ])) {
163
+ $ cacheTypes = array_keys ($ envData ['cache_types ' ]);
105
164
106
165
foreach ($ cacheTypes as $ cacheType ) {
107
- $ cacheData ['cache_types ' ][$ cacheType ] = 0 ;
166
+ $ envData ['cache_types ' ][$ cacheType ] = 0 ;
108
167
}
109
168
110
169
$ formatter = new PhpFormatter ();
111
- $ contents = $ formatter ->format ($ cacheData );
170
+ $ contents = $ formatter ->format ($ envData );
112
171
113
172
$ this ->write ->writeFile ($ this ->write ->getRelativePath ($ envPath ), $ contents );
114
173
if (function_exists ('opcache_invalidate ' )) {
@@ -121,12 +180,34 @@ private function saveCacheStatus($envPath)
121
180
}
122
181
123
182
/**
124
- * Create flag for regeneration of code and di
183
+ * Enables apppropriate cache types in app/etc/env.php based on the passed in $cacheTypes array
184
+ * TODO: to be removed in scope of MAGETWO-53476
185
+ *
186
+ * @param string[]
125
187
*
126
188
* @return void
127
189
*/
128
- public function requestRegeneration ( )
190
+ private function enableCacheTypes ( $ cacheTypes )
129
191
{
130
- $ this ->write ->touch (self ::REGENERATE_FLAG );
192
+ if (empty ($ cacheTypes )) {
193
+ return ;
194
+ }
195
+ $ envPath = $ this ->getEnvPath ();
196
+ if ($ this ->write ->isReadable ($ this ->write ->getRelativePath ($ envPath ))) {
197
+ $ envData = include $ envPath ;
198
+ foreach ($ cacheTypes as $ cacheType ) {
199
+ if (isset ($ envData ['cache_types ' ][$ cacheType ])) {
200
+ $ envData ['cache_types ' ][$ cacheType ] = 1 ;
201
+ }
202
+ }
203
+
204
+ $ formatter = new PhpFormatter ();
205
+ $ contents = $ formatter ->format ($ envData );
206
+
207
+ $ this ->write ->writeFile ($ this ->write ->getRelativePath ($ envPath ), $ contents );
208
+ if (function_exists ('opcache_invalidate ' )) {
209
+ opcache_invalidate ($ this ->write ->getAbsolutePath ($ envPath ));
210
+ }
211
+ }
131
212
}
132
213
}
0 commit comments