Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/sync snippets after upload #543

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions Controller/Adminhtml/FastlyCdn/Vcl/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Config\Model\ResourceModel\Config as CoreConfig;
use Magento\Framework\App\Cache\TypeListInterface;
use Fastly\Cdn\Model\Snippet\BuiltInSnippetList;

/**
* Class Upload
Expand Down Expand Up @@ -90,6 +91,11 @@ class Upload extends Action
*/
private $typeList;

/**
* @var BuiltInSnippetList
*/
private $builtInSnippetList;

/**
* Upload constructor.
*
Expand All @@ -105,6 +111,7 @@ class Upload extends Action
* @param Filesystem $filesystem
* @param CoreConfig $coreConfig
* @param TypeListInterface $typeList
* @param BuiltInSnippetList $builtInSnippetList
*/
public function __construct(
Context $context,
Expand All @@ -118,7 +125,8 @@ public function __construct(
TimezoneInterface $timezone,
Filesystem $filesystem,
CoreConfig $coreConfig,
TypeListInterface $typeList
TypeListInterface $typeList,
BuiltInSnippetList $builtInSnippetList
) {
$this->request = $request;
$this->resultJson = $resultJsonFactory;
Expand All @@ -132,7 +140,7 @@ public function __construct(
parent::__construct($context);
$this->coreConfig = $coreConfig;
$this->typeList = $typeList;

$this->builtInSnippetList = $builtInSnippetList;
}

/**
Expand All @@ -155,6 +163,7 @@ public function execute()
$customSnippetPath = $read->getAbsolutePath(Config::CUSTOM_SNIPPET_PATH);
$customSnippets = $this->config->getCustomSnippets($customSnippetPath);

$allowedSnippets = [];
foreach ($snippets as $key => $value) {
$priority = 50;
if ($key == 'hash') {
Expand All @@ -167,6 +176,7 @@ public function execute()
'priority' => $priority,
'content' => $value
];
$allowedSnippets[] = $snippetData['name'];
$this->api->uploadSnippet($clone->number, $snippetData);
}

Expand All @@ -183,9 +193,11 @@ public function execute()
'content' => $value,
'dynamic' => '0'
];
$allowedSnippets[] = $customSnippetData['name'];
$this->api->uploadSnippet($clone->number, $customSnippetData);
}

$this->syncSnippets($allowedSnippets, $clone->number);
$this->createGzipHeader($clone);

$condition = [
Expand Down Expand Up @@ -334,4 +346,28 @@ private function createGzipHeader($clone)

$this->api->createHeader($clone->number, $headerData);
}

/**
* Remove disabled snippets from current vcl file
*
* @param array $allowedSnippets
* @param int $version
* @throws LocalizedException
*/
private function syncSnippets(array $allowedSnippets, int $version): void
{
$snippets = $this->api->getSnippets($version);

$currentActiveSnippets = [];
foreach ($snippets as $item) {
$currentActiveSnippets[] = $item->name;
}
$snippetsForDelete = \array_diff($currentActiveSnippets, $allowedSnippets);

foreach ($snippetsForDelete as $snippetName) {
if (!$this->builtInSnippetList->checkIsBuiltInSnippet($snippetName)) {
$this->api->removeSnippet($version, $snippetName);
}
}
}
}
Loading