diff --git a/SymfonyRuntime.php b/SymfonyRuntime.php index 0ca9713..5612b3e 100644 --- a/SymfonyRuntime.php +++ b/SymfonyRuntime.php @@ -95,7 +95,7 @@ public function __construct(array $options = []) if (isset($options['env'])) { $_SERVER[$envKey] = $options['env']; - } elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) { + } elseif (empty($_GET) && isset($_SERVER['argv']) && class_exists(ArgvInput::class)) { $this->options = $options; $this->getInput(); } @@ -216,6 +216,10 @@ protected static function register(GenericRuntime $runtime): GenericRuntime private function getInput(): ArgvInput { + if (!empty($_GET) && filter_var(ini_get('register_argc_argv'), \FILTER_VALIDATE_BOOL)) { + throw new \Exception('CLI applications cannot be run safely on non-CLI SAPIs with register_argc_argv=On.'); + } + if (null !== $this->input) { return $this->input; } diff --git a/Tests/phpt/kernel.php b/Tests/phpt/kernel.php index ba29d34..b7c43c5 100644 --- a/Tests/phpt/kernel.php +++ b/Tests/phpt/kernel.php @@ -17,19 +17,21 @@ class TestKernel implements HttpKernelInterface { + private $env; private $var; - public function __construct(string $var) + public function __construct(string $env, string $var) { + $this->env = $env; $this->var = $var; } public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true): Response { - return new Response('OK Kernel '.$this->var); + return new Response('OK Kernel (env='.$this->env.') '.$this->var); } } return function (array $context) { - return new TestKernel($context['SOME_VAR']); + return new TestKernel($context['APP_ENV'], $context['SOME_VAR']); }; diff --git a/Tests/phpt/kernel.phpt b/Tests/phpt/kernel.phpt index e739eb0..e7df91e 100644 --- a/Tests/phpt/kernel.phpt +++ b/Tests/phpt/kernel.phpt @@ -9,4 +9,4 @@ require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/kernel.php'; ?> --EXPECTF-- -OK Kernel foo_bar +OK Kernel (env=dev) foo_bar diff --git a/Tests/phpt/kernel_register_argc_argv.phpt b/Tests/phpt/kernel_register_argc_argv.phpt new file mode 100644 index 0000000..4da82d2 --- /dev/null +++ b/Tests/phpt/kernel_register_argc_argv.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test HttpKernelInterface with register_argc_argv=1 +--INI-- +display_errors=1 +register_argc_argv=1 +--FILE-- + +--EXPECTF-- +OK Kernel (env=dev) foo_bar