8000 [DIC] Add a `require` env var processor by mpdude · Pull Request #30897 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DIC] Add a require env var processor #30897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CHANGELOG
* added ability to define an index for services in an injected service locator argument
* made `ServiceLocator` implement `ServiceProviderInterface`
* deprecated support for non-string default env() parameters
* added `%env(require:...)%` processor to `require()` a PHP file and use the value returned from it

4.2.0
-----
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function getProvidedTypes()
'default' => 'bool|int|float|string|array',
'string' => 'string',
'trim' => 'string',
'require' => 'bool|int|float|string|array',
];
}

Expand Down Expand Up @@ -102,15 +103,19 @@ public function getEnv($prefix, $name, \Closure $getEnv)
return '' === $default ? null : $this->container->getParameter($default);
}

if ('file' === $prefix) {
if ('file' === $prefix || 'require' === $prefix) {
if (!is_scalar($file = $getEnv($name))) {
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
}
if (!file_exists($file)) {
throw new EnvNotFoundException(sprintf('File "%s" not found (resolved from "%s").', $file, $name));
}

return file_get_contents($file);
if ('file' === $prefix) {
return file_get_contents($file);
} else {
return require $file;
}
}

if (false !== $i || 'string' !== $prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testSimpleProcessor()
'default' => ['bool', 'int', 'float', 'string', 'array'],
'string' => ['string'],
'trim' => ['string'],
'require' => ['bool', 'int', 'float', 'string', 'array'],
];

$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,32 @@ public function validNullables()
['NULL', 'NULL'],
];
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvNotFoundException
* @expectedExceptionMessage missing-file
*/
public function testRequireMissingFile()
{
$processor = new EnvVarProcessor(new Container());

$processor->getEnv('require', '/missing-file', function ($name) {
return $name;
});
}

public function testRequireFile()
{
$path = __DIR__.'/Fixtures/php/return_foo_string.php';

$processor = new EnvVarProcessor(new Container());

$result = $processor->getEnv('require', $path, function ($name) use ($path) {
$this->assertSame($path, $name);

return $path;
});

$this->assertEquals('foo', $result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
return 'foo';
0