Skip to content

Commit 7592980

Browse files
committed
feature #35029 [DI] allow "." and "-" in env processor lines (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [DI] allow "." and "-" in env processor lines | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #34864 | License | MIT | Doc PR | - As explained in the linked issue, this is especially usefull with the `key` processor. Commits ------- 231c505a47 [DI] allow "." and "-" in env processor lines
2 parents e5b9248 + 70afca9 commit 7592980

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

ParameterBag/EnvPlaceholderParameterBag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public function get(string $name)
4444
return $placeholder; // return first result
4545
}
4646
}
47-
if (!preg_match('/^(?:\w*+:)*+\w++$/', $env)) {
47+
if (!preg_match('/^(?:[-.\w]*+:)*+\w++$/', $env)) {
4848
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
4949
}
5050
if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) {
5151
throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
5252
}
5353

5454
$uniqueName = md5($name.'_'.self::$counter++);
55-
$placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), str_replace(':', '_', $env), $uniqueName);
55+
$placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), strtr($env, ':-.', '___'), $uniqueName);
5656
$this->envPlaceholders[$env][$placeholder] = $placeholder;
5757

5858
return $placeholder;

Tests/ParameterBag/EnvPlaceholderParameterBagTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,11 @@ public function testDefaultToNullAllowed()
187187
$bag->resolve();
188188
$this->assertNotNull($bag->get('env(default::BAR)'));
189189
}
190+
191+
public function testExtraCharsInProcessor()
192+
{
193+
$bag = new EnvPlaceholderParameterBag();
194+
$bag->resolve();
195+
$this->assertStringMatchesFormat('env_%s_key_a_b_c_FOO_%s', $bag->get('env(key:a.b-c:FOO)'));
196+
}
190197
}

0 commit comments

Comments
 (0)