Skip to content

Commit

Permalink
Merge pull request #36 from synolia/bugfix/disable-maintenance-file-w…
Browse files Browse the repository at this point in the history
…ithout-removing-it

fix: keep maintenance file alive when maintenance mode is disabled
  • Loading branch information
maxperei committed Dec 22, 2023
2 parents 21ceb43 + 59981d4 commit ac5681a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
php bin/console maintenance:disable
```

4. Remove configuration file using CLI

By default, **maintenance.yaml** configuration file remains when running `maintenance:disable` or via admin panel using toggle disable
Nevertheless passing option `[-c|--clear]` to command line above will reset previous saved configuration

### You can also turn your website under maintenance in Back Office :

- Enable/disable the plugin
Expand Down
21 changes: 18 additions & 3 deletions src/Command/DisableMaintenanceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
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 DisableMaintenanceCommand extends Command
Expand All @@ -19,22 +22,34 @@ public function __construct(
private ConfigurationFileManager $configurationFileManager,
private TranslatorInterface $translator,
private CacheInterface $synoliaMaintenanceCache,
private MaintenanceConfigurationFactory $configurationFactory,
private MaintenanceConfigurationExporter $configurationExporter,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Deactivate maintenance plugin')
->setHelp('This command allows you to delete the maintenance.yaml')
->setDescription('Disable maintenance plugin')
->addOption('clear', 'c', InputOption::VALUE_NONE, 'Reset maintenance mode')
->setHelp('This command allows you to disable or delete the maintenance.yaml')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->configurationFileManager->deleteMaintenanceFile();
$this->synoliaMaintenanceCache->delete(ConfigurationFileManager::MAINTENANCE_CACHE_KEY);
$maintenanceConfig = $this->configurationFactory->get();
$maintenanceConfig->setEnabled(false);
$this->configurationExporter->export($maintenanceConfig);

if (true === $input->getOption('clear')) {
$this->configurationFileManager->deleteMaintenanceFile();
$output->writeln($this->translator->trans('maintenance.ui.message_reset'));

return 0;
}

$output->writeln($this->translator->trans('maintenance.ui.message_disabled'));

Expand Down
5 changes: 1 addition & 4 deletions src/Exporter/MaintenanceConfigurationExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public function __construct(private ConfigurationFileManager $configurationFileM

public function export(MaintenanceConfiguration $configuration): void
{
$this->configurationFileManager->deleteMaintenanceFile();
if (!$configuration->isEnabled()) {
return;
}
$dataToExport = [];

$ipAddresses = $configuration->getArrayIpsAddresses();
Expand All @@ -40,6 +36,7 @@ public function export(MaintenanceConfiguration $configuration): void
if ($configuration->allowBots()) {
$dataToExport['allow_bots'] = true;
}
$dataToExport['enabled'] = $configuration->isEnabled();
$scheduler = $this->getSchedulerArray($configuration->getStartDate(), $configuration->getEndDate());

$this->configurationFileManager->createMaintenanceFile(array_merge(
Expand Down
2 changes: 2 additions & 0 deletions src/Factory/MaintenanceConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static function map(MaintenanceConfiguration $maintenanceConfiguration, ?
'custom_message' => '',
'token' => '',
'allow_bots' => false,
'enabled' => true,
]);
$options = $resolver->resolve($options);

Expand All @@ -67,6 +68,7 @@ public static function map(MaintenanceConfiguration $maintenanceConfiguration, ?
->setCustomMessage($options['custom_message'])
->setToken($options['token'])
->setAllowBots($options['allow_bots'])
->setEnabled($options['enabled'])
;
}
}
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ maintenance:
allow_bots: Grant access to search bots during maintenance
message_enabled: The maintenance plugin was successfully enabled.
message_disabled: The maintenance plugin was successfully disabled.
message_reset: The maintenance plugin was successfully reset.
message_success_ips: The ips were added to the file maintenance.yaml successfully.
message_error_ips: An error occurred while adding ips addresses. Add one or multiple ips addresses separated with a comma.
message_disabled_404: The plugin is disabled.
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ maintenance:
allow_bots: Accorder l'accès aux robots de recherche pendant la maintenance
message_enabled: Le plugin de maintenance a été activé avec succès.
message_disabled: Le plugin de maintenance a été désactivé avec succès.
message_reset: Le plugin de maintenance a été réinitialisé avec succès.
message_success_ips: Les adresses ip ont été ajoutés au fichier maintenance.yaml avec succès.
message_error_ips: Une erreur s'est produite lors de l'ajout d'adresses ip. Ajouter une ou plusieurs adresses ip séparées par une virgule.
message_disabled_404: Le plugin est désactivé.
Expand Down
16 changes: 15 additions & 1 deletion tests/PHPUnit/MaintenanceNotEnabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@

namespace Tests\Synolia\SyliusMaintenancePlugin\PHPUnit;

use Symfony\Component\Yaml\Yaml;

final class MaintenanceNotEnabledTest extends AbstractWebTestCase
{
public function testMaintenanceIsNotEnabled(): void
public function testMaintenanceIsNotEnabledWhenFileDoesNotExist(): void
{
self::$client->request('GET', '/en_US/');
$this->assertSiteIsUp();
}

public function testMaintenanceIsNotEnabledWhenFileIsNotEnabled(): void
{
\file_put_contents(
$this->file,
Yaml::dump([
'enabled' => false,
]),
);
self::$client->request('GET', '/en_US/');
$this->assertSiteIsUp();
}
Expand Down

0 comments on commit ac5681a

Please sign in to comment.