Skip to content

Commit

Permalink
add cache on maintenance.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed Nov 10, 2023
1 parent 5dbbbcb commit c7e7b11
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 2 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Command/DisableMaintenanceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +18,7 @@ final class DisableMaintenanceCommand extends Command
public function __construct(
private ConfigurationFileManager $configurationFileManager,
private TranslatorInterface $translator,
private CacheInterface $synoliaMaintenanceCache,
) {
parent::__construct();
}
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Command/EnableMaintenanceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -20,6 +22,7 @@ public function __construct(
private TranslatorInterface $translator,
private MaintenanceConfigurationExporter $maintenanceExporter,
private MaintenanceConfigurationFactory $configurationFactory,
private CacheInterface $synoliaMaintenanceCache,
) {
parent::__construct();
}
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Controller/MaintenanceConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +20,7 @@ public function __construct(
private TranslatorInterface $translator,
private MaintenanceConfigurationExporter $maintenanceExporter,
private MaintenanceConfigurationFactory $configurationFactory,
private CacheInterface $synoliaMaintenanceCache,
) {
}

Expand All @@ -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));
}

Expand Down
20 changes: 20 additions & 0 deletions src/DependencyInjection/SynoliaSyliusMaintenanceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
],
]);
}
}
24 changes: 23 additions & 1 deletion src/EventSubscriber/MaintenanceEventsubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +21,8 @@ public function __construct(
private Environment $twig,
private MaintenanceConfigurationFactory $configurationFactory,
private IsMaintenanceVoterInterface $isMaintenanceVoter,
private CacheInterface $synoliaMaintenanceCache,
private int $maintenanceCache,
) {
}

Expand All @@ -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()) {
Expand All @@ -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();
}
}
2 changes: 2 additions & 0 deletions src/FileManager/ConfigurationFileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

final class ConfigurationFileManager
{
public const MAINTENANCE_CACHE_KEY = 'synolia_maintenance_configuration';

private const MAINTENANCE_FILE = 'maintenance.yaml';

private string $maintenanceDirectory;
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -9,6 +11,7 @@ services:
public: false
bind:
$maintenanceDirectory: '%synolia_maintenance_dir%'
$maintenanceCache: '%synolia_maintenance_cache%'

_instanceof:
Synolia\SyliusMaintenancePlugin\Checker\IsMaintenanceCheckerInterface:
Expand Down

0 comments on commit c7e7b11

Please sign in to comment.