8000 #37180 skim non-standard options from connection uri into options · kralos/symfony@558f9cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 558f9cc

Browse files
author
Joe Bennett
committed
symfony#37180 skim non-standard options from connection uri into options
1 parent 8bb0897 commit 558f9cc

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,20 @@ public function __construct($mongo, array $options = [], float $initialTtl = 300
104104
if ($mongo instanceof Collection) {
105105
$this->collection = $mongo;
106106
} elseif ($mongo instanceof Client) {
107-
if (null === $this->options['database']) {
108-
throw new InvalidArgumentException(sprintf('"%s()" requires the "database" option when constructing with a "%s".', __METHOD__, Client::class));
109-
}
110-
if (null === $this->options['collection']) {
111-
throw new InvalidArgumentException(sprintf('"%s()" requires the "collection" option when constructing with a "%s".', __METHOD__, Client::class));
112-
}
113-
114107
$this->client = $mongo;
115108
} elseif (\is_string($mongo)) {
116-
if (false === $parsedUrl = parse_url($mongo)) {
117-
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid.', $mongo));
118-
}
119-
$query = [];
120-
if (isset($parsedUrl['query'])) {
121-
parse_str($parsedUrl['query'], $query);
122-
}
123-
$this->options['collection'] = $this->options['collection'] ?? $query['collection'] ?? null;
124-
$this->options['database'] = $this->options['database'] ?? ltrim($parsedUrl['path'] ?? '', '/') ?: null;
109+
$this->uri = $this->skimUri($mongo);
110+
} else {
111+
throw new InvalidArgumentException(sprintf('"%s()" requires "%s" or "%s" or URI as first argument, "%s" given.', __METHOD__, Collection::class, Client::class, get_debug_type($mongo)));
112+
}
113+
114+
if (!($mongo instanceof Collection)) {
125115
if (null === $this->options['database']) {
126116
throw new InvalidArgumentException(sprintf('"%s()" requires the "database" in the URI path or option when constructing with a URI.', __METHOD__));
127117
}
128118
if (null === $this->options['collection']) {
129119
throw new InvalidArgumentException(sprintf('"%s()" requires the "collection" in the URI querystring or option when constructing with a URI.', __METHOD__));
130120
}
131-
132-
$this->uri = $mongo;
133-
} else {
134-
throw new InvalidArgumentException(sprintf('"%s()" requires "%s" or "%s" or URI as first argument, "%s" given.', __METHOD__, Collection::class, Client::class, get_debug_type($mongo)));
135121
}
136122

137123
if ($this->options['gcProbablity'] < 0.0 || $this->options['gcProbablity'] > 1.0) {
@@ -143,6 +129,42 @@ public function __construct($mongo, array $options = [], float $initialTtl = 300
143129
}
144130
}
145131

132+
/**
133+
* Extract default database and collection from given connection uri and remove collection querystring
134+
*
135+
*/
136+
private function skimUri(string $uri): string
137+
{
138+
if (false === $parsedUrl = parse_url($uri)) {
139+
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid.', $mongo));
140+
}
141+
142+
$query = [];
143+
if (isset($parsedUrl['query'])) {
144+
parse_str($parsedUrl['query'], $query);
145+
}
146+
147+
if (null === ($this->options['collection'] ?? null) && isset($query['collection'])) {
148+
$this->options['collection'] = $query['collection'] ?? null;
149+
$queryStringPos = strrpos($uri, $parsedUrl['query']);
150+
unset($query['collection']);
151+
$prefix = substr($uri, 0, $queryStringPos);
152+
$newQuery = http_build_query($query, '', '&', PHP_QUERY_RFC3986);
153+
if (empty($newQuery)) {
154+
$prefix = rtrim($prefix, '?');
155+
}
156+
$suffix = substr($uri, $queryStringPos + strlen($parsedUrl['query'])); 87B7
157+
$uri = $prefix . $newQuery . $suffix;
158+
}
159+
160+
$pathDb = ltrim($parsedUrl['path'] ?? '', '/') ?: null;
161+
if (null === ($this->options['database'] ?? null) && null !== $pathDb) {
162+
$this->options['database'] = $pathDb;
163+
}
164+
165+
return $uri;
166+
}
167+
146168
/**
147169
* Creates a TTL index to automatically remove expired locks.
148170
*

0 commit comments

Comments
 (0)
0