Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit 746ed0d

Browse files
Use laravel's locking from version 5.8 upwards
1 parent 1494154 commit 746ed0d

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "6.1-dev"
44+
"dev-master": "6.2-dev"
4545
},
4646
"laravel": {
4747
"providers": [
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Alt Three Locker.
7+
*
8+
* (c) Alt Three Services Limited
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace AltThree\Locker\Connections;
15+
16+
use Illuminate\Contracts\Cache\LockProvider;
17+
18+
/**
19+
* This is the lock provider connection class.
20+
*
21+
* @author Graham Campbell <graham@alt-three.com>
22+
*/
23+
class LockProviderConnection implements ConnectionInterface
24+
{
25+
/**
26+
* The lock provider instance.
27+
*
28+
* @var \Illuminate\Contracts\Cache\LockProvider
29+
*/
30+
protected $provider;
31+
32+
/**
33+
* Create a new lock provider connection class.
34+
*
35+
* @param \Illuminate\Contracts\Cache\LockProvider $provider
36+
*
37+
* @return void
38+
*/
39+
public function __construct(LockProvider $provider)
40+
{
41+
$this->provider = $provider;
42+
}
43+
44+
/**
45+
* Store the value at the given key.
46+
*
47+
* Returns true if the key was fresh.
48+
*
49+
* @param string $key
50+
* @param string $value
51+
* @param int $timeout
52+
*
53+
* @return bool
54+
*/
55+
public function store(string $key, string $value, int $timeout)
56+
{
57+
return $this->provider->lock($key, intdiv($timeout, 1000), $value)->get();
58+
}
59+
60+
/**
61+
* Remove the value at the given key.
62+
*
63+
* @param string $key
64+
*
65+
* @return void
66+
*/
67+
public function remove(string $key)
68+
{
69+
return $this->provider->lock($key)->forceRelease();
70+
}
71+
}

src/LockerServiceProvider.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
use AltThree\Locker\Connections\ConnectionInterface;
1717
use AltThree\Locker\Connections\IlluminateConnection;
18-
use AltThree\Locker\Connections\PredisConnection;
18+
use AltThree\Locker\Connections\LockProviderConnection;
19+
use Illuminate\Cache\RedisStore;
1920
use Illuminate\Contracts\Container\Container;
2021
use Illuminate\Foundation\Application as LaravelApplication;
2122
use Illuminate\Support\ServiceProvider;
2223
use Laravel\Lumen\Application as LumenApplication;
2324
use Predis\ClientInterface;
25+
use ReflectionClass;
2426

2527
/**
2628
* This is the locker service provider class.
@@ -76,12 +78,16 @@ public function register()
7678
protected function registerConnection()
7779
{
7880
$this->app->singleton('locker.connection', function (Container $app) {
79-
$redis = $app['redis']->connection($app->config->get('locker.connection'));
81+
$connection = $app->config->get('locker.connection');
8082

81-
if ($redis instanceof ClientInterface) {
82-
return new PredisConnection($redis);
83+
if (count((new ReflectionClass(RedisStore::class))->getMethod('lock')->getParameters()) > 2) {
84+
$provider = new RedisStore($app['redis'], '', $connection);
85+
86+
return new LockProviderConnection($provider);
8387
}
8488

89+
$redis = $app['redis']->connection($connection);
90+
8591
return new IlluminateConnection($redis);
8692
});
8793

tests/AnalysisTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace AltThree\Tests\Locker;
1515

1616
use GrahamCampbell\Analyzer\AnalysisTrait;
17-
use Illuminate\Redis\Connections\Connection;
1817
use Laravel\Lumen\Application;
1918
use PHPUnit\Framework\TestCase;
2019

@@ -48,6 +47,6 @@ protected function getPaths()
4847
*/
4948
protected function getIgnored()
5049
{
51-
return [Application::class, Connection::class];
50+
return [Application::class];
5251
}
5352
}

tests/ServiceProviderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use AltThree\Locker\Connections\ConnectionInterface;
1717
use AltThree\Locker\Connections\IlluminateConnection;
18-
use AltThree\Locker\Connections\PredisConnection;
18+
use AltThree\Locker\Connections\LockProviderConnection;
1919
use AltThree\Locker\Http\Middleware\LockingMiddleware;
2020
use AltThree\Locker\Locker;
2121
use GrahamCampbell\TestBenchCore\ServiceProviderTrait;
@@ -49,7 +49,7 @@ public function testConnectionIsInjectable()
4949
$this->assertIsInjectable(ConnectionInterface::class);
5050

5151
$this->assertInstanceOf(
52-
class_exists(Connection::class) ? IlluminateConnection::class : PredisConnection::class,
52+
version_compare($this->app->version(), '5.8') >= 0 ? LockProviderConnection::class : IlluminateConnection::class,
5353
$this->app->make('locker.connection')
5454
);
5555
}

0 commit comments

Comments
 (0)