Skip to content

Commit

Permalink
symfony#37180 deprecated extracting default database or collection fr…
Browse files Browse the repository at this point in the history
…om MongoDB connection URI
  • Loading branch information
Joe Bennett committed Jun 10, 2020
1 parent d1b014a commit 1caf226
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Symfony/Component/Lock/Store/MongoDbStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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://'):
Expand Down

0 comments on commit 1caf226

Please sign in to comment.