Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Feature: schedule lock #82

Closed
wants to merge 12 commits into from
127 changes: 127 additions & 0 deletions Command/AbstractLockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Command;

use Lexik\Bundle\MaintenanceBundle\Drivers\AbstractDriver;
use Lexik\Bundle\MaintenanceBundle\Drivers\DriverTtlInterface;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

/**
* Create a lock action
*
* @package LexikMaintenanceBundle
* @author Wolfram Eberius <[email protected]>
*/
abstract class AbstractLockCommand extends ContainerAwareCommand
{
protected $ttl;

/**
* @param InputInterface $input
* @return integer|null
*/
abstract protected function getTtlFromInput(InputInterface $input);

protected function interactWithTtl(InputInterface $input, OutputInterface $output)
{
$ttl = null;

$driver = $this->getDriver();
$default = $driver->getOptions();

if ($driver instanceof DriverTtlInterface) {
if (null === $this->getTtlFromInput($input)) {
$output->writeln(array(
'',
'Do you want to redefine maintenance life time ?',
'If yes enter the number of seconds. Press enter to continue',
'',
));

$ttl = $this->askAndValidate(
$input,
$output,
sprintf('<info>%s</info> [<comment>Default value in your configuration: %s</comment>]%s ', 'Set time', $driver->hasTtl() ? $driver->getTtl() : 'unlimited', ':'),
function($value) use ($default) {
if (!is_numeric($value) && null === $default) {
return null;
} elseif (!is_numeric($value)) {
throw new \InvalidArgumentException('Time must be an integer');
}
return $value;
},
1,
isset($default['ttl']) ? $default['ttl'] : 0
);
}

$ttl = (int) $ttl;
$this->ttl = $ttl ? $ttl : $this->getTtlFromInput($input);
} else {
$output->writeln(array(
'',
sprintf('<fg=red>Ttl doesn\'t work with %s driver</>', get_class($driver)),
'',
));
}

}

/**
* Get driver
*
* @return AbstractDriver
*/
protected function getDriver()
{
return $this->getContainer()->get('lexik_maintenance.driver.factory')->getDriver();
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param $question
* @param InputInterface $input
* @param OutputInterface $output
* @return mixed
*/
protected function askConfirmation($question, InputInterface $input, OutputInterface $output) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askConfirmation($output, '<question>' . $question . '</question>', 'y');
}

return $this->getHelper('question')
->ask($input, $output, new \Symfony\Component\Console\Question\ConfirmationQuestion($question));
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param $question
* @param $validator
* @param int $attempts
* @param null $default
* @return mixed
*/
protected function askAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = 1, $default = null) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askAndValidate($output, $question, $validator, $attempts, $default);
}

$question = new \Symfony\Component\Console\Question\Question($question, $default);
$question->setValidator($validator);
$question->setMaxAttempts($attempts);

return $this->getHelper('question')
->ask($input, $output, $question);
}
}
111 changes: 7 additions & 104 deletions Command/DriverLockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

namespace Lexik\Bundle\MaintenanceBundle\Command;

use Lexik\Bundle\MaintenanceBundle\Drivers\AbstractDriver;
use Lexik\Bundle\MaintenanceBundle\Drivers\DriverTtlInterface;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

/**
* Create a lock action
*
* @package LexikMaintenanceBundle
* @author Gilles Gauthier <[email protected]>
*/
class DriverLockCommand extends ContainerAwareCommand
class DriverLockCommand extends AbstractLockCommand
{
protected $ttl;
public function getTtlFromInput(InputInterface $input)
{
return $input->getArgument('ttl');
}

/**
* {@inheritdoc}
Expand All @@ -28,19 +29,13 @@ protected function configure()
$this
->setName('lexik:maintenance:lock')
->setDescription('Lock access to the site while maintenance...')
->addArgument('ttl', InputArgument::OPTIONAL, 'Overwrite time to life from your configuration, doesn\'t work with file or shm driver. Time in seconds.', null)
->addArgument('ttl', InputArgument::OPTIONAL, 'Overwrite time to live from your configuration, doesn\'t work with file or shm driver. Time in seconds.', null)
->setHelp(<<<EOT

You can optionally set a time to life of the maintenance

<info>%command.full_name% 3600</info>

You can execute the lock without a warning message which you need to interact with:

<info>%command.full_name% --no-interaction</info>

Or

<info>%command.full_name% 3600 -n</info>
EOT
);
Expand Down Expand Up @@ -77,9 +72,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$driver = $this->getDriver();
$default = $driver->getOptions();

$formatter = $this->getHelperSet()->get('formatter');

if (null !== $input->getArgument('ttl') && !is_numeric($input->getArgument('ttl'))) {
Expand All @@ -92,96 +84,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
'',
));

$ttl = null;
if ($driver instanceof DriverTtlInterface) {
if (null === $input->getArgument('ttl')) {
$output->writeln(array(
'',
'Do you want to redefine maintenance life time ?',
'If yes enter the number of seconds. Press enter to continue',
'',
));

$ttl = $this->askAndValidate(
$input,
$output,
sprintf('<info>%s</info> [<comment>Default value in your configuration: %s</comment>]%s ', 'Set time', $driver->hasTtl() ? $driver->getTtl() : 'unlimited', ':'),
function($value) use ($default) {
if (!is_numeric($value) && null === $default) {
return null;
} elseif (!is_numeric($value)) {
throw new \InvalidArgumentException('Time must be an integer');
}
return $value;
},
1,
isset($default['ttl']) ? $default['ttl'] : 0
);
}

$ttl = (int) $ttl;
$this->ttl = $ttl ? $ttl : $input->getArgument('ttl');
} else {
$output->writeln(array(
'',
sprintf('<fg=red>Ttl doesn\'t work with %s driver</>', get_class($driver)),
'',
));
}
}

/**
* Get driver
*
* @return AbstractDriver
*/
private function getDriver()
{
return $this->getContainer()->get('lexik_maintenance.driver.factory')->getDriver();
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param $question
* @param InputInterface $input
* @param OutputInterface $output
* @return mixed
*/
protected function askConfirmation($question, InputInterface $input, OutputInterface $output) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askConfirmation($output, '<question>' . $question . '</question>', 'y');
}

return $this->getHelper('question')
->ask($input, $output, new \Symfony\Component\Console\Question\ConfirmationQuestion($question));
$this->interactWithTtl($input, $output);
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param $question
* @param $validator
* @param int $attempts
* @param null $default
* @return mixed
*/
protected function askAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = 1, $default = null) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askAndValidate($output, $question, $validator, $attempts, $default);
}

$question = new \Symfony\Component\Console\Question\Question($question, $default);
$question->setValidator($validator);
$question->setMaxAttempts($attempts);

return $this->getHelper('question')
->ask($input, $output, $question);
}
}
Loading