Skip to content

Commit 7be9b2b

Browse files
committed
Pushing v1.0.0
0 parents  commit 7be9b2b

32 files changed

+1228
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
.idea
3+
*.debug

Block/System/Cache/Additional.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Webscale. All rights reserved.
4+
* See LICENSE for license details.
5+
*/
6+
7+
namespace Webscale\Varnish\Block\System\Cache;
8+
9+
use Magento\Backend\Block\Template;
10+
use Magento\PageCache\Model\Config as CacheConfig;
11+
use Webscale\Varnish\Helper\Config;
12+
13+
class Additional extends Template
14+
{
15+
/** @var Config $config */
16+
private $config;
17+
18+
/** @var CacheConfig $cacheConfig */
19+
private $cacheConfig;
20+
21+
/**
22+
* @param Template\Context $context
23+
* @param Config $config
24+
* @param CacheConfig $cacheConfig
25+
*/
26+
public function __construct(
27+
Template\Context $context,
28+
Config $config,
29+
CacheConfig $cacheConfig
30+
) {
31+
$this->config = $config;
32+
$this->cacheConfig = $cacheConfig;
33+
34+
parent::__construct($context);
35+
}
36+
37+
/**
38+
* Retrieve purge cache URL
39+
*
40+
* @return string
41+
*/
42+
public function getPurgeAllUrl(): string
43+
{
44+
return $this->getUrl('webscalevarnish/purge/all', ['_current' => true]);
45+
}
46+
47+
/**
48+
* Check if module is enabled
49+
*
50+
* @return bool
51+
*/
52+
public function isEnabled(): bool
53+
{
54+
return ($this->cacheConfig->getType() == CacheConfig::VARNISH && $this->config->isAvailable());
55+
}
56+
}

Block/System/Config/Settings.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Webscale. All rights reserved.
4+
* See LICENSE for license details.
5+
*/
6+
7+
namespace Webscale\Varnish\Block\System\Config;
8+
9+
use Webscale\Varnish\Helper\Config;
10+
use Magento\Backend\Block\Context;
11+
use Magento\Backend\Model\Auth\Session;
12+
use Magento\Config\Block\System\Config\Form\Fieldset;
13+
use Magento\Framework\View\Helper\Js;
14+
use Magento\Backend\Model\UrlInterface;
15+
use Magento\PageCache\Model\Config as CacheConfig;
16+
use Magento\Framework\Data\Form\Element\AbstractElement;
17+
18+
/**
19+
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
20+
*/
21+
class Settings extends Fieldset
22+
{
23+
/**
24+
* @var Config $config
25+
*/
26+
protected $config;
27+
28+
/**
29+
* @param Context $context
30+
* @param Session $authSession
31+
* @param Js $jsHelper
32+
* @param Config $config
33+
* @param UrlInterface $urlBuilder
34+
* @param CacheConfig $cacheConfig
35+
* @param array $data
36+
*/
37+
public function __construct(
38+
Context $context,
39+
Session $authSession,
40+
Js $jsHelper,
41+
Config $config,
42+
UrlInterface $urlBuilder,
43+
CacheConfig $cacheConfig,
44+
array $data = []
45+
) {
46+
$this->config = $config;
47+
$this->urlBuilder = $urlBuilder;
48+
$this->cacheConfig = $cacheConfig;
49+
50+
parent::__construct($context, $authSession, $jsHelper, $data);
51+
}
52+
53+
/**
54+
* Return header comment part of html for fieldset
55+
*
56+
* @param AbstractElement $element
57+
* @return string
58+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
59+
*/
60+
protected function _getHeaderCommentHtml($element): string
61+
{
62+
return $this->getCacheConfigMessage() . $this->getApplicationConfigMessage();
63+
}
64+
65+
/**
66+
* Check if cache config set to varnish
67+
*
68+
* @return string
69+
*/
70+
private function getCacheConfigMessage(): string
71+
{
72+
if ($this->cacheConfig->getType() != CacheConfig::VARNISH) {
73+
$url = $this->urlBuilder->getUrl('adminhtml/system_config/edit/section/system');
74+
return $this->getMessageWrapper(
75+
__('Magento is configured to use the built-in Full Page Cache.' .
76+
' To use Webscale varnish caching please change "Caching Application" to "Varnish Cache"' .
77+
' under the "Full Page Cache" tab in <a href="%1">System Configuration</a>', $url),
78+
'error'
79+
);
80+
}
81+
82+
return '';
83+
}
84+
85+
/**
86+
* Check if account and application is configured
87+
*
88+
* @return string
89+
*/
90+
private function getApplicationConfigMessage(): string
91+
{
92+
if (empty($this->config->getApiToken())) {
93+
return $this->getMessageWrapper(
94+
__('Please configure API Token and Application Id.'),
95+
'warning'
96+
);
97+
}
98+
99+
if (!$this->config->isAvailable()) {
100+
return $this->getMessageWrapper(
101+
__('To be able to use Webscale varnish cache please configure "Application Id".'),
102+
'warning'
103+
);
104+
}
105+
106+
return '';
107+
}
108+
109+
/**
110+
* Get message wrapper
111+
*
112+
* @param string $message
113+
* @param string $severity
114+
* @return string
115+
*/
116+
private function getMessageWrapper(string $message = '', string $severity = 'notice'): string
117+
{
118+
$html = '<div style="padding:10px;"><div class="messages">';
119+
$html .= '<div class="message message-' . $severity . ' ' . $severity . '" style="margin-bottom: 0;">';
120+
$html .= '<div data-ui-id="messages-message-' . $severity . '">';
121+
$html .= $message;
122+
$html .= '</div></div></div></div>';
123+
124+
return $html;
125+
}
126+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Webscale. All rights reserved.
4+
* See LICENSE for license details.
5+
*/
6+
7+
namespace Webscale\Varnish\Controller\Adminhtml;
8+
9+
use Magento\Backend\App\Action;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Framework\App\Config\Storage\WriterInterface;
12+
use Magento\Framework\Controller\Result\JsonFactory;
13+
use Webscale\Varnish\Helper\Config;
14+
use Webscale\Varnish\Service\Api;
15+
use Magento\PageCache\Model\Config as CacheConfig;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use Webscale\Varnish\Model\PurgeCache;
18+
use Webscale\Varnish\Model\CredentialsManager;
19+
20+
abstract class AbstractController extends Action
21+
{
22+
/** @var Api $api */
23+
protected $api;
24+
25+
/** @var JsonFactory $json */
26+
protected $json;
27+
28+
/** @var CacheConfig $cacheConfig */
29+
protected $cacheConfig;
30+
31+
/** @var Config $config */
32+
protected $config;
33+
34+
/** @var StoreManagerInterface $storeManager */
35+
protected $storeManager;
36+
37+
/** @var PurgeCache $purgeCache */
38+
protected $purgeCache;
39+
40+
/**
41+
* @param Context $context
42+
* @param Api $api
43+
* @param JsonFactory $json
44+
* @param Config $config
45+
* @param CacheConfig $cacheConfig
46+
* @param StoreManagerInterface $storeManager
47+
* @param PurgeCache $purgeCache
48+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
49+
*/
50+
public function __construct(
51+
Context $context,
52+
Api $api,
53+
JsonFactory $json,
54+
Config $config,
55+
CacheConfig $cacheConfig,
56+
StoreManagerInterface $storeManager,
57+
PurgeCache $purgeCache
58+
) {
59+
$this->api = $api;
60+
$this->json = $json;
61+
$this->config = $config;
62+
$this->cacheConfig = $cacheConfig;
63+
$this->storeManager = $storeManager;
64+
$this->purgeCache = $purgeCache;
65+
66+
parent::__construct($context);
67+
}
68+
}

Controller/Adminhtml/Purge/All.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Webscale. All rights reserved.
4+
* See LICENSE for license details.
5+
*/
6+
7+
namespace Webscale\Varnish\Controller\Adminhtml\Purge;
8+
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\PageCache\Model\Config as CacheConfig;
11+
use Webscale\Varnish\Controller\Adminhtml\AbstractController;
12+
use Magento\Framework\App\ResponseInterface;
13+
14+
class All extends AbstractController implements HttpPostActionInterface
15+
{
16+
/**
17+
* Retrieve accounts
18+
*
19+
* @return ResponseInterface
20+
*/
21+
public function execute(): ResponseInterface
22+
{
23+
try {
24+
if ($this->cacheConfig->getType() == CacheConfig::VARNISH && $this->config->isAvailable()) {
25+
if ($this->purgeCache->sendPurgeRequest(['tagsPattern' => ['.*']])) {
26+
$this->messageManager->addSuccessMessage(
27+
__('Varnish cache flushed successfully.')
28+
);
29+
} else {
30+
$this->messageManager->addErrorMessage(
31+
__('There is error occurred while trying to purge varnish cache.' .
32+
' Please refer to logs for more information.')
33+
);
34+
}
35+
}
36+
} catch (\Exception $e) {
37+
$this->messageManager->addErrorMessage(
38+
__('There is error occurred while trying to purge varnish cache.' .
39+
' Please refer to logs for more information.')
40+
);
41+
}
42+
43+
return $this->_redirect('adminhtml/cache/index', ['_current' => true]);
44+
}
45+
}
80 KB
Loading

Documentation/debug-logs.png

30.7 KB
Loading
86.5 KB
Loading

Documentation/flush-cache.png

89.6 KB
Loading

Documentation/sync-accounts.png

57.7 KB
Loading

0 commit comments

Comments
 (0)