38
38
use Magento \Framework \App \Cache \TypeListInterface ;
39
39
40
40
/**
41
- * Class Upload
41
+ * Class for VCL Upload
42
42
*
43
- * @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl
44
43
*/
45
44
class Upload extends Action
46
45
{
@@ -132,7 +131,6 @@ public function __construct(
132
131
parent ::__construct ($ context );
133
132
$ this ->coreConfig = $ coreConfig ;
134
133
$ this ->typeList = $ typeList ;
135
-
136
134
}
137
135
138
136
/**
@@ -155,6 +153,7 @@ public function execute()
155
153
$ customSnippetPath = $ read ->getAbsolutePath (Config::CUSTOM_SNIPPET_PATH );
156
154
$ customSnippets = $ this ->config ->getCustomSnippets ($ customSnippetPath );
157
155
156
+ $ allowedSnippets = [];
158
157
foreach ($ snippets as $ key => $ value ) {
159
158
$ priority = 50 ;
160
159
if ($ key == 'hash ' ) {
@@ -167,6 +166,7 @@ public function execute()
167
166
'priority ' => $ priority ,
168
167
'content ' => $ value
169
168
];
169
+ $ allowedSnippets [] = $ snippetData ['name ' ];
170
170
$ this ->api ->uploadSnippet ($ clone ->number , $ snippetData );
171
171
}
172
172
@@ -183,9 +183,12 @@ public function execute()
183
183
'content ' => $ value ,
184
184
'dynamic ' => '0 '
185
185
];
186
+ $ allowedSnippets [] = $ customSnippetData ['name ' ];
186
187
$ this ->api ->uploadSnippet ($ clone ->number , $ customSnippetData );
187
188
}
188
189
190
+ $ this ->syncSnippets ($ allowedSnippets , $ clone ->number );
191
+
189
192
$ this ->createGzipHeader ($ clone );
190
193
191
194
$ condition = [
@@ -273,6 +276,8 @@ private function validateCustomSnippet($customSnippet)
273
276
}
274
277
275
278
/**
279
+ * Setup Dictionary
280
+ *
276
281
* @param $cloneNumber
277
282
* @param $currActiveVersion
278
283
* @return bool|mixed
@@ -291,6 +296,8 @@ private function setupDictionary($cloneNumber, $currActiveVersion)
291
296
}
292
297
293
298
/**
299
+ * Setup Acl
300
+ *
294
301
* @param $cloneNumber
295
302
* @param $currActiveVersion
296
303
* @return bool|mixed
@@ -309,10 +316,12 @@ private function setupAcl($cloneNumber, $currActiveVersion)
309
316
}
310
317
311
318
/**
319
+ * Create Gzip Header
320
+ *
312
321
* @param $clone
313
322
* @throws LocalizedException
314
323
*/
315
- private function createGzipHeader ($ clone )
324
+ private function createGzipHeader ($ clone ): void
316
325
{
317
326
$ condition = [
318
327
'name ' => Config::FASTLY_MAGENTO_MODULE . '_gzip_safety ' ,
@@ -334,4 +343,29 @@ private function createGzipHeader($clone)
334
343
335
344
$ this ->api ->createHeader ($ clone ->number , $ headerData );
336
345
}
346
+
347
+ /**
348
+ * Remove disabled snippets from current vcl file
349
+ *
350
+ * @param array $allowedSnippets
351
+ * @param int $version
352
+ * @throws LocalizedException
353
+ */
354
+ private function syncSnippets (array $ allowedSnippets , int $ version ): void
355
+ {
356
+ $ snippets = $ this ->api ->getSnippets ($ version );
357
+
358
+ $ currentActiveSnippets = [];
359
+ foreach ($ snippets as $ item ) {
360
+ $ currentActiveSnippets [] = $ item ->name ;
361
+ }
362
+ $ snippetsForDelete = array_diff ($ currentActiveSnippets , $ allowedSnippets );
363
+
364
+ foreach ($ snippetsForDelete as $ snippetName ) {
365
+ //remove only snippet name which starts with magento prefix
366
+ if (strpos ($ snippetName , Config::FASTLY_MAGENTO_MODULE . '_ ' ) === 0 ) {
367
+ $ this ->api ->removeSnippet ($ version , $ snippetName );
368
+ }
369
+ }
370
+ }
337
371
}
0 commit comments