From dfcf0349ca3af21ed41e096586c24efd4d66a580 Mon Sep 17 00:00:00 2001 From: peets Date: Sun, 14 Jan 2018 16:16:26 -0500 Subject: [PATCH] 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 --- README.md | 1 + src/Twigc/DefaultCommand.php | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index b93173e..36264b7 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Twigc/DefaultCommand.php b/src/Twigc/DefaultCommand.php index ce3d20d..22cc227 100644 --- a/src/Twigc/DefaultCommand.php +++ b/src/Twigc/DefaultCommand.php @@ -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) ) {