Support using environment variables as input data

This uses the $_ENV superglobal, which is an empty array unless the
variables_order ini directive contains "E"; see
http://php.net/manual/en/ini.core.php#ini.variables-order.

Environment variables have lower precedence than input data passed via
other means, e.g.

    $ echo '{{USER}}' | twigc --env
    peets
    $ echo '{{USER}}' | twigc --env -p USER=foo
    foo
This commit is contained in:
peets 2018-01-14 16:16:26 -05:00
parent 593f69d0c1
commit dfcf0349ca
2 changed files with 11 additions and 0 deletions

View file

@ -19,6 +19,7 @@ Options:
-V, --version Display version information
--credits Display dependency credits (including Twig version)
-d, --dir=DIR Add search directory to loader (multiple values allowed)
--env Treat environment variables as input data
-e, --escape=ESCAPE Set autoescape environment option
-j, --json=JSON Pass variables as JSON (dictionary string or file path)
-p, --pair=PAIR Pass variable as key=value pair (multiple values allowed)

View file

@ -63,6 +63,12 @@ class DefaultCommand extends Command {
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Add search directory to loader'
)
->addOption(
'env',
null,
InputOption::VALUE_NONE,
'Treat environment variables as input data'
)
->addOption(
'escape',
'e',
@ -349,6 +355,10 @@ class DefaultCommand extends Command {
}
}
if ( $input->getOption('env') ) {
$inputData = array_merge($_ENV, $inputData);
}
// Validate key names now
foreach ( $inputData as $key => $value ) {
if ( ! preg_match('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $key) ) {