8000 Merge branch '5.1' · jeremyFreeAgent/symfony@e191750 · GitHub
[go: up one dir, main page]

Skip to content

Commit e191750

Browse files
committed
Merge branch '5.1'
* 5.1: [Lock] MongoDbStore handle duplicate querystring keys in mongodb uri when stripping [PropertyInfo] Fix ReflectionExtractor + minor tweaks
2 parents 27d84db + 679cc4d commit e191750

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,21 @@ private function skimUri(string $uri): string
141141
if (false === $parsedUrl = parse_url($uri)) {
142142
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid.', $uri));
143143
}
144-
145-
$query = [];
146-
if (isset($parsedUrl['query'])) {
147-
parse_str($parsedUrl['query'], $query);
148-
}
149-
150-
if (isset($query['collection'])) {
151-
$this->options['collection'] = $query['collection'];
152-
$queryStringPos = strrpos($uri, $parsedUrl['query']);
153-
unset($query['collection']);
154-
$prefix = substr($uri, 0, $queryStringPos);
155-
$newQuery = http_build_query($query, '', '&', PHP_QUERY_RFC3986);
156-
if (empty($newQuery)) {
157-
$prefix = rtrim($prefix, '?');
158-
}
159-
$suffix = substr($uri, $queryStringPos + \strlen($parsedUrl['query']));
160-
$uri = $prefix.$newQuery.$suffix;
161-
}
162-
163144
$pathDb = ltrim($parsedUrl['path'] ?? '', '/') ?: null;
164145
if (null !== $pathDb) {
165146
$this->options['database'] = $pathDb;
166147
}
167148

149+
$matches = [];
150+
if (preg_match('/^(.*[\?&])collection=([^&#]*)&?(([^#]*).*)$/', $uri, $matches)) {
151+
$prefix = $matches[1];
152+
$this->options['collection'] = $matches[2];
153+
if (empty($matches[4])) {
154+
$prefix = substr($prefix, 0, -1);
155+
}
156+
$uri = $prefix.$matches[3];
157+
}
158+
168159
return $uri;
169160
}
170161

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,12 @@ public function provideUriCollectionStripArgs()
187187
yield ['mongodb://localhost/?replicaSet=repl', ['database' => 'test', 'collection' => 'lock'], 'mongodb://localhost/?replicaSet=repl'];
188188
yield ['mongodb://localhost/test?collection=lock&replicaSet=repl', [], 'mongodb://localhost/test?replicaSet=repl'];
189189
yield ['mongodb://localhost/test?replicaSet=repl', ['collection' => 'lock'], 'mongodb://localhost/test?replicaSet=repl'];
190+
191+
yield ['mongodb://localhost/test?readPreferenceTags=dc:foo&collection=lock&readPreferenceTags=dc:bar', [], 'mongodb://localhost/test?readPreferenceTags=dc:foo&readPreferenceTags=dc:bar'];
192+
yield ['mongodb://localhost?foo_collection=x&collection=lock&bar_collection=x#collection=x', ['database' => 'test'], 'mongodb://localhost?foo_collection=x&bar_collection=x#collection=x'];
193+
yield ['mongodb://localhost?collection=lock&foo_collection=x&bar_collection=x#collection=x', ['database' => 'test'], 'mongodb://localhost?foo_collection=x&bar_collection=x#collection=x'];
194+
yield ['mongodb://localhost?foo_collection=x&bar_collection=x&collection=lock#collection=x', ['database' => 'test'], 'mongodb://localhost?foo_collection=x&bar_collection=x#collection=x'];
195+
yield ['mongodb://user:?collection=a@localhost?collection=lock', ['database' => 'test'], 'mongodb://user:?collection=a@localhost'];
196+
yield ['mongodb://user:&collection=a@localhost/?collection=lock', ['database' => 'test'], 'mongodb://user:&collection=a@localhost/'];
190197
}
191198
}

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function getReadInfo(string $class, string $property, array $context = []
272272
/**
273273
* {@inheritdoc}
274274
*/
275-
public function getWriteInfo(string $class, string $property, array $context = []): PropertyWriteInfo
275+
public function getWriteInfo(string $class, string $property, array $context = []): ?PropertyWriteInfo
276276
{
277277
try {
278278
$reflClass = new \ReflectionClass($class);
@@ -308,10 +308,10 @@ public function getWriteInfo(string $class, string $property, array $context = [
308308
$mutator->setRemoverInfo(new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $removerAccessName, $this->getWriteVisiblityForMethod($removerMethod), $removerMethod->isStatic()));
309309

310310
return $mutator;
311-
} else {
312-
$errors = array_merge($errors, $adderAndRemoverErrors);
313311
}
314312

313+
$errors = array_merge($errors, $adderAndRemoverErrors);
314+
315315
foreach ($this->mutatorPrefixes as $mutatorPrefix) {
316316
$methodName = $mutatorPrefix.$camelized;
317317

@@ -330,12 +330,14 @@ public function getWriteInfo(string $class, string $property, array $context = [
330330

331331
$getsetter = lcfirst($camelized);
332332

333-
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, $getsetter, 1);
334-
if ($allowGetterSetter && $accessible) {
335-
$method = $reflClass->getMethod($getsetter);
333+
if ($allowGetterSetter) {
334+
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, $getsetter, 1);
335+
if ($accessible) {
336+
$method = $reflClass->getMethod($getsetter);
337+
338+
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $getsetter, $this->getWriteVisiblityForMethod($method), $method->isStatic());
339+
}
336340

337-
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $getsetter, $this->getWriteVisiblityForMethod($method), $method->isStatic());
338-
} else {
339341
$errors = array_merge($errors, $methodAccessibleErrors);
340342
}
341343

@@ -348,25 +350,27 @@ public function getWriteInfo(string $class, string $property, array $context = [
348350
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__set', 2);
349351
if ($accessible) {
350352
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
351-
} else {
352-
$errors = array_merge($errors, $methodAccessibleErrors);
353353
}
354354

355-
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__call', 2);
356-
if ($allowMagicCall && $accessible) {
357-
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, 'set'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
358-
} else {
355+
$errors = array_merge($errors, $methodAccessibleErrors);
356+
357+
if ($allowMagicCall) {
358+
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__call', 2);
359+
if ($accessible) {
360+
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, 'set'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
361+
}
362+
359363
$errors = array_merge($errors, $methodAccessibleErrors);
360364
}
361365

362366
if (!$allowAdderRemover && null !== $adderAccessName && null !== $removerAccessName) {
363-
$errors = array_merge($errors, [sprintf(
367+
$errors[] = sprintf(
364368
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
365369
'the new value must be an array or an instance of \Traversable',
366370
$property,
367371
$reflClass->getName(),
368372
implode('()", "', [$adderAccessName, $removerAccessName])
369-
)]);
373+
);
370374
}
371375

372376
$noneProperty = new PropertyWriteInfo();
@@ -626,7 +630,7 @@ private function getPropertyName(string $methodName, array $reflectionProperties
626630
private function findAdderAndRemover(\ReflectionClass $reflClass, array $singulars): array
627631
{
628632
if (!\is_array($this->arrayMutatorPrefixes) && 2 !== \count($this->arrayMutatorPrefixes)) {
629-
return null;
633+
return [null, null, []];
630634
}
631635

632636
[$addPrefix, $removePrefix] = $this->arrayMutatorPrefixes;
@@ -642,7 +646,9 @@ private function findAdderAndRemover(\ReflectionClass $reflClass, array $singula
642646

643647
if ($addMethodFound && $removeMethodFound) {
644648
return [$addMethod, $removeMethod, []];
645-
} elseif ($addMethodFound && !$removeMethodFound) {
649+
}
650+
651+
if ($addMethodFound && !$removeMethodFound) {
646652
$errors[] = sprintf('The add method "%s" in class "%s" was found, but the corresponding remove method "%s" was not found', $addMethod, $reflClass->getName(), $removeMethod);
647653
} elseif (!$addMethodFound && $removeMethodFound) {
648654
$errors[] = sprintf('The remove method "%s" in class "%s" was found, but the corresponding add method "%s" was not found', $removeMethod, $reflClass->getName(), $addMethod);

0 commit comments

Comments
 (0)
0