Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b57ea9

Browse files
committed
Use env variable to create anytype of lock store
1 parent 4c78e60 commit 6b57ea9

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

src/Symfony/Component/Lock/Store/StoreFactory.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1415
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1516
use Symfony\Component\Cache\Traits\RedisProxy;
1617
use Symfony\Component\Lock\Exception\InvalidArgumentException;
18+
use Symfony\Component\Lock\StoreInterface;
1719

1820
/**
1921
* StoreFactory create stores and connections.
@@ -23,9 +25,9 @@
2325
class StoreFactory
2426
{
2527
/**
26-
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Memcached|\Zookeeper $connection
28+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Memcached|\Zookeeper|string $connection Connection or DSN or Store short name
2729
*
28-
* @return RedisStore|MemcachedStore|ZookeeperStore
30+
* @return StoreInterface
2931
*/
3032
public static function createStore($connection)
3133
{
@@ -45,7 +47,21 @@ public static function createStore($connection)
4547
if ($connection instanceof \Zookeeper) {
4648
return new ZookeeperStore($connection);
4749
}
50+
if (!\is_string($connection)) {
51+
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
52+
}
4853

49-
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
54+
switch (true) {
55+
case 'flock' === $connection:
56+
return new FlockStore();
57+
case 0 === strpos($connection, 'flock://'):
58+
return new FlockStore(substr($connection, 8));
59+
case 'semaphore' === $connection:
60+
return new SemaphoreStore();
61+
case preg_match('#^[a-z]++://#', $connection):
62+
return static::createStore(AbstractAdapter::createConnection($connection));
63+
default:
64+
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
65+
}
5066
}
5167
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Lock\Tests\Store;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Cache\Traits\RedisProxy;
16+
use Symfony\Component\Lock\Store\FlockStore;
17+
use Symfony\Component\Lock\Store\MemcachedStore;
18+
use Symfony\Component\Lock\Store\RedisStore;
19+
use Symfony\Component\Lock\Store\SemaphoreStore;
20+
use Symfony\Component\Lock\Store\StoreFactory;
21+
use Symfony\Component\Lock\Store\ZookeeperStore;
22+
23+
/**
24+
* @author Jérémy Derussé <[email protected]>
25+
*/
26+
class StoreFactoryTest extends TestCase
27+
{
28+
/**
29+
* @dataProvider validConnections
30+
*/
31+
public function testCreateStore($connection, string $expectedStoreClass)
32+
{
33+
$store = StoreFactory::createStore($connection);
34+
35+
$this->assertInstanceOf($expectedStoreClass, $store);
36+
}
37+
38+
public function validConnections()
39+
{
40+
if (\class_exists(\Redis::class)) {
41+
yield [$this->createMock(\Redis::class), RedisStore::class];
42+
}
43+
yield [$this->createMock(RedisProxy::class), RedisStore::class];
44+
yield [$this->createMock(\Predis\Client::class), RedisStore::class];
45+
if (\class_exists(\Memcached::class)) {
46+
yield [$this->createMock(\Memcached::class), MemcachedStore::class];
47+
}
48+
if (\class_exists(\Zookeeper::class)) {
49+
yield [$this->createMock(\Zookeeper::class), ZookeeperStore::class];
50+
}
51+
yield ['flock', FlockStore::class];
52+
yield ['flock:///tmp', FlockStore::class];
53+
yield ['semaphore', SemaphoreStore::class];
54+
if (\class_exists(\Memcached::class)) {
55+
yield ['memcached://server.com', MemcachedStore::class];
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)