diff --git a/src/Symfony/Component/Lock/Store/StoreFactory.php b/src/Symfony/Component/Lock/Store/StoreFactory.php index 5f97a8715df34..a7c8168d8d562 100644 --- a/src/Symfony/Component/Lock/Store/StoreFactory.php +++ b/src/Symfony/Component/Lock/Store/StoreFactory.php @@ -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. @@ -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) { @@ -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)); + } } } diff --git a/src/Symfony/Component/Lock/Tests/Store/StoreFactoryTest.php b/src/Symfony/Component/Lock/Tests/Store/StoreFactoryTest.php new file mode 100644 index 0000000000000..cd93a8cc9f8ad --- /dev/null +++ b/src/Symfony/Component/Lock/Tests/Store/StoreFactoryTest.php @@ -0,0 +1,58 @@ + + * + * 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é + */ +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]; + } + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy