Skip to content

Commit

Permalink
Merge pull request lexik#41 from lexik/update
Browse files Browse the repository at this point in the history
Update requirements and code
  • Loading branch information
Cédric Girard committed Aug 4, 2015
2 parents db730c0 + 30eba3d commit 66e43fd
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 177 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ php:
- 5.4
- 5.5
- 5.6

env:
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=2.6.*
- SYMFONY_VERSION=2.7.*

before_script:
Expand Down
2 changes: 1 addition & 1 deletion Command/DriverLockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->isInteractive()) {
if (!$dialog->askConfirmation($output, '<question>WARNING! Are you sure you wish to continue? (y/n)</question>', 'y')) {
$output->writeln('<error>Maintenance cancelled!</error>');
exit;
return;
}
} elseif (null !== $input->getArgument('ttl')) {
$this->ttl = $input->getArgument('ttl');
Expand Down
28 changes: 14 additions & 14 deletions Command/DriverUnlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
class DriverUnlockCommand extends ContainerAwareCommand
{
/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::configure()
* {@inheritdoc}
*/
protected function configure()
{
Expand All @@ -32,12 +31,13 @@ protected function configure()
}

/**
* (non-PHPdoc)
* @see Symfony\Component\Console\Command.Command::execute()
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->unlock($input, $output);
if (!$this->confirmUnlock($input, $output)) {
return;
}

$driver = $this->getContainer()->get('lexik_maintenance.driver.factory')->getDriver();

Expand All @@ -47,17 +47,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* (non-PHPdoc)
*/
protected function unlock(InputInterface $input, OutputInterface $output)
* @param InputInterface $input
* @param OutputInterface $output
* @return bool
*/
protected function confirmUnlock(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$formatter = $this->getHelperSet()->get('formatter');

if ($input->getOption('no-interaction', false)) {
$confirmation = true;
}
else {
} else {
// confirm
$output->writeln(array(
'',
Expand All @@ -72,11 +73,10 @@ protected function unlock(InputInterface $input, OutputInterface $output)
);
}

if ($confirmation) {
return;
} else {
if (!$confirmation) {
$output->writeln('<error>Action cancelled!</error>');
exit;
}

return $confirmation;
}
}
20 changes: 9 additions & 11 deletions Drivers/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lexik\Bundle\MaintenanceBundle\Drivers;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Abstract class for drivers
Expand All @@ -18,20 +18,18 @@ abstract class AbstractDriver
protected $options;

/**
* @var Translator
* @var TranslatorInterface
*/
protected $trans;
protected $translator;

/**
* Constructor
*
* @param Translator $trans Translator service
* @param array $options Array of options
* @param array $options Array of options
*/
public function __construct(Translator $trans, array $options = array())
public function __construct(array $options = array())
{
$this->options = $options;
$this->trans = $trans;
}

/**
Expand Down Expand Up @@ -122,12 +120,12 @@ public function getOptions()
}

/**
* Set translator
* Set translatorlator
*
* @param Translator $trans Translator service
* @param TranslatorInterface $translator
*/
public function setTranslator(Translator $trans)
public function setTranslator(TranslatorInterface $translator)
{
$this->trans = $trans;
$this->translator = $translator;
}
}
31 changes: 17 additions & 14 deletions Drivers/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Lexik\Bundle\MaintenanceBundle\Drivers;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Lexik\Bundle\MaintenanceBundle\Drivers\Query\DefaultQuery;
use Lexik\Bundle\MaintenanceBundle\Drivers\Query\DsnQuery;

/**
* Class driver for handle database
Expand All @@ -12,8 +14,19 @@
*/
class DatabaseDriver extends AbstractDriver implements DriverTtlInterface
{
/**
* @var Registry
*/
protected $doctrine;

/**
* @var array
*/
protected $options;

/**
* @var string
*/
protected $db;

/**
Expand Down Expand Up @@ -119,29 +132,19 @@ public function isExists()
*/
public function getMessageLock($resultTest)
{
$message = '';
if ($resultTest) {
$message = $this->trans->trans('lexik_maintenance.success_lock_database', array(), 'maintenance');
} else {
$message = $this->trans->trans('lexik_maintenance.not_success_lock', array(), 'maintenance');
}
$key = $resultTest ? 'lexik_maintenance.success_lock_database' : 'lexik_maintenance.not_success_lock';

return $message;
return $this->translator->trans($key, array(), 'maintenance');
}

/**
* {@inheritDoc}
*/
public function getMessageUnlock($resultTest)
{
$message = '';
if ($resultTest) {
$message = $this->trans->trans('lexik_maintenance.success_unlock', array(), 'maintenance');
} else {
$message = $this->trans->trans('lexik_maintenance.not_success_unlock', array(), 'maintenance');
}
$key = $resultTest ? 'lexik_maintenance.success_unlock' : 'lexik_maintenance.not_success_unlock';

return $message;
return $this->translator->trans($key, array(), 'maintenance');
}

/**
Expand Down
60 changes: 37 additions & 23 deletions Drivers/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Lexik\Bundle\MaintenanceBundle\Drivers;

use Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver;
use Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver\DatabaseDriverInterface;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Factory for create driver
Expand All @@ -16,29 +14,41 @@
*/
class DriverFactory
{
protected $driver;
/**
* @var array
*/
protected $driverOptions;

/**
* @var DatabaseDriver
*/
protected $dbDriver;
protected $trans;

const DATABASE_DRIVER = "Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver";
/**
* @var TranslatorInterface
*/
protected $translator;

const DATABASE_DRIVER = 'Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver';

/**
* Constructor driver factory
*
* @param DatabaseDriver $dbDriver The databaseDriver Service
* @param Translator $trans The translator service
* @param array $driverOptions Options driver
* @param DatabaseDriver $dbDriver The databaseDriver Service
* @param TranslatorInterface $translator The translator service
* @param array $driverOptions Options driver
* @throws \ErrorException
*/
public function __construct(DatabaseDriver $dbDriver, Translator $trans, array $driverOptions)
public function __construct(DatabaseDriver $dbDriver, TranslatorInterface $translator, array $driverOptions)
{
$this->driver = $driverOptions;
$this->driverOptions = $driverOptions;

if ( ! isset($this->driver['class'])) {
if ( ! isset($this->driverOptions['class'])) {
throw new \ErrorException('You need to define a driver class');
}

$this->dbDriver = $dbDriver;
$this->trans = $trans;
$this->translator = $translator;
}

/**
Expand All @@ -49,17 +59,21 @@ public function __construct(DatabaseDriver $dbDriver, Translator $trans, array $
*/
public function getDriver()
{
$class = $this->driver['class'];
if (class_exists($class)) {
if ($class === self::DATABASE_DRIVER) {
$driver = $this->dbDriver;
$driver->setOptions($this->driver['options']);
$driver->setTranslator($this->trans);
return $driver;
}
return new $class($this->trans, $this->driver['options']);
} else {
$class = $this->driverOptions['class'];

if (!class_exists($class)) {
throw new \ErrorException("Class '".$class."' not found in ".get_class($this));
}

if ($class === self::DATABASE_DRIVER) {
$driver = $this->dbDriver;
$driver->setOptions($this->driverOptions['options']);
} else {
$driver = new $class($this->driverOptions['options']);
}

$driver->setTranslator($this->translator);

return $driver;
}
}
40 changes: 12 additions & 28 deletions Drivers/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ class FileDriver extends AbstractDriver
/**
* Constructor
*
* @param Translator $translator Translator service
* @param array $options Options driver
* @param array $options Options driver
*/
public function __construct($translator, array $options = array())
public function __construct(array $options = array())
{
parent::__construct($translator, $options);
parent::__construct($options);

if ( ! isset($options['file_path'])) {
throw new \InvalidArgumentException('$options[\'file_path\'] cannot be defined if Driver File configuration is used');
Expand All @@ -29,26 +28,23 @@ public function __construct($translator, array $options = array())
}

/**
* (non-PHPdoc)
* @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::createLock()
* {@inheritDoc}
*/
protected function createLock()
{
return (fopen($this->filePath, 'w+'));
}

/**
* (non-PHPdoc)
* @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::createUnlock()
* {@inheritDoc}
*/
protected function createUnlock()
{
return @unlink($this->filePath);
}

/**
* (non-PHPdoc)
* @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::isExists()
* {@inheritDoc}
*/
public function isExists()
{
Expand Down Expand Up @@ -84,34 +80,22 @@ public function isEndTime($timeTtl)
}

/**
* (non-PHPdoc)
* @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::getMessageLock()
* {@inheritDoc}
*/
public function getMessageLock($resultTest)
{
$message = '';
if ($resultTest) {
$message = $this->trans->trans('lexik_maintenance.success_lock_file', array(), 'maintenance');
} else {
$message = $this->trans->trans('lexik_maintenance.not_success_lock', array(), 'maintenance');
}
$key = $resultTest ? 'lexik_maintenance.success_lock_file' : 'lexik_maintenance.not_success_lock';

return $message;
return $this->translator->trans($key, array(), 'maintenance');
}

/**
* (non-PHPdoc)
* @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::getMessageUnlock()
* {@inheritDoc}
*/
public function getMessageUnlock($resultTest)
{
$message = '';
if ($resultTest) {
$message = $this->trans->trans('lexik_maintenance.success_unlock', array(), 'maintenance');
} else {
$message = $this->trans->trans('lexik_maintenance.not_success_unlock', array(), 'maintenance');
}
$key = $resultTest ? 'lexik_maintenance.success_unlock' : 'lexik_maintenance.not_success_unlock';

return $message;
return $this->translator->trans($key, array(), 'maintenance');
}
}
Loading

0 comments on commit 66e43fd

Please sign in to comment.