From 8d2e803d6cee3db917cd41195714e50c8517e8fb Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 15:11:59 +0100 Subject: [PATCH 1/8] update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 40b879f..b476c56 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ phpunit.xml .settings vendor composer.lock + +# IDE +.idea From ab523aebb54d85d7e0fde62881f8a8de99647da4 Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 15:12:15 +0100 Subject: [PATCH 2/8] add missing package "symfony/translation" --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8d5513c..9b50d6c 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ ], "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" }, "require-dev": { "symfony/phpunit-bridge": "~2.7|~3.0" From 160968639610f3b59e45d938e74395bf459b9bf9 Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 16:09:50 +0100 Subject: [PATCH 3/8] add packages "predis" and "phpunit" (dev) --- composer.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 9b50d6c..f670f34 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,12 @@ "require": { "php": ">=5.3.2", "symfony/framework-bundle": "~2.7|~3.0", - "symfony/translation": "~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\\": "" } From a96e58b5ea7246a0130c230db49eef2f942533f7 Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 16:10:01 +0100 Subject: [PATCH 4/8] implement RedisDriver --- Drivers/RedisDriver.php | 135 ++++++++++++++++++++++++++ Tests/Maintenance/RedisDriverTest.php | 74 ++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 Drivers/RedisDriver.php create mode 100644 Tests/Maintenance/RedisDriverTest.php diff --git a/Drivers/RedisDriver.php b/Drivers/RedisDriver.php new file mode 100644 index 0000000..3992193 --- /dev/null +++ b/Drivers/RedisDriver.php @@ -0,0 +1,135 @@ +redisInstance = new \Predis\Client($options['connection_parameters']); + $this->keyName = $options['key_name']; + } + + $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/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)); + } +} From 036bf7dba269e1202d70cb39c5cb56502ba8193a Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 16:14:09 +0100 Subject: [PATCH 5/8] update doc --- Drivers/RedisDriver.php | 2 -- Resources/doc/index.md | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Drivers/RedisDriver.php b/Drivers/RedisDriver.php index 3992193..7fd1a6a 100644 --- a/Drivers/RedisDriver.php +++ b/Drivers/RedisDriver.php @@ -2,8 +2,6 @@ namespace Lexik\Bundle\MaintenanceBundle\Drivers; -use Doctrine\Common\Cache\PredisCache; - /** * Class RedisDriver * diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 91e9704..2a60ae8 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -95,6 +95,12 @@ 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 + options: # need to define a key_name and connection_parameters + - key_name: 'maintenance' + - connection_parameters: 'tcp://127.0.0.1:6379' # any valid redis connection uri, see https://github.com/nrk/predis#connecting-to-redis + # Database driver: class: 'Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver' # class for database driver From 24402803b535720b539cf56f1f5a672a793121bf Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 16:52:01 +0100 Subject: [PATCH 6/8] update doc --- Resources/doc/index.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 2a60ae8..d690748 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -97,9 +97,8 @@ The ttl (time to life) option is optional everywhere, it is used to indicate the # Redis driver class: Lexik\Bundle\MaintenanceBundle\Drivers\RedisDriver - options: # need to define a key_name and connection_parameters - - key_name: 'maintenance' - - connection_parameters: 'tcp://127.0.0.1:6379' # any valid redis connection uri, see https://github.com/nrk/predis#connecting-to-redis + # 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 From 68299fb1a92186795e05f838b224d85b01bdd66e Mon Sep 17 00:00:00 2001 From: Bruce17 Date: Mon, 23 Jan 2017 17:17:32 +0100 Subject: [PATCH 7/8] add missing redis connect --- .travis.yml | 3 +++ Drivers/RedisDriver.php | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) 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 index 7fd1a6a..a218547 100644 --- a/Drivers/RedisDriver.php +++ b/Drivers/RedisDriver.php @@ -46,8 +46,10 @@ public function __construct(array $options = array()) $options['ttl'] = intval($options['ttl']); } - $this->redisInstance = new \Predis\Client($options['connection_parameters']); $this->keyName = $options['key_name']; + + $this->redisInstance = new \Predis\Client($options['connection_parameters']); + $this->redisInstance->connect(); } $this->options = $options; From f09790f8efed5242a9f9217b484da8f5fa6da48e Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 27 Jan 2017 17:10:48 +0100 Subject: [PATCH 8/8] Update .gitignore --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index b476c56..40b879f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,3 @@ phpunit.xml .settings vendor composer.lock - -# IDE -.idea