8000 #37180 deprecated extracting default database or collection from Mong… · kralos/symfony@845adfb · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 845adfb

Browse files
author
Joe Bennett
committed
symfony#37180 deprecated extracting default database or collection from MongoDB connection URI
1 parent d1b014a commit 845adfb

File tree

7 files changed

+93
-21
lines changed

7 files changed

+93
-21
lines changed

UPGRADE-5.2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 5.1 to 5.2
22
=======================
33

4+
Lock
5+
----
6+
7+
* Deprecated passing of `database` or `collection` to `MongoDbStore` via connection URI, use `$options` instead.
8+
49
Mime
510
----
611

UPGRADE-6.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ Inflector
7373

7474
* The component has been removed, use `EnglishInflector` from the String component instead.
7575

76+
Lock
77+
----
78+
79+
* `MongoDbStore` can no longer be constructed by passing `database` or `collection` via connection URI, use `$options` instead.
80+
7681
Mailer
7782
------
7883

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* deprecated passing of `database` or `collection` to `MongoDbStore` via connection URI, use `$options` instead.
8+
49
5.1.0
510
-----
611

@@ -19,10 +24,10 @@ CHANGELOG
1924
* added InvalidTtlException
2025
* deprecated `StoreInterface` in favor of `BlockingStoreInterface` and `PersistingStoreInterface`
2126
* `Factory` is deprecated, use `LockFactory` instead
22-
* `StoreFactory::createStore` allows PDO and Zookeeper DSN.
23-
* deprecated services `lock.store.flock`, `lock.store.semaphore`, `lock.store.memcached.abstract` and `lock.store.redis.abstract`,
27+
* `StoreFactory::createStore` allows PDO and Zookeeper DSN.
28+
* deprecated services `lock.store.flock`, `lock.store.semaphore`, `lock.store.memcached.abstract` and `lock.store.redis.abstract`,
2429
use `StoreFactory::createStore` instead.
25-
30+
2631
4.2.0
2732
-----
2833

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,12 @@ class MongoDbStore implements BlockingStoreInterface
6868
*
6969
* Options:
7070
* gcProbablity: Should a TTL Index be created expressed as a probability from 0.0 to 1.0 [default: 0.001]
71-
* database: The name of the database [required when $mongo is a Client]
72-
* collection: The name of the collection [required when $mongo is a Client]
71+
* database: The name of the database [required when $mongo is not a Collection]
72+
* collection: The name of the collection [required when $mongo is not a Collection]
7373
* uriOptions: Array of uri options. [used when $mongo is a URI]
7474
* driverOptions: Array of driver options. [used when $mongo is a URI]
7575
*
76-
* When using a URI string:
77-
* the database is determined from the "database" option, otherwise the uri's path is used.
78-
* the collection is determined from the "collection" option, otherwise the uri's "collection" querystring parameter is used.
79-
*
80-
* For example: mongodb://myuser:mypass@myhost/mydatabase?collection=mycollection
81-
*
82-
* @see https://docs.mongodb.com/php-library/current/reference/method/MongoDBClient__construct/
76+
* For uriOptions and driverOptions @see https://docs.mongodb.com/php-library/current/reference/method/MongoDBClient__construct/
8377
*
8478
* If gcProbablity is set to a value greater than 0.0 there is a chance
8579
* this store will attempt to create a TTL index on self::save().
@@ -89,6 +83,7 @@ class MongoDbStore implements BlockingStoreInterface
8983
*
9084
* writeConcern, readConcern and readPreference are not specified by
9185
* MongoDbStore meaning the collection's settings will take effect.
86+
*
9287
* @see https://docs.mongodb.com/manual/applications/replication/
9388
*/
9489
public function __construct($mongo, array $options = [], float $initialTtl = 300.0)
@@ -122,13 +117,21 @@ public function __construct($mongo, array $options = [], float $initialTtl = 300
122117
if (isset($parsedUrl['query'])) {
123118
parse_str($parsedUrl['query'], $query);
124119
}
120+
if (isset($query['collection'])) {
121+
trigger_deprecation('symfony/lock', '5.2', 'Constructing a "%s" by passing the "collection" via a connection URI is deprecated. Either contruct with a "%s" or pass it via $options instead.', __CLASS__, Collection::class);
122+
}
125123
$this->options['collection'] = $this->options['collection'] ?? $query['collection'] ?? null;
126-
$this->options['database'] = $this->options['database'] ?? ltrim($parsedUrl['path'] ?? '', '/') ?: null;
124+
$authSource = $this->options['uriOptions']['authSource'] ?? $query['authSource'] ?? null;
125+
$pathDb = ltrim($parsedUrl['path'] ?? '', '/') ?: null;
126+
if (null === $this->options['database'] && null !== $pathDb) {
127+
trigger_deprecation('symfony/lock', '5.2', 'Constructing a "%s" by passing the "database" via a connection URI is deprecated. Either contruct with a "%s" or pass it via $options instead.', __CLASS__, Collection::class);
128+
}
129+
$this->options['database'] = $this->options['database'] ?? $pathDb;
127130
if (null === $this->options['database']) {
128-
throw new InvalidArgumentException(sprintf('"%s()" requires the "database" in the URI path or option when constructing with a URI.', __METHOD__));
131+
throw new InvalidArgumentException(sprintf('"%s()" requires the "database" to be passed via $options or the URI path.', __METHOD__));
129132
}
130133
if (null === $this->options['collection']) {
131-
throw new InvalidArgumentException(sprintf('"%s()" requires the "collection" in the URI querystring or option when constructing with a URI.', __METHOD__));
134+
throw new InvalidArgumentException(sprintf('"%s()" requires the "collection" to be passed via $options or the URI querystring.', __METHOD__));
132135
}
133136

134137
$this->uri = $mongo;

src/Symfony/Component/Lock/Store/StoreFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public static function createStore($connection)
8181
return new $storeClass($connection);
8282

8383
case 0 === strpos($connection, 'mongodb'):
84+
trigger_deprecation('symfony/lock', '5.2', 'Using "%s" to construct a "%s" with a connection URI string is deprecated. Use a "%s" instead.', __CLASS__, MongoDbStore::class, Collection::class);
85+
8486
return new MongoDbStore($connection);
8587

8688
case 0 === strpos($connection, 'mssql://'):

src/Symfony/Component/Lock/Tests/Store/MongoDbStoreTest.php

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use MongoDB\Client;
1515
use MongoDB\Driver\Exception\ConnectionTimeoutException;
16+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1617
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1718
use Symfony\Component\Lock\Exception\NotSupportedException;
1819
use Symfony\Component\Lock\Key;
@@ -27,6 +28,7 @@
2728
*/
2829
class MongoDbStoreTest extends AbstractStoreTest
2930
{
31+
use ExpectDeprecationTrait;
3032
use ExpiringStoreTestTrait;
3133

3234
public static function setupBeforeClass(): void
@@ -97,10 +99,10 @@ public function testNonBlocking()
9799
*/
98100
public function testConstructionMethods($mongo, array $options)
99101
{
100-
$key = new Key(uniqid(__METHOD__, true));
101-
102102
$store = new MongoDbStore($mongo, $options);
103103

104+
$key = new Key(uniqid(__METHOD__, true));
105+
104106
$store->save($key);
105107
$this->assertTrue($store->exists($key));
106108

@@ -116,9 +118,57 @@ public function provideConstructorArgs()
116118
$collection = $client->selectCollection('test', 'lock');
117119
yield [$collection, []];
118120

119-
yield ['mongodb://localhost/test?collection=lock', []];
120-
yield ['mongodb://localhost/test', ['collection' => 'lock']];
121121
yield ['mongodb://localhost/', ['database' => 'test', 'collection' => 'lock']];
122+
yield ['mongodb://localhost/authDb', ['database' => 'test', 'collection' => 'lock']];
123+
}
124+
125+
/**
126+
* @dataProvider provideDeprecatedDatabaseConstructorArgs
127+
* @group legacy
128+
*/
129+
public function testDeprecatedDatabaseConstructionMethods($mongo, array $options)
130+
{
131+
$this->expectDeprecation('Since symfony/lock 5.2: Constructing a "Symfony\Component\Lock\Store\MongoDbStore" by passing the "database" via a connection URI is deprecated. Either contruct with a "MongoDB\Collection" or pass it via $options instead.');
132+
133+
$store = new MongoDbStore($mongo, $options);
134+
135+
$key = new Key(uniqid(__METHOD__, true));
136+
137+
$store->save($key);
138+
$this->assertTrue($store->exists($key));
139+
140+
$store->delete($key);
141+
$this->assertFalse($store->exists($key));
142+
}
143+
144+
public function provideDeprecatedDatabaseConstructorArgs()
145+
{
146+
yield ['mongodb://localhost/test', ['collection' => 'lock']];
147+
}
148+
149+
/**
150+
* @dataProvider provideDeprecatedCollectionConstructorArgs
151+
* @group legacy
152+
*/
153+
public function testDeprecatedCollectionConstructionMethods($mongo, array $options)
154+
{
155+
$this->expectDeprecation('Since symfony/lock 5.2: Constructing a "Symfony\Component\Lock\Store\MongoDbStore" by passing the "collection" via a connection URI is deprecated. Either contruct with a "MongoDB\Collection" or pass it via $options instead.');
156+
157+
$store = new MongoDbStore($mongo, $options);
158+
159+
$key = new Key(uniqid(__METHOD__, true));
160+
161+
$store->save($key);
162+
$this->assertTrue($store->exists($key));
163+
164+
$store->delete($key);
165+
$this->assertFalse($store->exists($key));
166+
}
167+
168+
public function provideDeprecatedCollectionConstructorArgs()
169+
{
170+
yield ['mongodb://localhost/?collection=lock', ['database' => 'test']];
171+
yield ['mongodb://localhost/?collection=lock', ['database' => 'test', 'collection' => 'lock']];
122172
}
123173

124174
/**
@@ -134,11 +184,12 @@ public function testInvalidConstructionMethods($mongo, array $options)
134184
public function provideInvalidConstructorArgs()
135185
{
136186
$client = self::getMongoClient();
137-
yield [$client, ['collection' => 'lock']];
187+
yield [$client, []];
138188
yield [$client, ['database' => 'test']];
189+
yield [$client, ['collection' => 'lock']];
139190

140-
yield ['mongodb://localhost/?collection=lock', []];
141191
yield ['mongodb://localhost/test', []];
192+
yield ['mongodb://localhost/?collection=lock', []];
142193
yield ['mongodb://localhost/', []];
143194
}
144195
}

src/Symfony/Component/Lock/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require": {
1919
"php": ">=7.2.5",
2020
"psr/log": "~1.0",
21+
"symfony/deprecation-contracts": "^2.1",
2122
"symfony/polyfill-php80": "^1.15"
2223
},
2324
"require-dev": {

0 commit comments

Comments
 (0)
0