From c7e7b1151264ee7d04808d405788039ac8825637 Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Thu, 9 Nov 2023 18:05:03 +0100 Subject: [PATCH] add cache on maintenance.yaml --- README.md | 20 +++++++++++++++- src/Command/DisableMaintenanceCommand.php | 4 ++++ src/Command/EnableMaintenanceCommand.php | 4 ++++ .../MaintenanceConfigurationController.php | 5 ++++ .../SynoliaSyliusMaintenanceExtension.php | 20 ++++++++++++++++ .../MaintenanceEventsubscriber.php | 24 ++++++++++++++++++- src/FileManager/ConfigurationFileManager.php | 2 ++ src/Resources/config/services.yaml | 3 +++ 8 files changed, 80 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6392a6f..6e48bcc 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ * Custom your message in your Sylius Back-office * Allow access to search bots to avoid negative impact on SEO +![Alt text](images/maintenance.png "maintenance_configure") + ## Requirements | | Version | @@ -102,7 +104,23 @@ For example : SYNOLIA_MAINTENANCE_DIR=var/maintenance ``` -![Alt text](images/maintenance.png "maintenance_configure") +### If you want to add cache on the **maintenance.yaml**: + +``` yaml +# .env +SYNOLIA_MAINTENANCE_CACHE=30 # ttl in seconds +``` +And in project code (for exemple with redis) +``` yaml +# config/packages/prod/cache.yaml +framework: + cache: + ... + pools: + ... + synolia_maintenance.cache: + adapter: cache.adapter.redis +``` ## Development diff --git a/src/Command/DisableMaintenanceCommand.php b/src/Command/DisableMaintenanceCommand.php index b56f200..7483acc 100644 --- a/src/Command/DisableMaintenanceCommand.php +++ b/src/Command/DisableMaintenanceCommand.php @@ -7,6 +7,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Synolia\SyliusMaintenancePlugin\FileManager\ConfigurationFileManager; @@ -17,6 +18,7 @@ final class DisableMaintenanceCommand extends Command public function __construct( private ConfigurationFileManager $configurationFileManager, private TranslatorInterface $translator, + private CacheInterface $synoliaMaintenanceCache, ) { parent::__construct(); } @@ -32,6 +34,8 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $this->configurationFileManager->deleteMaintenanceFile(); + $this->synoliaMaintenanceCache->delete(ConfigurationFileManager::MAINTENANCE_CACHE_KEY); + $output->writeln($this->translator->trans('maintenance.ui.message_disabled')); return 0; diff --git a/src/Command/EnableMaintenanceCommand.php b/src/Command/EnableMaintenanceCommand.php index d047d46..55464a6 100644 --- a/src/Command/EnableMaintenanceCommand.php +++ b/src/Command/EnableMaintenanceCommand.php @@ -8,9 +8,11 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Synolia\SyliusMaintenancePlugin\Exporter\MaintenanceConfigurationExporter; use Synolia\SyliusMaintenancePlugin\Factory\MaintenanceConfigurationFactory; +use Synolia\SyliusMaintenancePlugin\FileManager\ConfigurationFileManager; final class EnableMaintenanceCommand extends Command { @@ -20,6 +22,7 @@ public function __construct( private TranslatorInterface $translator, private MaintenanceConfigurationExporter $maintenanceExporter, private MaintenanceConfigurationFactory $configurationFactory, + private CacheInterface $synoliaMaintenanceCache, ) { parent::__construct(); } @@ -43,6 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $maintenanceConfiguration->setIpAddresses(implode(',', $ipsAddress)); } $this->maintenanceExporter->export($maintenanceConfiguration); + $this->synoliaMaintenanceCache->delete(ConfigurationFileManager::MAINTENANCE_CACHE_KEY); $output->writeln($this->translator->trans('maintenance.ui.message_enabled')); return 0; diff --git a/src/Controller/MaintenanceConfigurationController.php b/src/Controller/MaintenanceConfigurationController.php index 8ee61f0..0cd412c 100644 --- a/src/Controller/MaintenanceConfigurationController.php +++ b/src/Controller/MaintenanceConfigurationController.php @@ -7,9 +7,11 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Synolia\SyliusMaintenancePlugin\Exporter\MaintenanceConfigurationExporter; use Synolia\SyliusMaintenancePlugin\Factory\MaintenanceConfigurationFactory; +use Synolia\SyliusMaintenancePlugin\FileManager\ConfigurationFileManager; use Synolia\SyliusMaintenancePlugin\Form\Type\MaintenanceConfigurationType; final class MaintenanceConfigurationController extends AbstractController @@ -18,6 +20,7 @@ public function __construct( private TranslatorInterface $translator, private MaintenanceConfigurationExporter $maintenanceExporter, private MaintenanceConfigurationFactory $configurationFactory, + private CacheInterface $synoliaMaintenanceCache, ) { } @@ -40,6 +43,8 @@ public function __invoke(Request $request): Response $message = 'maintenance.ui.message_enabled'; } + $this->synoliaMaintenanceCache->delete(ConfigurationFileManager::MAINTENANCE_CACHE_KEY); + $request->getSession()->getFlashBag()->add('success', $this->translator->trans($message)); } diff --git a/src/DependencyInjection/SynoliaSyliusMaintenanceExtension.php b/src/DependencyInjection/SynoliaSyliusMaintenanceExtension.php index c47ada0..57077e9 100644 --- a/src/DependencyInjection/SynoliaSyliusMaintenanceExtension.php +++ b/src/DependencyInjection/SynoliaSyliusMaintenanceExtension.php @@ -18,8 +18,28 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('services.yaml'); } + public function prepend(ContainerBuilder $container): void + { + $this->addCachePool($container); + } + public function getAlias(): string { return 'synolia_sylius_maintenance'; } + + private function addCachePool(ContainerBuilder $container): void + { + $container->prependExtensionConfig('framework', [ + 'cache' => [ + 'pools' => [ + 'synolia_maintenance.cache' => [ + 'adapter' => 'cache.adapter.array', + 'public' => false, + 'tags' => false, + ], + ], + ], + ]); + } } diff --git a/src/EventSubscriber/MaintenanceEventsubscriber.php b/src/EventSubscriber/MaintenanceEventsubscriber.php index 61c7d5a..2bff3aa 100644 --- a/src/EventSubscriber/MaintenanceEventsubscriber.php +++ b/src/EventSubscriber/MaintenanceEventsubscriber.php @@ -7,7 +7,11 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; use Synolia\SyliusMaintenancePlugin\Factory\MaintenanceConfigurationFactory; +use Synolia\SyliusMaintenancePlugin\FileManager\ConfigurationFileManager; +use Synolia\SyliusMaintenancePlugin\Model\MaintenanceConfiguration; use Synolia\SyliusMaintenancePlugin\Voter\IsMaintenanceVoterInterface; use Twig\Environment; @@ -17,6 +21,8 @@ public function __construct( private Environment $twig, private MaintenanceConfigurationFactory $configurationFactory, private IsMaintenanceVoterInterface $isMaintenanceVoter, + private CacheInterface $synoliaMaintenanceCache, + private int $maintenanceCache, ) { } @@ -29,7 +35,7 @@ public static function getSubscribedEvents(): array public function handle(RequestEvent $event): void { - $configuration = $this->configurationFactory->get(); + $configuration = $this->getMaintenanceConfiguration(); /** @phpstan-ignore-next-line */ /** Call to function method_exists() with RequestEvent and 'isMainRequest' will always evaluate to true. */ if (method_exists($event, 'isMainRequest') && !$event->isMainRequest()) { @@ -51,4 +57,20 @@ public function handle(RequestEvent $event): void $event->setResponse(new Response($responseContent, Response::HTTP_SERVICE_UNAVAILABLE)); } + + private function getMaintenanceConfiguration(): MaintenanceConfiguration + { + if (0 !== $this->maintenanceCache) { + /** @var MaintenanceConfiguration $configuration */ + $configuration = $this->synoliaMaintenanceCache->get(ConfigurationFileManager::MAINTENANCE_CACHE_KEY, function (ItemInterface $item): MaintenanceConfiguration { + $item->expiresAfter($this->maintenanceCache); + + return $this->configurationFactory->get(); + }); + + return $configuration; + } + + return $this->configurationFactory->get(); + } } diff --git a/src/FileManager/ConfigurationFileManager.php b/src/FileManager/ConfigurationFileManager.php index 42d9798..15a9b90 100644 --- a/src/FileManager/ConfigurationFileManager.php +++ b/src/FileManager/ConfigurationFileManager.php @@ -12,6 +12,8 @@ final class ConfigurationFileManager { + public const MAINTENANCE_CACHE_KEY = 'synolia_maintenance_configuration'; + private const MAINTENANCE_FILE = 'maintenance.yaml'; private string $maintenanceDirectory; diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 3e982b4..982830d 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -1,6 +1,8 @@ parameters: env(SYNOLIA_MAINTENANCE_DIR): '' synolia_maintenance_dir: '%env(resolve:SYNOLIA_MAINTENANCE_DIR)%' + env(SYNOLIA_MAINTENANCE_CACHE): 0 + synolia_maintenance_cache: '%env(resolve:int:SYNOLIA_MAINTENANCE_CACHE)%' services: _defaults: @@ -9,6 +11,7 @@ services: public: false bind: $maintenanceDirectory: '%synolia_maintenance_dir%' + $maintenanceCache: '%synolia_maintenance_cache%' _instanceof: Synolia\SyliusMaintenancePlugin\Checker\IsMaintenanceCheckerInterface: