Skip to content

Commit a522801

Browse files
committed
Add missing lock connection string in FrameworkExtension
1 parent 787adcb commit a522801

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ CHANGELOG
77
* added InvalidTtlException
88
* deprecated `StoreInterface` in favor of `BlockingStoreInterface` and `PersistingStoreInterface`
99
* `Factory` is deprecated, use `LockFactory` instead
10-
10+
* `StoreFactory::createStore` allows PDO and Zookeeper DSN.
11+
* deprecated services `lock.store.flock`, `lock.store.semaphore`, `lock.store.memcached.abstract` and `lock.store.redis.abstract`,
12+
use `StoreFactory::createStore` instead.
13+
1114
4.2.0
1215
-----
1316

Store/StoreFactory.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,24 @@ public static function createStore($connection)
5858
return new FlockStore(substr($connection, 8));
5959
case 'semaphore' === $connection:
6060
return new SemaphoreStore();
61-
case class_exists(AbstractAdapter::class) && preg_match('#^[a-z]++://#', $connection):
62-
return static::createStore(AbstractAdapter::createConnection($connection));
61+
case 0 === strpos($connection, 'redis://') && class_exists(AbstractAdapter::class):
62+
case 0 === strpos($connection, 'rediss://') && class_exists(AbstractAdapter::class):
63+
return new RedisStore(AbstractAdapter::createConnection($connection, ['lazy' => true]));
64+
case 0 === strpos($connection, 'memcached://') && class_exists(AbstractAdapter::class):
65+
return new MemcachedStore(AbstractAdapter::createConnection($connection, ['lazy' => true]));
66+
case 0 === strpos($connection, 'sqlite:'):
67+
case 0 === strpos($connection, 'mysql:'):
68+
case 0 === strpos($connection, 'pgsql:'):
69+
case 0 === strpos($connection, 'oci:'):
70+
case 0 === strpos($connection, 'sqlsrv:'):
71+
case 0 === strpos($connection, 'sqlite3://'):
72+
case 0 === strpos($connection, 'mysql2://'):
73+
case 0 === strpos($connection, 'postgres://'):
74+
case 0 === strpos($connection, 'postgresql://'):
75+
case 0 === strpos($connection, 'mssql://'):
76+
return new PdoStore($connection);
77+
case 0 === strpos($connection, 'zookeeper://'):
78+
return new ZookeeperStore(ZookeeperStore::createConnection($connection));
6379
default:
6480
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
6581
}

Store/ZookeeperStore.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14+
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1415
use Symfony\Component\Lock\Exception\LockAcquiringException;
1516
use Symfony\Component\Lock\Exception\LockConflictedException;
1617
use Symfony\Component\Lock\Exception\LockReleasingException;
@@ -34,6 +35,24 @@ public function __construct(\Zookeeper $zookeeper)
3435
$this->zookeeper = $zookeeper;
3536
}
3637

38+
public static function createConnection(string $dsn): \Zookeeper
39+
{
40+
if (0 !== strpos($dsn, 'zookeeper:')) {
41+
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
42+
}
43+
44+
if (false === $params = parse_url($dsn)) {
45+
throw new InvalidArgumentException(sprintf('Invalid Zookeeper DSN: %s.', $dsn));
46+
}
47+
48+
$host = $params['host'] ?? '';
49+
if (isset($params['port'])) {
50+
$host .= ':'.$params['port'];
51+
}
52+
53+
return new \Zookeeper($host);
54+
}
55+
3756
/**
3857
* {@inheritdoc}
3958
*/

Tests/Store/StoreFactoryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Cache\Traits\RedisProxy;
1717
use Symfony\Component\Lock\Store\FlockStore;
1818
use Symfony\Component\Lock\Store\MemcachedStore;
19+
use Symfony\Component\Lock\Store\PdoStore;
1920
use Symfony\Component\Lock\Store\RedisStore;
2021
use Symfony\Component\Lock\Store\SemaphoreStore;
2122
use Symfony\Component\Lock\Store\StoreFactory;
@@ -50,13 +51,34 @@ public function validConnections()
5051
}
5152
if (class_exists(\Zookeeper::class)) {
5253
yield [$this->createMock(\Zookeeper::class), ZookeeperStore::class];
54+
yield ['zookeeper://localhost:2181', ZookeeperStore::class];
5355
}
5456
if (\extension_loaded('sysvsem')) {
5557
yield ['semaphore', SemaphoreStore::class];
5658
}
5759
if (class_exists(\Memcached::class) && class_exists(AbstractAdapter::class)) {
5860
yield ['memcached://server.com', MemcachedStore::class];
5961
}
62+
if (class_exists(\Redis::class) && class_exists(AbstractAdapter::class)) {
63+
yield ['redis://localhost', RedisStore::class];
64+
}
65+
if (class_exists(\PDO::class)) {
66+
yield ['sqlite:/tmp/sqlite.db', PdoStore::class];
67+
yield ['sqlite::memory:', PdoStore::class];
68+
yield ['mysql:host=localhost;dbname=test;', PdoStore::class];
69+
yield ['pgsql:host=localhost;dbname=test;', PdoStore::class];
70+
yield ['oci:host=localhost;dbname=test;', PdoStore::class];
71+
yield ['sqlsrv:server=localhost;Database=test', PdoStore::class];
72+
yield ['mysql://server.com/test', PdoStore::class];
73+
yield ['mysql2://server.com/test', PdoStore::class];
74+
yield ['pgsql://server.com/test', PdoStore::class];
75+
yield ['postgres://server.com/test', PdoStore::class];
76+
yield ['postgresql://server.com/test', PdoStore::class];
77+
yield ['sqlite:///tmp/test', PdoStore::class];
78+
yield ['sqlite3:///tmp/test', PdoStore::class];
79+
yield ['oci:///server.com/test', PdoStore::class];
80+
yield ['mssql:///server.com/test', PdoStore::class];
81+
}
6082

6183
yield ['flock', FlockStore::class];
6284
yield ['flock://'.sys_get_temp_dir(), FlockStore::class];

Tests/Store/ZookeeperStoreTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,26 @@ public function getStore(): PersistingStoreInterface
3030
{
3131
$zookeeper_server = getenv('ZOOKEEPER_HOST').':2181';
3232

33-
$zookeeper = new \Zookeeper(implode(',', [$zookeeper_server]));
33+
$zookeeper = new \Zookeeper($zookeeper_server);
3434

3535
return StoreFactory::createStore($zookeeper);
3636
}
3737

38+
/**
39+
* @dataProvider provideValidConnectionString
40+
*/
41+
public function testCreateConnection(string $connectionString)
42+
{
43+
$this->assertInstanceOf(\Zookeeper::class, ZookeeperStore::createConnection($connectionString));
44+
}
45+
46+
public function provideValidConnectionString(): iterable
47+
{
48+
yield 'single host' => ['zookeeper://localhost:2181'];
49+
yield 'single multiple host' => ['zookeeper://localhost:2181,localhost:2181'];
50+
yield 'with extra attributes' => ['zookeeper://localhost:2181/path?option=value'];
51+
}
52+
3853
public function testSaveSucceedsWhenPathContainsMoreThanOneNode()
3954
{
4055
$store = $this->getStore();

0 commit comments

Comments
 (0)