From 58342915d49b57ff4a2e9aeca40415e0a90ffc62 Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Wed, 10 May 2023 21:34:19 +0100 Subject: [PATCH 1/8] Added license header --- Plugin/CachePlugin.php | 2 -- etc/acl.xml | 7 ++++++- etc/cron_groups.xml | 6 ++++++ etc/crontab.xml | 7 ++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Plugin/CachePlugin.php b/Plugin/CachePlugin.php index e7e9da6..da09197 100644 --- a/Plugin/CachePlugin.php +++ b/Plugin/CachePlugin.php @@ -15,7 +15,6 @@ /** * Class CachePlugin for creating Button in Backend */ - class CachePlugin { /** @@ -48,7 +47,6 @@ public function __construct(CacheButton $cacheButton, AuthorizationInterface $au * * @return void */ - public function beforeSetLayout(Cache $subject, LayoutInterface $layout) { if ($this->authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache')) { diff --git a/etc/acl.xml b/etc/acl.xml index ca1967e..6879646 100644 --- a/etc/acl.xml +++ b/etc/acl.xml @@ -1,5 +1,10 @@ - + diff --git a/etc/cron_groups.xml b/etc/cron_groups.xml index da05065..4c56b38 100644 --- a/etc/cron_groups.xml +++ b/etc/cron_groups.xml @@ -1,4 +1,10 @@ + 1 diff --git a/etc/crontab.xml b/etc/crontab.xml index e21c92e..733f7d7 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -1,8 +1,13 @@ + - selectivecache/cron/cronjob_clear_invalidated_cache From 849aedc3d0baef6dfc1e981da35e6dd9d71a9c02 Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 08:43:29 +0100 Subject: [PATCH 2/8] Removed magento/module-admin-notification dependency --- Notification/CacheInvalidated.php | 129 ++++++++++++++++++++++++++ Plugin/CacheOutdatedMessagePlugin.php | 80 ---------------- composer.json | 1 - etc/adminhtml/di.xml | 16 ++++ etc/di.xml | 3 - 5 files changed, 145 insertions(+), 84 deletions(-) create mode 100644 Notification/CacheInvalidated.php delete mode 100644 Plugin/CacheOutdatedMessagePlugin.php create mode 100644 etc/adminhtml/di.xml diff --git a/Notification/CacheInvalidated.php b/Notification/CacheInvalidated.php new file mode 100644 index 0000000..921c56b --- /dev/null +++ b/Notification/CacheInvalidated.php @@ -0,0 +1,129 @@ +authorization = $authorization; + $this->cacheTypeList = $cacheTypeList; + $this->urlBuilder = $urlBuilder; + $this->escaper = $escaper; + } + + /** + * @return string + */ + public function getText(): string + { + $link = sprintf( + '%s', + $this->getFlushInvalidatedUrl(), + $this->escaper->escapeHtml(__('Flush Invalidated Cache')) + ); + + return $this->escaper->escapeHtml(__('Invalidated cache type(s)')) . + ': ' . + implode(', ', $this->getCacheTypesForRefresh()) . + '. ' . + $link; + } + + /** + * Retrieve unique message identity + * + * @return string + */ + public function getIdentity(): string + { + return implode('|', $this->getCacheTypesForRefresh()); + } + + /** + * Check whether + * + * @return bool + */ + public function isDisplayed(): bool + { + return $this->authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache') + && $this->getCacheTypesForRefresh(); + } + + /** + * Retrieve message severity + * + * @return int + */ + public function getSeverity(): int + { + return MessageInterface::SEVERITY_CRITICAL; + } + + /** + * @return array + */ + private function getCacheTypesForRefresh(): array + { + $output = []; + foreach ($this->cacheTypeList->getInvalidated() as $type) { + $output[] = $type->getCacheType(); + } + return $output; + } + + /** + * Method getFlushInvalidatedOnlyUrl gets Url for flushing only invalidated cache types + * + * @return string + */ + private function getFlushInvalidatedUrl(): string + { + return $this->urlBuilder->getUrl('pronko_selectivecache/cache/flushInvalidated'); + } +} diff --git a/Plugin/CacheOutdatedMessagePlugin.php b/Plugin/CacheOutdatedMessagePlugin.php deleted file mode 100644 index 01e1929..0000000 --- a/Plugin/CacheOutdatedMessagePlugin.php +++ /dev/null @@ -1,80 +0,0 @@ -urlBuilder = $urlBuilder; - $this->authorization = $authorization; - $this->escaper = $escaper; - } - - /** - * Method afterGetText - * - * @param CacheOutdated $subject - * @param string $result - * @return string - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterGetText(CacheOutdated $subject, string $result): string - { - if ($this->authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache')) { - $link = sprintf( - '%s', - $this->getFlushInvalidatedOnlyUrl(), - $this->escaper->escapeHtml(__('Flush Invalidated Cache')) - ); - - $result .= '
' . $this->escaper->escapeHtml(__('Recommended')) . ': ' . $link; - } - - return $result; - } - - /** - * Method getFlushInvalidatedOnlyUrl gets Url for flushing only invalidated cache types - * - * @return string - */ - private function getFlushInvalidatedOnlyUrl(): string - { - return $this->urlBuilder->getUrl('pronko_selectivecache/cache/flushInvalidated'); - } -} diff --git a/composer.json b/composer.json index e20e95f..c2e50dc 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,6 @@ "require": { "magento/framework": "~102|~103", "magento/module-backend": "~101|~102", - "magento/module-admin-notification": "~100", "symfony/console": "~4|~5", "psr/log": "*" }, diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml new file mode 100644 index 0000000..f7c4741 --- /dev/null +++ b/etc/adminhtml/di.xml @@ -0,0 +1,16 @@ + + + + + + + Pronko\SelectiveCache\Notification\CacheInvalidated + + + + diff --git a/etc/di.xml b/etc/di.xml index a9ac7a8..42c7eee 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -9,9 +9,6 @@ - - - From eb2f095498927371ac646d0fb01fe3f29a208929 Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 08:48:38 +0100 Subject: [PATCH 3/8] Moved button plugin configuration to adminhtml area --- etc/adminhtml/di.xml | 3 +++ etc/di.xml | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index f7c4741..aba4cee 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -6,6 +6,9 @@ */ --> + + + diff --git a/etc/di.xml b/etc/di.xml index 42c7eee..ccbe277 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -6,9 +6,6 @@ */ --> - - - From 0953fa12f8ecbcdb17003c7d7b2d1c31fb128205 Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 09:02:38 +0100 Subject: [PATCH 4/8] Moved urlBuilder to a separate UrlProvider class --- Notification/CacheInvalidated.php | 39 +++++++++++++--------------- Service/CacheButton.php | 29 +++++++-------------- Service/UrlProvider.php | 42 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 42 deletions(-) create mode 100644 Service/UrlProvider.php diff --git a/Notification/CacheInvalidated.php b/Notification/CacheInvalidated.php index 921c56b..c64ca6b 100644 --- a/Notification/CacheInvalidated.php +++ b/Notification/CacheInvalidated.php @@ -11,7 +11,7 @@ use Magento\Framework\AuthorizationInterface; use Magento\Framework\Escaper; use Magento\Framework\Notification\MessageInterface; -use Magento\Framework\UrlInterface; +use Pronko\SelectiveCache\Service\UrlProvider; /** * Cache Types message with the link to refresh invalidated cache types @@ -29,9 +29,9 @@ class CacheInvalidated implements MessageInterface private TypeListInterface $cacheTypeList; /** - * @var UrlInterface + * @var UrlProvider */ - private UrlInterface $urlBuilder; + private UrlProvider $url; /** * @var Escaper @@ -41,35 +41,37 @@ class CacheInvalidated implements MessageInterface /** * @param AuthorizationInterface $authorization * @param TypeListInterface $cacheTypeList - * @param UrlInterface $urlBuilder + * @param UrlProvider $url * @param Escaper $escaper */ public function __construct( AuthorizationInterface $authorization, TypeListInterface $cacheTypeList, - UrlInterface $urlBuilder, + UrlProvider $url, Escaper $escaper ) { $this->authorization = $authorization; $this->cacheTypeList = $cacheTypeList; - $this->urlBuilder = $urlBuilder; + $this->url = $url; $this->escaper = $escaper; } /** + * Returns a link to flush invalidated cache + * * @return string */ public function getText(): string { $link = sprintf( '%s', - $this->getFlushInvalidatedUrl(), + $this->url->getFlushInvalidatedUrl(), $this->escaper->escapeHtml(__('Flush Invalidated Cache')) ); return $this->escaper->escapeHtml(__('Invalidated cache type(s)')) . ': ' . - implode(', ', $this->getCacheTypesForRefresh()) . + implode(', ', $this->getInvalidatedCacheTypes()) . '. ' . $link; } @@ -81,18 +83,19 @@ public function getText(): string */ public function getIdentity(): string { - return implode('|', $this->getCacheTypesForRefresh()); + return implode('|', $this->getInvalidatedCacheTypes()); } /** - * Check whether + * Check whether there are invalidated cache types in the system. + * It also checks for permissions to show the message. * * @return bool */ public function isDisplayed(): bool { return $this->authorization->isAllowed('Pronko_SelectiveCache::flush_invalidated_cache') - && $this->getCacheTypesForRefresh(); + && $this->getInvalidatedCacheTypes(); } /** @@ -106,9 +109,11 @@ public function getSeverity(): int } /** + * Retrieve invalidated cache types + * * @return array */ - private function getCacheTypesForRefresh(): array + private function getInvalidatedCacheTypes(): array { $output = []; foreach ($this->cacheTypeList->getInvalidated() as $type) { @@ -116,14 +121,4 @@ private function getCacheTypesForRefresh(): array } return $output; } - - /** - * Method getFlushInvalidatedOnlyUrl gets Url for flushing only invalidated cache types - * - * @return string - */ - private function getFlushInvalidatedUrl(): string - { - return $this->urlBuilder->getUrl('pronko_selectivecache/cache/flushInvalidated'); - } } diff --git a/Service/CacheButton.php b/Service/CacheButton.php index a1997fe..fee6265 100644 --- a/Service/CacheButton.php +++ b/Service/CacheButton.php @@ -9,7 +9,6 @@ use Magento\Backend\Block\Cache; use Magento\Framework\Escaper; -use Magento\Framework\UrlInterface; /** * Class CacheButton creates a button to refresh invalidated cache types @@ -17,9 +16,9 @@ class CacheButton { /** - * @var UrlInterface + * @var UrlProvider */ - private UrlInterface $url; + private UrlProvider $url; /** * @var Escaper @@ -27,12 +26,11 @@ class CacheButton private Escaper $escaper; /** - * CacheButton constructor. - * @param UrlInterface $url + * @param UrlProvider $url * @param Escaper $escaper */ public function __construct( - UrlInterface $url, + UrlProvider $url, Escaper $escaper ) { $this->url = $url; @@ -40,7 +38,7 @@ public function __construct( } /** - * Method execute + * Adds the "Refresh Invalidated Cache" button to the Cache block class * * @param Cache $cache * @return void @@ -51,21 +49,12 @@ public function execute(Cache $cache): void 'refresh_invalidated_cache', [ 'label' => $this->escaper->escapeHtml(__('Refresh Invalidated Cache')), - 'onclick' => sprintf("setLocation('%s')", $this->getFlushInvalidatedOnlyUrl()), + 'onclick' => sprintf( + "setLocation('%s')", + $this->escaper->escapeUrl($this->url->getFlushInvalidatedUrl()) + ), 'class' => 'primary flush-cache-magento' ] ); } - - /** - * Method getFlushInvalidatedOnlyUrl - * - * @return string - */ - private function getFlushInvalidatedOnlyUrl(): string - { - return $this->escaper->escapeUrl( - $this->url->getUrl('pronko_selectivecache/*/flushInvalidated') - ); - } } diff --git a/Service/UrlProvider.php b/Service/UrlProvider.php new file mode 100644 index 0000000..690c8d7 --- /dev/null +++ b/Service/UrlProvider.php @@ -0,0 +1,42 @@ +url = $url; + } + + /** + * Provides the url for flushing invalidated cache types + * + * @return string + */ + public function getFlushInvalidatedUrl(): string + { + return $this->url->getUrl(self::FLUSH_CACHE_URI); + } +} From c694520b9660b8a0cc1250648221a3615d3704c8 Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 09:06:11 +0100 Subject: [PATCH 5/8] Fixed unit test --- Test/Unit/Service/CacheButtonTest.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Test/Unit/Service/CacheButtonTest.php b/Test/Unit/Service/CacheButtonTest.php index 0634652..b04bcb8 100644 --- a/Test/Unit/Service/CacheButtonTest.php +++ b/Test/Unit/Service/CacheButtonTest.php @@ -7,12 +7,12 @@ namespace Pronko\SelectiveCache\Test\Unit\Service; -use Magento\Backend\Block\Cache; use Magento\Framework\Escaper; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\MockObject; +use Magento\Backend\Block\Cache; use Pronko\SelectiveCache\Service\CacheButton; -use Magento\Framework\UrlInterface; +use Pronko\SelectiveCache\Service\UrlProvider; /** * Class CacheButtonTest creates TestCase for button Creation @@ -48,7 +48,7 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); - $this->url = $this->getMockBuilder(UrlInterface::class) + $this->url = $this->getMockBuilder(UrlProvider::class) ->disableOriginalConstructor() ->getMock(); @@ -67,8 +67,7 @@ public function testExecute(): void $url = 'https://www.example.com/pronko_selectivecache/index/flushInvalidated'; $label = 'Refresh Invalidated Cache'; $this->url->expects($this->any()) - ->method('getUrl') - ->with('pronko_selectivecache/*/flushInvalidated') + ->method('getFlushInvalidatedUrl') ->willReturn($url); $this->escaper->method('escapeUrl') From 99edfe11d5a66fdd76c9922e0f4782c856dec1b0 Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 09:17:37 +0100 Subject: [PATCH 6/8] Fixed incorrect url link --- Service/UrlProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Service/UrlProvider.php b/Service/UrlProvider.php index 690c8d7..e26b999 100644 --- a/Service/UrlProvider.php +++ b/Service/UrlProvider.php @@ -7,7 +7,7 @@ namespace Pronko\SelectiveCache\Service; -use Magento\Framework\UrlInterface; +use Magento\Backend\Model\UrlInterface; class UrlProvider { @@ -15,7 +15,7 @@ class UrlProvider * Flush Invalidated Cache Types URI * @var string */ - private const FLUSH_CACHE_URI = 'pronko_selectivecache/*/flushInvalidated'; + private const FLUSH_CACHE_URI = 'pronko_selectivecache/cache/flushInvalidated'; /** * @var UrlInterface From dcd4f9c7917924066f5d8d83d45f235489a999ae Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 09:54:54 +0100 Subject: [PATCH 7/8] Fixed cron command when configured with a custom cron group --- etc/cron_groups.xml | 18 ------------------ etc/crontab.xml | 2 +- 2 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 etc/cron_groups.xml diff --git a/etc/cron_groups.xml b/etc/cron_groups.xml deleted file mode 100644 index 4c56b38..0000000 --- a/etc/cron_groups.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - 1 - 4 - 2 - 10 - 60 - 600 - 1 - - diff --git a/etc/crontab.xml b/etc/crontab.xml index 733f7d7..e519055 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -6,7 +6,7 @@ */ --> - + selectivecache/cron/cronjob_clear_invalidated_cache From 2cad5ac44c3f459631afd37b240ffd0c41050abd Mon Sep 17 00:00:00 2001 From: Max Pronko Date: Thu, 11 May 2023 09:55:25 +0100 Subject: [PATCH 8/8] Updated cron class to follow event/observer logic as per others --- Cron/FlushInvalidatedCacheTypes.php | 43 +++++++++++++++++++---------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/Cron/FlushInvalidatedCacheTypes.php b/Cron/FlushInvalidatedCacheTypes.php index 472e3dc..2b159e8 100644 --- a/Cron/FlushInvalidatedCacheTypes.php +++ b/Cron/FlushInvalidatedCacheTypes.php @@ -8,7 +8,8 @@ namespace Pronko\SelectiveCache\Cron; use Psr\Log\LoggerInterface; -use Magento\Framework\App\Cache\TypeListInterface; +use Magento\Framework\DataObject\Factory; +use Magento\Framework\Event\Manager as EventManager; use Magento\Framework\App\Config\ScopeConfigInterface; /** @@ -16,11 +17,6 @@ */ class FlushInvalidatedCacheTypes { - /** - * @var TypeListInterface - */ - private TypeListInterface $cacheTypeList; - /** * @var ScopeConfigInterface */ @@ -32,16 +28,29 @@ class FlushInvalidatedCacheTypes private LoggerInterface $logger; /** - * @param TypeListInterface $cacheTypeList + * @var EventManager + */ + private EventManager $eventManager; + + /** + * @var Factory + */ + private Factory $dataObjectFactory; + + /** + * @param EventManager $eventManager + * @param Factory $dataObjectFactory * @param ScopeConfigInterface $scopeConfig * @param LoggerInterface $logger */ public function __construct( - TypeListInterface $cacheTypeList, + EventManager $eventManager, + Factory $dataObjectFactory, ScopeConfigInterface $scopeConfig, LoggerInterface $logger ) { - $this->cacheTypeList = $cacheTypeList; + $this->eventManager = $eventManager; + $this->dataObjectFactory = $dataObjectFactory; $this->scopeConfig = $scopeConfig; $this->logger = $logger; } @@ -57,15 +66,19 @@ public function execute(): void return; } - foreach ($this->cacheTypeList->getInvalidated() as $invalidatedType) { - $this->cacheTypeList->cleanType($invalidatedType->getData('id')); - $cacheLabels[] = $invalidatedType->getData('cache_type'); - } + $cacheContainer = $this->dataObjectFactory->create(); + + $this->eventManager->dispatch( + 'cache_flush_invalidated', + ['cache_container' => $cacheContainer] + ); + + $labels = $cacheContainer->getData('labels'); //TODO add configuration setting to enable/disable logging - if (!empty($cacheLabels)) { + if (!empty($labels)) { $this->logger->info( - sprintf("Cache types cleared automatically: %s", implode(', ', $cacheLabels)) + sprintf("Cache types cleared automatically: %s", implode(', ', $labels)) ); } }