diff --git a/.travis.yml b/.travis.yml index f0fecc3..649399e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,9 @@ php: - 7.0 - hhvm +services: + - redis-server + sudo: false cache: diff --git a/Drivers/RedisDriver.php b/Drivers/RedisDriver.php new file mode 100644 index 0000000..a218547 --- /dev/null +++ b/Drivers/RedisDriver.php @@ -0,0 +1,135 @@ +keyName = $options['key_name']; + + $this->redisInstance = new \Predis\Client($options['connection_parameters']); + $this->redisInstance->connect(); + } + + $this->options = $options; + } + + function __destruct() + { + if (isset($this->redisInstance)) { + $this->redisInstance->disconnect(); + } + + unset($this->redisInstance); + } + + + /** + * {@inheritdoc} + */ + protected function createLock() + { + return ($this->redisInstance->setex($this->keyName, $this->options['ttl'], true) === 'OK'); + } + + /** + * {@inheritdoc} + */ + protected function createUnlock() + { + return $this->redisInstance->del(array($this->keyName)) > 0; + } + + /** + * {@inheritdoc} + */ + public function isExists() + { + return $this->redisInstance->exists($this->keyName); + } + + /** + * {@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']); + } +} \ No newline at end of file diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 91e9704..d690748 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -95,6 +95,11 @@ The ttl (time to life) option is optional everywhere, it is used to indicate the class: Lexik\Bundle\MaintenanceBundle\Drivers\MemCacheDriver # class for MemCache driver options: {key_name: 'maintenance', host: 127.0.0.1, port: 11211} # need to define a key_name, the host and port + # Redis driver + class: Lexik\Bundle\MaintenanceBundle\Drivers\RedisDriver + # Need to define a key_name and connection_parameters. Any valid redis connection uri, see https://github.com/nrk/predis#connecting-to-redis + options: { key_name: 'maintenance', connection_parameters: 'tcp://127.0.0.1:6379' } + # Database driver: class: 'Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver' # class for database driver diff --git a/Tests/Maintenance/RedisDriverTest.php b/Tests/Maintenance/RedisDriverTest.php new file mode 100644 index 0000000..3f77ecc --- /dev/null +++ b/Tests/Maintenance/RedisDriverTest.php @@ -0,0 +1,74 @@ + 'foo')); + } + + public function testTtlIsSetWithDefaultValue() + { + $redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost')); + + $this->assertTrue($redis->hasTtl()); + $this->assertEquals(0, $redis->getTtl()); + } + + public function testTtlIsSetWithCustomValue() + { + $redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost', 'ttl' => 1234)); + + $this->assertTrue($redis->hasTtl()); + $this->assertEquals(1234, $redis->getTtl()); + } + + public function testTtlIsSetWithCustomValueAsString() + { + $redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost', 'ttl' => '1234')); + + $this->assertTrue($redis->hasTtl()); + $this->assertEquals(1234, $redis->getTtl()); + } + + public function testKeyName() + { + $redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost')); + + $property = new \ReflectionProperty($redis, 'keyName'); + $property->setAccessible(true); + + $this->assertEquals('foo', $property->getValue($redis)); + } + + public function testRedisInstance() + { + $redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost')); + + $property = new \ReflectionProperty($redis, 'redisInstance'); + $property->setAccessible(true); + + $this->assertNotEmpty($property->getValue($redis)); + $this->assertInstanceOf('\Predis\Client', $property->getValue($redis)); + } +} diff --git a/composer.json b/composer.json index 8d5513c..f670f34 100644 --- a/composer.json +++ b/composer.json @@ -21,10 +21,13 @@ ], "require": { "php": ">=5.3.2", - "symfony/framework-bundle": "~2.7|~3.0" + "symfony/framework-bundle": "~2.7|~3.0", + "symfony/translation": "~2.7|~3.0", + "predis/predis": "^1.1" }, "require-dev": { - "symfony/phpunit-bridge": "~2.7|~3.0" + "symfony/phpunit-bridge": "~2.7|~3.0", + "phpunit/phpunit": "~4.8" }, "autoload": { "psr-4": { "Lexik\\Bundle\\MaintenanceBundle\\": "" }