Skip to content

Commit 6ac5f4f

Browse files
committed
[DependencyInjection] Fix query_string env processor for URLs without query string
1 parent 83617f6 commit 6ac5f4f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,15 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
325325
}
326326

327327
if ('query_string' === $prefix) {
328-
$queryString = parse_url($env, \PHP_URL_QUERY) ?: $env;
328+
$queryString = parse_url($env, \PHP_URL_QUERY);
329+
330+
if (null === $queryString) {
331+
if (null !== parse_url($env, \PHP_URL_SCHEME)) {
332+
return [];
333+
}
334+
$queryString = $env;
335+
}
336+
329337
parse_str($queryString, $result);
330338

331339
return $result;

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,27 @@ public static function provideGetEnvDefined(): iterable
982982
yield 'Null' => [false, fn () => null];
983983
yield 'Env var not defined' => [false, fn () => throw new EnvNotFoundException()];
984984
}
985+
986+
/**
987+
* @dataProvider provideQueryStringScenarios
988+
*/
989+
public function testQueryStringEnvVarProcessor($envValue, $expectedResult)
990+
{
991+
$processor = new EnvVarProcessor(new Container());
992+
993+
$result = $processor->getEnv('query_string', 'MY_VAR', fn () => $envValue);
994+
995+
$this->assertSame($expectedResult, $result);
996+
}
997+
998+
public static function provideQueryStringScenarios(): iterable
999+
{
1000+
yield 'url_without_query' => ['https://example.com', []];
1001+
1002+
yield 'url_with_empty_query' => ['https://example.com?', []];
1003+
1004+
yield 'url_with_query' => ['https://example.com?foo=bar&baz=123', ['foo' => 'bar', 'baz' => '123']];
1005+
1006+
yield 'raw_query_string' => ['foo=bar&test=1', ['foo' => 'bar', 'test' => '1']];
1007+
}
9851008
}

0 commit comments

Comments
 (0)