diff --git a/src/Symfony/Component/Lock/Store/MongoDbStore.php b/src/Symfony/Component/Lock/Store/MongoDbStore.php index 296c68be1072a..93cb49dfcb37c 100644 --- a/src/Symfony/Component/Lock/Store/MongoDbStore.php +++ b/src/Symfony/Component/Lock/Store/MongoDbStore.php @@ -68,18 +68,12 @@ class MongoDbStore implements BlockingStoreInterface * * Options: * gcProbablity: Should a TTL Index be created expressed as a probability from 0.0 to 1.0 [default: 0.001] - * database: The name of the database [required when $mongo is a Client] - * collection: The name of the collection [required when $mongo is a Client] + * database: The name of the database [required when $mongo is not a Collection] + * collection: The name of the collection [required when $mongo is not a Collection] * uriOptions: Array of uri options. [used when $mongo is a URI] * driverOptions: Array of driver options. [used when $mongo is a URI] * - * When using a URI string: - * the database is determined from the "database" option, otherwise the uri's path is used. - * the collection is determined from the "collection" option, otherwise the uri's "collection" querystring parameter is used. - * - * For example: mongodb://myuser:mypass@myhost/mydatabase?collection=mycollection - * - * @see https://docs.mongodb.com/php-library/current/reference/method/MongoDBClient__construct/ + * For uriOptions and driverOptions @see https://docs.mongodb.com/php-library/current/reference/method/MongoDBClient__construct/ * * If gcProbablity is set to a value greater than 0.0 there is a chance * this store will attempt to create a TTL index on self::save(). @@ -122,13 +116,21 @@ public function __construct($mongo, array $options = [], float $initialTtl = 300 if (isset($parsedUrl['query'])) { parse_str($parsedUrl['query'], $query); } + if (isset($query['collection'])) { + 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); + } $this->options['collection'] = $this->options['collection'] ?? $query['collection'] ?? null; - $this->options['database'] = $this->options['database'] ?? ltrim($parsedUrl['path'] ?? '', '/') ?: null; + $authSource = $this->options['uriOptions']['authSource'] ?? $query['authSource'] ?? null; + $pathDb = ltrim($parsedUrl['path'] ?? '', '/') ?: null; + if (null === $this->options['database'] && null !== $pathDb) { + 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); + } + $this->options['database'] = $this->options['database'] ?? $pathDb; if (null === $this->options['database']) { - throw new InvalidArgumentException(sprintf('"%s()" requires the "database" in the URI path or option when constructing with a URI.', __METHOD__)); + throw new InvalidArgumentException(sprintf('"%s()" requires the "database" to be passed via $options or the URI path.', __METHOD__)); } if (null === $this->options['collection']) { - throw new InvalidArgumentException(sprintf('"%s()" requires the "collection" in the URI querystring or option when constructing with a URI.', __METHOD__)); + throw new InvalidArgumentException(sprintf('"%s()" requires the "collection" to be passed via $options or the URI querystring.', __METHOD__)); } $this->uri = $mongo; diff --git a/src/Symfony/Component/Lock/Store/StoreFactory.php b/src/Symfony/Component/Lock/Store/StoreFactory.php index 69b53c792eaef..abba680a4e03b 100644 --- a/src/Symfony/Component/Lock/Store/StoreFactory.php +++ b/src/Symfony/Component/Lock/Store/StoreFactory.php @@ -81,6 +81,7 @@ public static function createStore($connection) return new $storeClass($connection); case 0 === strpos($connection, 'mongodb'): + 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); return new MongoDbStore($connection); case 0 === strpos($connection, 'mssql://'):