Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Gauthier committed Nov 9, 2011
0 parents commit 596eb6e
Show file tree
Hide file tree
Showing 26 changed files with 2,028 additions and 0 deletions.
116 changes: 116 additions & 0 deletions Command/DriverLockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Lexik\Bundle\MaintenanceBundle\Drivers\FileDriver;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;

/**
* Create a lock action
*
* @package LexikMaintenanceBundle
* @author Gilles Gauthier <[email protected]>
*/
class DriverLockCommand extends ContainerAwareCommand
{
protected $driver;

/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::configure()
*/
protected function configure()
{
$this
->setName('lexik:maintenance:lock')
->setDescription('Lock access to the site while maintenance...');

$this->addOption(
'set-ttl', 'ttl',
InputOption::VALUE_NONE,
'Overwrite time to life from your configuration, doesn\'t work with file driver. Time in seconds.',
null
);
}

/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::execute()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->driver->setTtl($input->getOption('set-ttl'));

$lockMessage = $this->driver->getMessageLock($this->driver->lock());

$output->writeln('<info>'.$lockMessage.'</info>');
}

/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::interact()
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->driver = $this->getContainer()->get('lexik_maintenance.driver.factory')->getDriver();

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

// confirme
$output->writeln(array(
'',
$formatter->formatBlock('You are about to launch maintenance', 'bg=red;fg=white', true),
'',
));

$confirmation = $dialog->askConfirmation(
$output,
'<question>WARNING! Are you sure you wish to continue? (y/n)</question>',
'y'
);

if ($confirmation === true) {
// pass option ttl in command ?
$optiontTtl = false !== $input->getOption('set-ttl') ? true : false;

// Get default value
$default = $this->driver->getOptions();

if (false === $optiontTtl && !($this->driver instanceof FileDriver)) {
$output->writeln(array(
'',
'Do you want to redefine maintenance life time ?',
'If yes enter the number of seconds',
'<fg=red>This doesn\'t work with file driver</>',
'',
));

$ttl = $dialog->askAndValidate(
$output,
sprintf('<info>%s</info> [<comment>Default value in your configuration: %s</comment>]%s ', 'Set time', isset($default['ttl']) ? $default['ttl'] : '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;
},
false,
isset($default['ttl']) ? $default['ttl'] : 0
);

$input->setOption('set-ttl', $ttl);
} else {
$input->setOption('set-ttl', isset($default['ttl']) ? $default['ttl'] : null);
}
} else {
$output->writeln('<error>Maintenance cancelled!</error>');
return exit;
}
}
}
70 changes: 70 additions & 0 deletions Command/DriverUnlockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Command;

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

/**
* Create an unlock action
*
* @package LexikMaintenanceBundle
* @author Gilles Gauthier <[email protected]>
*/
class DriverUnlockCommand extends ContainerAwareCommand
{
/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::configure()
*/
protected function configure()
{
$this
->setName('lexik:maintenance:unlock')
->setDescription('Unlock access to the site while maintenance...');
}

/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::execute()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$driver = $this->getContainer()->get('lexik_maintenance.driver.factory')->getDriver();

$unlockMessage = $driver->getMessageUnlock($driver->unlock());

$output->writeln('<info>'.$unlockMessage.'</info>');
}

/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::interact()
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$formatter = $this->getHelperSet()->get('formatter');

// confirme
$output->writeln(array(
'',
$formatter->formatBlock('You are about to unlock your server.', 'bg=green;fg=white', true),
'',
));

$confirmation = $dialog->askConfirmation(
$output,
'<question>WARNING! Are you sure you wish to continue? (y/n) </question>',
'y'
);

if ($confirmation) {
return;
} else {
$output->writeln('<error>Action cancelled!</error>');
exit;
}
}
}
50 changes: 50 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*
* @package LexikMaintenanceBundle
* @author Gilles Gauthier <[email protected]>
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('lexik_maintenance');

$rootNode
->addDefaultsIfNotSet()
->children()
->variableNode('authorized_ips')
->defaultNull()
->end()
->arrayNode('driver')
->addDefaultsIfNotSet()
->children()
->scalarNode('class')
->defaultNull()
->end()
->scalarNode('ttl')
->defaultNull()
->end()
->variableNode('options')
->defaultValue(array())
->end()
->end()
->end()
->end();

return $treeBuilder;
}
}
66 changes: 66 additions & 0 deletions DependencyInjection/LexikMaintenanceExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*
* @package LexikMaintenanceBundle
* @author Gilles Gauthier <[email protected]>
*/
class LexikMaintenanceExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$loader->load('database.xml');

if (isset($config['driver']['ttl'])) {
$config['driver']['options']['ttl'] = $config['driver']['ttl'];
}

$container->setParameter('lexik_maintenance.driver', $config['driver']);
$container->setParameter('lexik_maintenance.authorized_ips', $config['authorized_ips']);

if (isset($config['driver']['options']['dsn'])) {
$this->registerDsnconfiguration($config['driver']['options']);
}
}

/**
* Load dsn configuration
*
* @param array $config A configuration array
*
* @throws InvalidArgumentException
*/
protected function registerDsnconfiguration($options)
{
if ( ! isset($options['table'])) {
throw new InvalidArgumentException('You need to define table for dsn use');
}

if ( ! isset($options['user'])) {
throw new InvalidArgumentException('You need to define user for dsn use');
}

if ( ! isset($options['password'])) {
throw new InvalidArgumentException('You need to define password for dsn use');
}
}
}
Loading

0 comments on commit 596eb6e

Please sign in to comment.