Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 50a24c5

Browse filesBrowse files
committed
Use Dsn component to create connections in cache
1 parent 55a7691 commit 50a24c5
Copy full SHA for 50a24c5

File tree

11 files changed

+151
-241
lines changed
Filter options

11 files changed

+151
-241
lines changed

‎UPGRADE-3.4.md

Copy file name to clipboardExpand all lines: UPGRADE-3.4.md
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
UPGRADE FROM 3.3 to 3.4
22
=======================
33

4+
Cache
5+
-----
6+
7+
* The `AbstractAdapter::createConnection()`, `RedisTrait::createConnection()`
8+
and `MemcachedTrait::createConnection()` methods have been deprecated and
9+
will be removed in 4.0. Use the Dsn component instead.
10+
411
DependencyInjection
512
-------------------
613

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Definition;
2020
use Symfony\Component\DependencyInjection\Reference;
2121
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
22+
use Symfony\Component\Dsn\ConnectionFactory;
2223

2324
/**
2425
* @author Nicolas Grekas <p@tchwork.com>
@@ -126,7 +127,7 @@ public static function getServiceProvider(ContainerBuilder $container, $name)
126127
if (!$container->hasDefinition($name = 'cache_connection.'.ContainerBuilder::hash($dsn))) {
127128
$definition = new Definition(AbstractAdapter::class);
128129
$definition->setPublic(false);
129-
$definition->setFactory(array(AbstractAdapter::class, 'createConnection'));
130+
$definition->setFactory(array(ConnectionFactory::class, 'createConnection'));
130131
$definition->setArguments(array($dsn));
131132
$container->setDefinition($name, $definition);
132133
}

‎src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
+15-7Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Symfony\Component\Cache\CacheItem;
1919
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2020
use Symfony\Component\Cache\Traits\AbstractTrait;
21+
use Symfony\Component\Dsn\ConnectionFactory;
22+
use Symfony\Component\Dsn\Factory\MemcachedConnectionFactory;
23+
use Symfony\Component\Dsn\Factory\RedisConnectionFactory;
2124

2225
/**
2326
* @author Nicolas Grekas <p@tchwork.com>
@@ -127,14 +130,19 @@ public static function createSystemCache($namespace, $defaultLifetime, $version,
127130

128131
public static function createConnection($dsn, array $options = array())
129132
{
130-
if (!is_string($dsn)) {
131-
throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, gettype($dsn)));
132-
}
133-
if (0 === strpos($dsn, 'redis://')) {
134-
return RedisAdapter::createConnection($dsn, $options);
133+
@trigger_error(sprintf('The %s() method is deprecated since version 3.4 and will be removed in 4.0. Use the DsnFactory::createConnection() method from Dsn component instead.', __METHOD__), E_USER_DEPRECATED);
134+
135+
try {
136+
$type = ConnectionFactory::getType($dsn);
137+
} catch (\Symfony\Component\Dsn\Exception\InvalidArgumentException $e) {
138+
throw new InvalidArgumentException($e->getMessage(), 0, $e);
135139
}
136-
if (0 === strpos($dsn, 'memcached://')) {
137-
return MemcachedAdapter::createConnection($dsn, $options);
140+
141+
switch ($type) {
142+
case ConnectionFactory::TYPE_MEMCACHED:
143+
return MemcachedConnectionFactory::createConnection($dsn, $options);
144+
case ConnectionFactory::TYPE_REDIS:
145+
return RedisConnectionFactory::createConnection($dsn, $options);
138146
}
139147

140148
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));

‎src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php
+17-16Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function setupBeforeClass()
2828
if (!MemcachedAdapter::isSupported()) {
2929
self::markTestSkipped('Extension memcached >=2.2.0 required.');
3030
}
31-
self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false));
31+
self::$client = MemcachedConnectionFactory::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false));
3232
self::$client->get('foo');
3333
$code = self::$client->getResultCode();
3434

@@ -39,29 +39,24 @@ public static function setupBeforeClass()
3939

4040
public function createCachePool($defaultLifetime = 0)
4141
{
42-
$client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST')) : self::$client;
42+
$client = $defaultLifetime ? MemcachedConnectionFactory::createConnection('memcached://'.getenv('MEMCACHED_HOST')) : self::$client;
4343

4444
return new MemcachedAdapter($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
4545
}
4646

47-
public function testOptions()
47+
/**
48+
* @group legacy
49+
* @expectedDeprecation This "%s" method is deprecated.
50+
*/
51+
public function testCreateConnectionDeprecated()
4852
{
49-
$client = MemcachedAdapter::createConnection(array(), array(
50-
'libketama_compatible' => false,
51-
'distribution' => 'modula',
52-
'compression' => true,
53-
'serializer' => 'php',
54-
'hash' => 'md5',
55-
));
56-
57-
$this->assertSame(\Memcached::SERIALIZER_PHP, $client->getOption(\Memcached::OPT_SERIALIZER));
58-
$this->assertSame(\Memcached::HASH_MD5, $client->getOption(\Memcached::OPT_HASH));
59-
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
60-
$this->assertSame(0, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
61-
$this->assertSame(\Memcached::DISTRIBUTION_MODULA, $client->getOption(\Memcached::OPT_DISTRIBUTION));
53+
$client = MemcachedAdapter::createConnection('memcached://localhost');
54+
55+
$this->assertInstanceOf(\Memcached::class, $client);
6256
}
6357

6458
/**
59+
* @group legacy
6560
* @dataProvider provideBadOptions
6661
* @expectedException \ErrorException
6762
* @expectedExceptionMessage constant(): Couldn't find constant Memcached::
@@ -81,6 +76,9 @@ public function provideBadOptions()
8176
);
8277
}
8378

79+
/**
80+
* @group legacy
81+
*/
8482
public function testDefaultOptions()
8583
{
8684
$this->assertTrue(MemcachedAdapter::isSupported());
@@ -93,6 +91,7 @@ public function testDefaultOptions()
9391
}
9492

9593
/**
94+
* @group legacy
9695
* @expectedException \Symfony\Component\Cache\Exception\CacheException
9796
* @expectedExceptionMessage MemcachedAdapter: "serializer" option must be "php" or "igbinary".
9897
*/
@@ -106,6 +105,7 @@ public function testOptionSerializer()
106105
}
107106

108107
/**
108+
* @group legacy
109109
* @dataProvider provideServersSetting
110110
*/
111111
public function testServersSetting($dsn, $host, $port)
@@ -163,6 +163,7 @@ public function provideServersSetting()
163163
}
164164

165165
/**
166+
* @group legacy
166167
* @dataProvider provideDsnWithOptions
167168
*/
168169
public function testDsnWithOptions($dsn, array $options, array $expectedOptions)

‎src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public static function setupBeforeClass()
2222
self::$redis = new \Predis\Client(array('host' => getenv('REDIS_HOST')));
2323
}
2424

25+
/**
26+
* @group legacy
27+
* @expectedDeprecation This "%s" method is deprecated.
28+
*/
29+
public function testCreateConnectionDeprecated()
30+
{
31+
$client = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
32+
33+
$this->assertInstanceOf(\Predis\Client, $client);
34+
}
35+
36+
/**
37+
* @group legacy
38+
*/
2539
public function testCreateConnection()
2640
{
2741
$redisHost = getenv('REDIS_HOST');

‎src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public static function setupBeforeClass()
2222
self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
2323
}
2424

25+
/**
26+
* @group legacy
27+
* @expectedDeprecation This "%s" method is deprecated.
28+
*/
29+
public function testCreateConnectionDeprecated()
30+
{
31+
$client = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
32+
33+
$this->assertInstanceOf(\Redis::class, $client);
34+
}
35+
36+
/**
37+
* @group legacy
38+
*/
2539
public function testCreateConnection()
2640
{
2741
$redisHost = getenv('REDIS_HOST');
@@ -45,6 +59,7 @@ public function testCreateConnection()
4559
}
4660

4761
/**
62+
* @group legacy
4863
* @dataProvider provideFailedCreateConnection
4964
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
5065
* @expectedExceptionMessage Redis connection failed
@@ -64,6 +79,7 @@ public function provideFailedCreateConnection()
6479
}
6580

6681
/**
82+
* @group legacy
6783
* @dataProvider provideInvalidCreateConnection
6884
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
6985
* @expectedExceptionMessage Invalid Redis DSN

‎src/Symfony/Component/Cache/Tests/Simple/MemcachedCacheTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Simple/MemcachedCacheTest.php
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1515
use Symfony\Component\Cache\Simple\MemcachedCache;
16+
use Symfony\Component\Dsn\Factory\MemcachedConnectionFactory;
1617

1718
class MemcachedCacheTest extends CacheTestCase
1819
{
@@ -29,7 +30,7 @@ public static function setupBeforeClass()
2930
if (!MemcachedCache::isSupported()) {
3031
self::markTestSkipped('Extension memcached >=2.2.0 required.');
3132
}
32-
self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'));
33+
self::$client = MemcachedConnectionFactory::createConnection('memcached://'.getenv('MEMCACHED_HOST'));
3334
self::$client->get('foo');
3435
$code = self::$client->getResultCode();
3536

@@ -40,11 +41,25 @@ public static function setupBeforeClass()
4041

4142
public function createSimpleCache($defaultLifetime = 0)
4243
{
43-
$client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false)) : self::$client;
44+
$client = $defaultLifetime ? MemcachedConnectionFactory::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false)) : self::$client;
4445

4546
return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
4647
}
4748

49+
/**
50+
* @group legacy
51+
* @expectedDeprecation This "%s" method is deprecated.
52+
*/
53+
public function testCreateConnection()
54+
{
55+
$client = MemcachedCache::createConnection('memcached://localhost');
56+
57+
$this->assertInstanceOf(\Memcached::class, $client);
58+
}
59+
60+
/**
61+
* @group legacy
62+
*/
4863
public function testOptions()
4964
{
5065
$client = MemcachedCache::createConnection(array(), array(
@@ -63,6 +78,7 @@ public function testOptions()
6378
}
6479

6580
/**
81+
* @group legacy
6682
* @dataProvider provideBadOptions
6783
* @expectedException \ErrorException
6884
* @expectedExceptionMessage constant(): Couldn't find constant Memcached::
@@ -82,6 +98,9 @@ public function provideBadOptions()
8298
);
8399
}
84100

101+
/**
102+
* @group legacy
103+
*/
85104
public function testDefaultOptions()
86105
{
87106
$this->assertTrue(MemcachedCache::isSupported());
@@ -94,6 +113,7 @@ public function testDefaultOptions()
94113
}
95114

96115
/**
116+
* @group legacy
97117
* @expectedException \Symfony\Component\Cache\Exception\CacheException
98118
* @expectedExceptionMessage MemcachedAdapter: "serializer" option must be "php" or "igbinary".
99119
*/
@@ -107,6 +127,7 @@ public function testOptionSerializer()
107127
}
108128

109129
/**
130+
* @group legacy
110131
* @dataProvider provideServersSetting
111132
*/
112133
public function testServersSetting($dsn, $host, $port)

‎src/Symfony/Component/Cache/Tests/Simple/RedisCacheTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Simple/RedisCacheTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ public static function setupBeforeClass()
2121
self::$redis = RedisCache::createConnection('redis://'.getenv('REDIS_HOST'));
2222
}
2323

24+
/**
25+
* @group legacy
26+
* @expectedDeprecation This "%s" method is deprecated.
27+
*/
28+
public function testCreateConnectionDeprecated()
29+
{
30+
$client = RedisCache::createConnection('redis://'.getenv('REDIS_HOST'));
31+
32+
$this->assertInstanceOf(\Predis\Client, $client);
33+
}
34+
35+
/**
36+
* @group legacy
37+
*/
2438
public function testCreateConnection()
2539
{
2640
$redisHost = getenv('REDIS_HOST');
@@ -44,6 +58,7 @@ public function testCreateConnection()
4458
}
4559

4660
/**
61+
* @group legacy
4762
* @dataProvider provideFailedCreateConnection
4863
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
4964
* @expectedExceptionMessage Redis connection failed
@@ -63,6 +78,7 @@ public function provideFailedCreateConnection()
6378
}
6479

6580
/**
81+
* @group legacy
6682
* @dataProvider provideInvalidCreateConnection
6783
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
6884
* @expectedExceptionMessage Invalid Redis DSN

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.