From 593f69d0c10d2dbea2abc0f868065960bd7e0fd4 Mon Sep 17 00:00:00 2001 From: peets Date: Sun, 14 Jan 2018 15:53:07 -0500 Subject: [PATCH 1/3] Ignore phar.readonly directive when compiling phar --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d79857a..9dad973 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ vendor: composer install twigc.phar: vendor - php bin/compile + php -d phar.readonly=0 bin/compile build: twigc.phar From dfcf0349ca3af21ed41e096586c24efd4d66a580 Mon Sep 17 00:00:00 2001 From: peets Date: Sun, 14 Jan 2018 16:16:26 -0500 Subject: [PATCH 2/3] 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) ) { From 281b50438a3d74b4b8b47e815030410f80ae67c0 Mon Sep 17 00:00:00 2001 From: peets Date: Sun, 14 Jan 2018 16:52:30 -0500 Subject: [PATCH 3/3] Add json escape strategy --- src/Twigc/DefaultCommand.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Twigc/DefaultCommand.php b/src/Twigc/DefaultCommand.php index 22cc227..cfe3bb3 100644 --- a/src/Twigc/DefaultCommand.php +++ b/src/Twigc/DefaultCommand.php @@ -260,6 +260,9 @@ class DefaultCommand extends Command { case 'js': $escape = 'js'; break; + case 'json': + $escape = 'json'; + break; default: $escape = false; break; @@ -409,6 +412,16 @@ class DefaultCommand extends Command { 'autoescape' => $escape, ]); + $twig->getExtension('core')->setEscaper( + 'json', + function($twigEnv, $string, $charset) { + return json_encode( + $string, + \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE + ); + } + ); + $output->writeln( rtrim($twig->render(basename($template), $inputData), "\r\n") );