Skip to content

Commit

Permalink
Merge pull request #29 from mcspronko/custom-logger
Browse files Browse the repository at this point in the history
Removed magento/module-admin-notification dependency
  • Loading branch information
mcspronko authored May 11, 2023
2 parents 815c5ea + 2cad5ac commit 81fc5a1
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 145 deletions.
43 changes: 28 additions & 15 deletions Cron/FlushInvalidatedCacheTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
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;

/**
* Class FlushInvalidatedCacheTypes flushes invalidated cache types by cronjob
*/
class FlushInvalidatedCacheTypes
{
/**
* @var TypeListInterface
*/
private TypeListInterface $cacheTypeList;

/**
* @var ScopeConfigInterface
*/
Expand All @@ -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;
}
Expand All @@ -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))
);
}
}
Expand Down
124 changes: 124 additions & 0 deletions Notification/CacheInvalidated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* Copyright © Pronko Consulting (https://www.pronkoconsulting.com)
* See LICENSE for the license details.
*/
declare(strict_types=1);

namespace Pronko\SelectiveCache\Notification;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Notification\MessageInterface;
use Pronko\SelectiveCache\Service\UrlProvider;

/**
* Cache Types message with the link to refresh invalidated cache types
*/
class CacheInvalidated implements MessageInterface
{
/**
* @var AuthorizationInterface
*/
private AuthorizationInterface $authorization;

/**
* @var TypeListInterface
*/
private TypeListInterface $cacheTypeList;

/**
* @var UrlProvider
*/
private UrlProvider $url;

/**
* @var Escaper
*/
private Escaper $escaper;

/**
* @param AuthorizationInterface $authorization
* @param TypeListInterface $cacheTypeList
* @param UrlProvider $url
* @param Escaper $escaper
*/
public function __construct(
AuthorizationInterface $authorization,
TypeListInterface $cacheTypeList,
UrlProvider $url,
Escaper $escaper
) {
$this->authorization = $authorization;
$this->cacheTypeList = $cacheTypeList;
$this->url = $url;
$this->escaper = $escaper;
}

/**
* Returns a link to flush invalidated cache
*
* @return string
*/
public function getText(): string
{
$link = sprintf(
'<a href="%s">%s</a>',
$this->url->getFlushInvalidatedUrl(),
$this->escaper->escapeHtml(__('Flush Invalidated Cache'))
);

return $this->escaper->escapeHtml(__('Invalidated cache type(s)')) .
': ' .
implode(', ', $this->getInvalidatedCacheTypes()) .
'. ' .
$link;
}

/**
* Retrieve unique message identity
*
* @return string
*/
public function getIdentity(): string
{
return implode('|', $this->getInvalidatedCacheTypes());
}

/**
* 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->getInvalidatedCacheTypes();
}

/**
* Retrieve message severity
*
* @return int
*/
public function getSeverity(): int
{
return MessageInterface::SEVERITY_CRITICAL;
}

/**
* Retrieve invalidated cache types
*
* @return array
*/
private function getInvalidatedCacheTypes(): array
{
$output = [];
foreach ($this->cacheTypeList->getInvalidated() as $type) {
$output[] = $type->getCacheType();
}
return $output;
}
}
80 changes: 0 additions & 80 deletions Plugin/CacheOutdatedMessagePlugin.php

This file was deleted.

2 changes: 0 additions & 2 deletions Plugin/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
/**
* Class CachePlugin for creating Button in Backend
*/

class CachePlugin
{
/**
Expand Down Expand Up @@ -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')) {
Expand Down
29 changes: 9 additions & 20 deletions Service/CacheButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,36 @@

use Magento\Backend\Block\Cache;
use Magento\Framework\Escaper;
use Magento\Framework\UrlInterface;

/**
* Class CacheButton creates a button to refresh invalidated cache types
*/
class CacheButton
{
/**
* @var UrlInterface
* @var UrlProvider
*/
private UrlInterface $url;
private UrlProvider $url;

/**
* @var Escaper
*/
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;
$this->escaper = $escaper;
}

/**
* Method execute
* Adds the "Refresh Invalidated Cache" button to the Cache block class
*
* @param Cache $cache
* @return void
Expand All @@ -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')
);
}
}
Loading

0 comments on commit 81fc5a1

Please sign in to comment.