From b3ee8b71dfefcd6ee8c37cc73a36500b6f11883b Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Fri, 10 Jan 2025 17:38:20 +0100 Subject: [PATCH 1/3] chore: PHP CS Fixer fixes --- Tests/phpt/kernel.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Tests/phpt/kernel.php b/Tests/phpt/kernel.php index f664967..7326849 100644 --- a/Tests/phpt/kernel.php +++ b/Tests/phpt/kernel.php @@ -15,8 +15,7 @@ require __DIR__.'/autoload.php'; -class TestKernel implements HttpKernelInterface -{ +return fn (array $context) => new class($context['APP_ENV'], $context['SOME_VAR']) implements HttpKernelInterface { private string $env; private string $var; @@ -30,6 +29,4 @@ public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = tr { return new Response('OK Kernel (env='.$this->env.') '.$this->var); } -} - -return fn (array $context) => new TestKernel($context['APP_ENV'], $context['SOME_VAR']); +}; From b495af904d1a9e56f4b1b6f2730460e4122ae68d Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 28 Mar 2025 09:22:25 -0400 Subject: [PATCH 2/3] Deprecate returning a non-integer value from a `\Closure` function set via `Command::setCode()` --- Tests/phpt/application.php | 4 +++- Tests/phpt/command.php | 4 +++- Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php | 4 +++- Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php | 4 +++- Tests/phpt/dotenv_overload_command_env_exists.php | 4 +++- Tests/phpt/dotenv_overload_command_no_debug.php | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Tests/phpt/application.php b/Tests/phpt/application.php index 1e1014e..ca2de55 100644 --- a/Tests/phpt/application.php +++ b/Tests/phpt/application.php @@ -18,8 +18,10 @@ return function (array $context) { $command = new Command('go'); - $command->setCode(function (InputInterface $input, OutputInterface $output) use ($context) { + $command->setCode(function (InputInterface $input, OutputInterface $output) use ($context): int { $output->write('OK Application '.$context['SOME_VAR']); + + return 0; }); $app = new Application(); diff --git a/Tests/phpt/command.php b/Tests/phpt/command.php index 3a5fa11..e307d19 100644 --- a/Tests/phpt/command.php +++ b/Tests/phpt/command.php @@ -19,7 +19,9 @@ return function (Command $command, InputInterface $input, OutputInterface $output, array $context) { $command->addOption('hello', 'e', InputOption::VALUE_REQUIRED, 'How should I greet?', 'OK'); - return $command->setCode(function () use ($input, $output, $context) { + return $command->setCode(function () use ($input, $output, $context): int { $output->write($input->getOption('hello').' Command '.$context['SOME_VAR']); + + return 0; }); }; diff --git a/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php b/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php index af6409d..2968e37 100644 --- a/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php +++ b/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php @@ -20,6 +20,8 @@ require __DIR__.'/autoload.php'; -return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): void { +return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): int { $output->writeln($context['DEBUG_ENABLED']); + + return 0; }); diff --git a/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php b/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php index 78a0bf2..1f2fa35 100644 --- a/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php +++ b/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php @@ -20,6 +20,8 @@ require __DIR__.'/autoload.php'; -return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): void { +return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): int { $output->writeln($context['DEBUG_MODE']); + + return 0; }); diff --git a/Tests/phpt/dotenv_overload_command_env_exists.php b/Tests/phpt/dotenv_overload_command_env_exists.php index 3e72372..8587f20 100644 --- a/Tests/phpt/dotenv_overload_command_env_exists.php +++ b/Tests/phpt/dotenv_overload_command_env_exists.php @@ -20,6 +20,8 @@ require __DIR__.'/autoload.php'; -return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): void { +return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): int { $output->writeln($context['ENV_MODE']); + + return 0; }); diff --git a/Tests/phpt/dotenv_overload_command_no_debug.php b/Tests/phpt/dotenv_overload_command_no_debug.php index 3fe4f44..4ab7694 100644 --- a/Tests/phpt/dotenv_overload_command_no_debug.php +++ b/Tests/phpt/dotenv_overload_command_no_debug.php @@ -19,6 +19,8 @@ require __DIR__.'/autoload.php'; -return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): void { +return static fn (Command $command, OutputInterface $output, array $context): Command => $command->setCode(static function () use ($output, $context): int { $output->writeln($context['DEBUG_ENABLED']); + + return 0; }); From fda552ee63dce9f3365f9c397efe7a80c8abac0a Mon Sep 17 00:00:00 2001 From: nathanpage Date: Fri, 4 Apr 2025 16:43:58 +1100 Subject: [PATCH 3/3] [Runtime] Support extra dot-env files --- SymfonyRuntime.php | 20 ++++++++++++++++---- Tests/phpt/.env.extra | 1 + Tests/phpt/dotenv_extra_load.php | 25 +++++++++++++++++++++++++ Tests/phpt/dotenv_extra_load.phpt | 12 ++++++++++++ Tests/phpt/dotenv_extra_overload.php | 25 +++++++++++++++++++++++++ Tests/phpt/dotenv_extra_overload.phpt | 12 ++++++++++++ 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 Tests/phpt/.env.extra create mode 100644 Tests/phpt/dotenv_extra_load.php create mode 100644 Tests/phpt/dotenv_extra_load.phpt create mode 100644 Tests/phpt/dotenv_extra_overload.php create mode 100644 Tests/phpt/dotenv_extra_overload.phpt diff --git a/SymfonyRuntime.php b/SymfonyRuntime.php index c66035f..4035f28 100644 --- a/SymfonyRuntime.php +++ b/SymfonyRuntime.php @@ -41,6 +41,7 @@ class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || clas * - "test_envs" to define the names of the test envs - defaults to ["test"]; * - "use_putenv" to tell Dotenv to set env vars using putenv() (NOT RECOMMENDED.) * - "dotenv_overload" to tell Dotenv to override existing vars + * - "dotenv_extra_paths" to define a list of additional dot-env files * * When the "debug" / "env" options are not defined, they will fallback to the * "APP_DEBUG" / "APP_ENV" environment variables, and to the "--env|-e" / "--no-debug" @@ -86,6 +87,7 @@ class SymfonyRuntime extends GenericRuntime * env_var_name?: string, * debug_var_name?: string, * dotenv_overload?: ?bool, + * dotenv_extra_paths?: ?string[], * } $options */ public function __construct(array $options = []) @@ -107,12 +109,22 @@ public function __construct(array $options = []) } if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) { - (new Dotenv($envKey, $debugKey)) + $overrideExistingVars = $options['dotenv_overload'] ?? false; + $dotenv = (new Dotenv($envKey, $debugKey)) ->setProdEnvs((array) ($options['prod_envs'] ?? ['prod'])) - ->usePutenv($options['use_putenv'] ?? false) - ->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false); + ->usePutenv($options['use_putenv'] ?? false); - if (isset($this->input) && ($options['dotenv_overload'] ?? false)) { + $dotenv->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $overrideExistingVars); + + if (\is_array($options['dotenv_extra_paths'] ?? null) && $options['dotenv_extra_paths']) { + $options['dotenv_extra_paths'] = array_map(fn (string $path) => $options['project_dir'].'/'.$path, $options['dotenv_extra_paths']); + + $overrideExistingVars + ? $dotenv->overload(...$options['dotenv_extra_paths']) + : $dotenv->load(...$options['dotenv_extra_paths']); + } + + if (isset($this->input) && $overrideExistingVars) { if ($this->input->getParameterOption(['--env', '-e'], $_SERVER[$envKey], true) !== $_SERVER[$envKey]) { throw new \LogicException(\sprintf('Cannot use "--env" or "-e" when the "%s" file defines "%s" and the "dotenv_overload" runtime option is true.', $options['dotenv_path'] ?? '.env', $envKey)); } diff --git a/Tests/phpt/.env.extra b/Tests/phpt/.env.extra new file mode 100644 index 0000000..0e7e46a --- /dev/null +++ b/Tests/phpt/.env.extra @@ -0,0 +1 @@ +SOME_VAR=foo_bar_extra diff --git a/Tests/phpt/dotenv_extra_load.php b/Tests/phpt/dotenv_extra_load.php new file mode 100644 index 0000000..3564499 --- /dev/null +++ b/Tests/phpt/dotenv_extra_load.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +$_SERVER['SOME_VAR'] = 'ccc'; +$_SERVER['APP_RUNTIME_OPTIONS'] = [ + 'dotenv_extra_paths' => [ + '.env.extra', + ], + 'dotenv_overload' => false, +]; + +require __DIR__.'/autoload.php'; + +return fn (Request $request, array $context) => new Response('OK Request '.$context['SOME_VAR']); diff --git a/Tests/phpt/dotenv_extra_load.phpt b/Tests/phpt/dotenv_extra_load.phpt new file mode 100644 index 0000000..89da5c2 --- /dev/null +++ b/Tests/phpt/dotenv_extra_load.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Dotenv extra paths load +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +OK Request ccc diff --git a/Tests/phpt/dotenv_extra_overload.php b/Tests/phpt/dotenv_extra_overload.php new file mode 100644 index 0000000..e834257 --- /dev/null +++ b/Tests/phpt/dotenv_extra_overload.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +$_SERVER['SOME_VAR'] = 'ccc'; +$_SERVER['APP_RUNTIME_OPTIONS'] = [ + 'dotenv_extra_paths' => [ + '.env.extra', + ], + 'dotenv_overload' => true, +]; + +require __DIR__.'/autoload.php'; + +return fn (Request $request, array $context) => new Response('OK Request '.$context['SOME_VAR']); diff --git a/Tests/phpt/dotenv_extra_overload.phpt b/Tests/phpt/dotenv_extra_overload.phpt new file mode 100644 index 0000000..88fa4c5 --- /dev/null +++ b/Tests/phpt/dotenv_extra_overload.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Dotenv extra paths overload +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +OK Request foo_bar_extra