Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 03da3a2

Browse filesBrowse files
committed
Add a require env var processor
This allows to process .php files, returning the value returned from that file. Leverages the opcache.
1 parent 65b46a5 commit 03da3a2
Copy full SHA for 03da3a2

File tree

5 files changed

+39
-2
lines changed
Filter options

5 files changed

+39
-2
lines changed

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* added ability to define an index for services in an injected service locator argument
1616
* made `ServiceLocator` implement `ServiceProviderInterface`
1717
* deprecated support for non-string default env() parameters
18+
* added `%env(require:...)%` processor to `require()` a PHP file and use the value returned from it
1819

1920
4.2.0
2021
-----

‎src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static function getProvidedTypes()
4747
'default' => 'bool|int|float|string|array',
4848
'string' => 'string',
4949
'trim' => 'string',
50+
'require' => 'bool|int|float|string|array',
5051
];
5152
}
5253

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

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

113-
return file_get_contents($file);
114+
if ('file' === $prefix) {
115+
return file_get_contents($file);
116+
} else {
117+
return require $file;
118+
}
114119
}
115120

116121
if (false !== $i || 'string' !== $prefix) {

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function testSimpleProcessor()
4545
'default' => ['bool', 'int', 'float', 'string', 'array'],
4646
'string' => ['string'],
4747
'trim' => ['string'],
48+
'require' => ['bool', 'int', 'float', 'string', 'array'],
4849
];
4950

5051
$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,32 @@ public function validNullables()
458458
['NULL', 'NULL'],
459459
];
460460
}
461+
462+
/**
463+
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvNotFoundException
464+
* @expectedExceptionMessage missing-file
465+
*/
466+
public function testRequireMissingFile()
467+
{
468+
$processor = new EnvVarProcessor(new Container());
469+
470+
$processor->getEnv('require', '/missing-file', function ($name) {
471+
return $name;
472+
});
473+
}
474+
475+
public function testRequireFile()
476+
{
477+
$path = __DIR__.'/Fixtures/php/return_foo_string.php';
478+
479+
$processor = new EnvVarProcessor(new Container());
480+
481+
$result = $processor->getEnv('require', $path, function ($name) use ($path) {
482+
$this->assertSame($path, $name);
483+
484+
return $path;
485+
});
486+
487+
$this->assertEquals('foo', $result);
488+
}
461489
}
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
return 'foo';

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.