8000 Use env variable to create anytype of lock store by jderusse · Pull Request #30935 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Use env variable to create anytype of lock store #30935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Component\Lock\Store;

use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\StoreInterface;

/**
* StoreFactory create stores and connections.
Expand All @@ -23,9 +25,9 @@
class StoreFactory
{
/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Memcached|\Zookeeper $connection
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Memcached|\Zookeeper|string $connection Connection or DSN or Store short name
*
* @return RedisStore|MemcachedStore|ZookeeperStore
* @return StoreInterface
*/
public static function createStore($connection)
{
Expand All @@ -45,7 +47,21 @@ public static function createStore($connection)
if ($connection instanceof \Zookeeper) {
return new ZookeeperStore($connection);
}
if (!\is_string($connection)) {
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
}

throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
switch (true) {
case 'flock' === $connection:
return new FlockStore();
case 0 === strpos($connection, 'flock://'):
return new FlockStore(substr($connection, 8));
case 'semaphore' === $connection:
return new SemaphoreStore();
case preg_match('#^[a-z]++://#', $connection):
return static::createStore(AbstractAdapter::createConnection($connection));
default:
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
}
}
}
58 changes: 58 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/StoreFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Lock\Tests\Store;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Store\FlockStore;
use Symfony\Component\Lock\Store\MemcachedStore;
use Symfony\Component\Lock\Store\RedisStore;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Lock\Store\StoreFactory;
use Symfony\Component\Lock\Store\ZookeeperStore;

/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class StoreFactoryTest extends TestCase
{
/**
* @dataProvider validConnections
*/
public function testCreateStore($connection, string $expectedStoreClass)
{
$store = StoreFactory::createStore($connection);

$this->assertInstanceOf($expectedStoreClass, $store);
}

public function validConnections()
{
if (\class_exists(\Redis::class)) {
yield [$this->createMock(\Redis::class), RedisStore::class];
}
yield [$this->createMock(RedisProxy::class), RedisStore::class];
yield [$this->createMock(\Predis\Client::class), RedisStore::class];
if (\class_exists(\Memcached::class)) {
yield [$this->createMock(\Memcached::class), MemcachedStore::class];
}
if (\class_exists(\Zookeeper::class)) {
yield [$this->createMock(\Zookeeper::class), ZookeeperStore::class];
}
yield ['flock', FlockStore::class];
yield ['flock:///tmp', FlockStore::class];
yield ['semaphore', SemaphoreStore::class];
if (\class_exists(\Memcached::class)) {
yield ['memcached://server.com', MemcachedStore::class];
}
}
}
0