mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENH Allow multiple backtick variables in a single value
This commit is contained in:
parent
baaa323644
commit
6a779d07d0
@ -58,13 +58,16 @@ Environment::setEnv('API_KEY', 'AABBCCDDEEFF012345');
|
|||||||
|
|
||||||
### Using environment variables in config
|
### Using environment variables in config
|
||||||
|
|
||||||
To use environment variables in `.yaml` configs you can reference them using backticks.
|
To use environment variables in `.yaml` configs you can reference them using backticks. You can have multiple
|
||||||
|
environment variables within a single value, though the overall value must start and end with backticks.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
SilverStripe\Core\Injector\Injector:
|
SilverStripe\Core\Injector\Injector:
|
||||||
MyServiceClass:
|
MyServiceClass:
|
||||||
properties:
|
properties:
|
||||||
MyProperty: '`ENV_VAR_HERE`'
|
MyProperty: '`ENV_VAR_ONE`'
|
||||||
|
MultiValueProperty: '`ENV_VAR_ONE`:`ENV_VAR_TWO`'
|
||||||
|
ThisWillNotSubstitute: 'lorem `REGULAR_TEXT` ipsum'
|
||||||
```
|
```
|
||||||
|
|
||||||
[info]
|
[info]
|
||||||
|
@ -524,16 +524,31 @@ class Injector implements ContainerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate constants surrounded by back ticks
|
// Evaluate constants surrounded by back ticks
|
||||||
if (preg_match('/^`(?<name>[^`]+)`$/', $value ?? '', $matches)) {
|
$hasBacticks = false;
|
||||||
$envValue = Environment::getEnv($matches['name']);
|
$allMissing = true;
|
||||||
if ($envValue !== false) {
|
// $value must start and end with backticks, though there can be multiple
|
||||||
$value = $envValue;
|
// things being subsituted within $value e.g. "`VAR_ONE`:`VAR_TWO`:`VAR_THREE`"
|
||||||
} elseif (defined($matches['name'] ?? '')) {
|
if (preg_match('/^`.+`$/', $value ?? '')) {
|
||||||
$value = constant($matches['name'] ?? '');
|
$hasBacticks = true;
|
||||||
} else {
|
preg_match_all('/`(?<name>[^`]+)`/', $value, $matches);
|
||||||
$value = null;
|
foreach ($matches['name'] as $name) {
|
||||||
|
$envValue = Environment::getEnv($name);
|
||||||
|
$val = '';
|
||||||
|
if ($envValue !== false) {
|
||||||
|
$val = $envValue;
|
||||||
|
} elseif (defined($name)) {
|
||||||
|
$val = constant($name);
|
||||||
|
}
|
||||||
|
$value = str_replace("`$name`", $val, $value);
|
||||||
|
if ($val) {
|
||||||
|
$allMissing = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// silverstripe sometimes explictly expects a null value rather than just an empty string
|
||||||
|
if ($hasBacticks && $allMissing && $value === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\Core\Tests\Injector;
|
namespace SilverStripe\Core\Tests\Injector;
|
||||||
|
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\Core\Environment;
|
||||||
use SilverStripe\Core\Injector\Factory;
|
use SilverStripe\Core\Injector\Factory;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\Core\Injector\InjectorNotFoundException;
|
use SilverStripe\Core\Injector\InjectorNotFoundException;
|
||||||
@ -815,6 +816,32 @@ class InjectorTest extends SapphireTest
|
|||||||
$this->assertInstanceOf(OtherTestObject::class, $item->property->property);
|
$this->assertInstanceOf(OtherTestObject::class, $item->property->property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideConvertServicePropertyBackTicks
|
||||||
|
*/
|
||||||
|
public function testConvertServicePropertyBackTicks($value, $expected)
|
||||||
|
{
|
||||||
|
Environment::setEnv('INJECTOR_TEST_CSP_A', 'ABC');
|
||||||
|
Environment::setEnv('INJECTOR_TEST_CSP_B', 'DEF');
|
||||||
|
Environment::setEnv('INJECTOR_TEST_CSP_C', 'GHI');
|
||||||
|
$actual = Injector::inst()->convertServiceProperty($value);
|
||||||
|
$this->assertSame($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideConvertServicePropertyBackTicks()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['`INJECTOR_TEST_CSP_A`', 'ABC'],
|
||||||
|
['`INJECTOR_TEST_CSP_A`:`INJECTOR_TEST_CSP_B`', 'ABC:DEF'],
|
||||||
|
['`INJECTOR_TEST_CSP_A` some text `INJECTOR_TEST_CSP_B`', 'ABC some text DEF'],
|
||||||
|
['`INJECTOR_TEST_CSP_A``INJECTOR_TEST_CSP_B`', 'ABCDEF'],
|
||||||
|
['`INJECTOR_TEST_CSP_A`:`INJECTOR_TEST_CSP_B``INJECTOR_TEST_CSP_C`', 'ABC:DEFGHI'],
|
||||||
|
['`INJECTOR_TEST_CSP_A`:`INJECTOR_TEST_CSP_X`', 'ABC:'],
|
||||||
|
['`INJECTOR_TEST_CSP_X`', null],
|
||||||
|
['lorem `INJECTOR_TEST_CSP_A` ipsum', 'lorem `INJECTOR_TEST_CSP_A` ipsum'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function testNamedServices()
|
public function testNamedServices()
|
||||||
{
|
{
|
||||||
$injector = new Injector();
|
$injector = new Injector();
|
||||||
|
Loading…
Reference in New Issue
Block a user