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

Support for Symfony Events #85

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions Drivers/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Lexik\Bundle\MaintenanceBundle\Drivers;

use Lexik\Bundle\MaintenanceBundle\Event\PostLockEvent;
use Lexik\Bundle\MaintenanceBundle\Event\PostUnlockEvent;
use Lexik\Bundle\MaintenanceBundle\Event\PreLockEvent;
use Lexik\Bundle\MaintenanceBundle\Event\PreUnlockEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
Expand All @@ -22,6 +27,11 @@ abstract class AbstractDriver
*/
protected $translator;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* Constructor
*
Expand Down Expand Up @@ -76,10 +86,14 @@ abstract public function getMessageUnlock($resultTest);
*
* @return boolean
*/
public function lock()
final public function lock()
{
if (!$this->isExists()) {
return $this->createLock();
$this->eventDispatcher->dispatch(PreLockEvent::NAME, new PreLockEvent());
$success = $this->createLock();
$this->eventDispatcher->dispatch(PostLockEvent::NAME, new PostLockEvent($success));

return $success;
} else {
return false;
}
Expand All @@ -90,10 +104,14 @@ public function lock()
*
* @return boolean
*/
public function unlock()
final public function unlock()
{
if ($this->isExists()) {
return $this->createUnlock();
$this->eventDispatcher->dispatch(PreUnlockEvent::NAME, new PreUnlockEvent());
$success = $this->createUnlock();
$this->eventDispatcher->dispatch(PostUnlockEvent::NAME, new PostUnlockEvent($success));

return $success;
} else {
return false;
}
Expand Down Expand Up @@ -128,4 +146,15 @@ public function setTranslator(TranslatorInterface $translator)
{
$this->translator = $translator;
}

/**
* @param EventDispatcherInterface $eventDispatcher
* @return AbstractDriver
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;

return $this;
}
}
17 changes: 13 additions & 4 deletions Drivers/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

/**
Expand All @@ -29,17 +30,23 @@ class DriverFactory
*/
protected $translator;

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

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

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

Expand All @@ -49,6 +56,7 @@ public function __construct(DatabaseDriver $dbDriver, TranslatorInterface $trans

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

/**
Expand All @@ -73,6 +81,7 @@ public function getDriver()
}

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

return $driver;
}
Expand Down
156 changes: 156 additions & 0 deletions Drivers/MemcachedDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Drivers;

/**
* Class to handle a memcached driver
*
* @package LexikMaintenanceBundle
*/
class MemcachedDriver extends AbstractDriver implements DriverTtlInterface
{
/**
* Value store in memcache
*
* @var string
*/
const VALUE_TO_STORE = 'maintenance';

/**
* The key store in memcache
*
* @var string keyName
*/
protected $key;

/**
* Memcached instance
*
* @var \Memcached
*/
protected $memcached;

/**
* @param array $options
*
* @throws \InvalidArgumentException
*/
public function __construct(array $options = array())
{
parent::__construct($options);

if (isset($options['key']) === false) {
throw new \InvalidArgumentException('Option "key" must be defined if driver Memcached configuration is used');
}

if (isset($options['servers']) === false || empty($options['servers']) === true) {
throw new \InvalidArgumentException('Option "servers" must be defined if driver Memcached configuration is used');
}

$this->key = $options['key'];
// TODO: A configured Memcached instance should be injected into the constructor for easier testing.
$this->setMemcached(new \Memcached());
foreach ($options['servers'] as $server) {
if (isset($server['host']) === false) {
throw new \InvalidArgumentException('Option "host" must be defined for each server if driver Memcached configuration is used');
}

if (isset($server['port']) === false || is_int($server['port']) === false) {
throw new \InvalidArgumentException('Option "port" must be defined as an integer for each server if driver Memcached configuration is used');
}

$this->getMemcached()->addServer($server['host'], $server['port']);
}
}

/**
* {@inheritdoc}
*/
public function isExists()
{
if (false !== $this->getMemcached()->get($this->key)) {
return true;
}

return false;
}

/**
* {@inheritdoc}
*/
public function getMessageLock($resultTest)
{
$key = $resultTest ? 'lexik_maintenance.success_lock_memc' : 'lexik_maintenance.not_success_lock';

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

/**
* {@inheritdoc}
*/
public function getMessageUnlock($resultTest)
{
$key = $resultTest ? 'lexik_maintenance.success_unlock' : 'lexik_maintenance.not_success_unlock';

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

/**
* {@inheritdoc}
*/
public function setTtl($value)
{
$this->options['ttl'] = $value;
}

/**
* {@inheritdoc}
*/
public function getTtl()
{
return $this->options['ttl'];
}

/**
* {@inheritdoc}
*/
public function hasTtl()
{
return isset($this->options['ttl']);
}

/**
* {@inheritdoc}
*/
protected function createLock()
{
return $this->getMemcached()->set($this->key, self::VALUE_TO_STORE, (isset($this->options['ttl']) ? $this->options['ttl'] : 0));
}

/**
* {@inheritdoc}
*/
protected function createUnlock()
{
return $this->getMemcached()->delete($this->key);
}

/**
* @return \Memcached
*/
private function getMemcached()
{
return $this->memcached;
}

/**
* @param \Memcached $memcached
* @return MemcachedDriver
*/
private function setMemcached($memcached)
{
$this->memcached = $memcached;

return $this;
}
}
15 changes: 15 additions & 0 deletions Event/AbstractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Event;

use Symfony\Component\EventDispatcher\Event;

/**
* Class AbstractEvent
*
* @package Lexik\Bundle\MaintenanceBundle\Event
*/
abstract class AbstractEvent extends Event
{

}
45 changes: 45 additions & 0 deletions Event/AbstractPostEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Event;

/**
* Class AbstractPostEvent
*
* @package Lexik\Bundle\MaintenanceBundle\Event
*/
class AbstractPostEvent extends AbstractEvent
{
/**
* @var bool
*/
private $success;

/**
* PostUnlockEvent constructor.
*
* @param bool $success
*/
public function __construct($success)
{
$this->setSuccess($success);
}

/**
* @return bool
*/
public function isSuccess()
{
return $this->success;
}

/**
* @param bool $success
* @return AbstractPostEvent
*/
private function setSuccess($success)
{
$this->success = $success;

return $this;
}
}
13 changes: 13 additions & 0 deletions Event/PostLockEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Event;

/**
* An event dispatched just after the lock command is executed
*
* @package Lexik\Bundle\MaintenanceBundle\Event
*/
class PostLockEvent extends AbstractPostEvent
{
const NAME = 'maintenance-bundle.lock.post';
}
13 changes: 13 additions & 0 deletions Event/PostUnlockEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Event;

/**
* An event dispatched just after the unlock command is executed
*
* @package Lexik\Bundle\MaintenanceBundle\Event
*/
class PostUnlockEvent extends AbstractPostEvent
{
const NAME = 'maintenance-bundle.unlock.post';
}
13 changes: 13 additions & 0 deletions Event/PreLockEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Event;

/**
* An event dispatched just before the lock command is executed
*
* @package Lexik\Bundle\MaintenanceBundle\Event
*/
class PreLockEvent extends AbstractEvent
{
const NAME = 'maintenance-bundle.lock.pre';
}
Loading