+ */
+class StoreFactory
+{
+ public static function createConnection($dsn, array $options = array())
+ {
+ if (!is_string($dsn)) {
+ throw new InvalidArgumentException(sprintf('The %s() method expects argument #1 to be string, %s given.', __METHOD__, gettype($dsn)));
+ }
+ if (0 === strpos($dsn, 'redis://')) {
+ return RedisStore::createConnection($dsn, $options);
+ }
+ if (0 === strpos($dsn, 'memcached://')) {
+ return MemcachedStore::createConnection($dsn, $options);
+ }
+
+ throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
+ }
+
+ /**
+ * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Memcached $connection
+ *
+ * @return RedisStore|MemcachedStore
+ */
+ public static function createStore($connection)
+ {
+ if ($connection instanceof \Redis || $connection instanceof \RedisArray || $connection instanceof \RedisCluster || $connection instanceof \Predis\Client) {
+ return new RedisStore($connection);
+ }
+ if ($connection instanceof \Memcached) {
+ return new MemcachedStore($connection);
+ }
+
+ throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', get_class($connection)));
+ }
+}
diff --git a/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php
index 53d2ae78dc78a..ef3650c3124b5 100644
--- a/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php
+++ b/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php
@@ -26,7 +26,7 @@ class FlockStoreTest extends AbstractStoreTest
*/
protected function getStore()
{
- return new FlockStore(sys_get_temp_dir());
+ return new FlockStore();
}
/**
diff --git a/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php
index eb030fba0f9de..cfe03b25e2c34 100644
--- a/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php
+++ b/src/Symfony/Component/Lock/Tests/Store/MemcachedStoreTest.php
@@ -54,4 +54,100 @@ public function testAbortAfterExpiration()
{
$this->markTestSkipped('Memcached expects a TTL greater than 1 sec. Simulating a slow network is too hard');
}
+
+ public function testDefaultOptions()
+ {
+ $this->assertTrue(MemcachedStore::isSupported());
+
+ $client = MemcachedStore::createConnection('memcached://127.0.0.1');
+
+ $this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
+ $this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
+ $this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
+ }
+
+ /**
+ * @dataProvider provideServersSetting
+ */
+ public function testServersSetting($dsn, $host, $port)
+ {
+ $client1 = MemcachedStore::createConnection($dsn);
+ $client3 = MemcachedStore::createConnection(array($host, $port));
+ $expect = array(
+ 'host' => $host,
+ 'port' => $port,
+ );
+
+ $f = function ($s) { return array('host' => $s['host'], 'port' => $s['port']); };
+ $this->assertSame(array($expect), array_map($f, $client1->getServerList()));
+ $this->assertSame(array($expect), array_map($f, $client3->getServerList()));
+ }
+
+ public function provideServersSetting()
+ {
+ yield array(
+ 'memcached://127.0.0.1/50',
+ '127.0.0.1',
+ 11211,
+ );
+ yield array(
+ 'memcached://localhost:11222?weight=25',
+ 'localhost',
+ 11222,
+ );
+ if (ini_get('memcached.use_sasl')) {
+ yield array(
+ 'memcached://user:password@127.0.0.1?weight=50',
+ '127.0.0.1',
+ 11211,
+ );
+ }
+ yield array(
+ 'memcached:///var/run/memcached.sock?weight=25',
+ '/var/run/memcached.sock',
+ 0,
+ );
+ yield array(
+ 'memcached:///var/local/run/memcached.socket?weight=25',
+ '/var/local/run/memcached.socket',
+ 0,
+ );
+ if (ini_get('memcached.use_sasl')) {
+ yield array(
+ 'memcached://user:password@/var/local/run/memcached.socket?weight=25',
+ '/var/local/run/memcached.socket',
+ 0,
+ );
+ }
+ }
+
+ /**
+ * @dataProvider provideDsnWithOptions
+ */
+ public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
+ {
+ $client = MemcachedStore::createConnection($dsn, $options);
+
+ foreach ($expectedOptions as $option => $expect) {
+ $this->assertSame($expect, $client->getOption($option));
+ }
+ }
+
+ public function provideDsnWithOptions()
+ {
+ if (!class_exists('\Memcached')) {
+ self::markTestSkipped('Extension memcached required.');
+ }
+
+ yield array(
+ 'memcached://localhost:11222?retry_timeout=10',
+ array(\Memcached::OPT_RETRY_TIMEOUT => 8),
+ array(\Memcached::OPT_RETRY_TIMEOUT => 10),
+ );
+ yield array(
+ 'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2',
+ array(\Memcached::OPT_RETRY_TIMEOUT => 8),
+ array(\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8),
+ );
+ }
}
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