Skip to content

Commit e0c4af9

Browse files
committed
fix: prevent project .env.slic.local from overriding staged PHP version
1 parent d6a74bd commit e0c4af9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/slic.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,10 @@ function setup_slic_env( $root_dir, $reset = false, $stack_id = null ) {
284284
load_env_file( $root_dir . '/.env.slic.local' );
285285
}
286286

287-
// Start by loadind the run file for all stacks, if any.
287+
// Start by loading the run file for all stacks, if any.
288288
$run_file = $root_dir . '/.env.slic.run';
289+
$staged_php_version = null;
290+
289291
if ( is_file( $run_file ) ) {
290292
load_env_file( $run_file );
291293
}
@@ -313,6 +315,19 @@ function setup_slic_env( $root_dir, $reset = false, $stack_id = null ) {
313315
}
314316
}
315317

318+
// Read the staged PHP version before a project's .env.slic.local can overwrite it.
319+
// The staged version may come from the global run file or a stack-specific state file.
320+
$staged_env_file = ! empty( $stack_run_file ) && is_file( $stack_run_file ) ? $stack_run_file : $run_file;
321+
322+
if ( is_file( $staged_env_file ) ) {
323+
$staged_env = read_env_file( $staged_env_file );
324+
$is_php_version_staged = ( $staged_env['SLIC_PHP_VERSION_STAGED'] ?? '' ) === '1';
325+
326+
if ( $is_php_version_staged && isset( $staged_env['SLIC_PHP_VERSION'] ) ) {
327+
$staged_php_version = $staged_env['SLIC_PHP_VERSION'];
328+
}
329+
}
330+
316331
/*
317332
* Special handling of the `use` command.
318333
*
@@ -341,6 +356,9 @@ function setup_slic_env( $root_dir, $reset = false, $stack_id = null ) {
341356
if ( $target_version ) {
342357
putenv( "SLIC_PHP_VERSION=$target_version" );
343358
putenv( "SLIC_PHP_CLI_VERSION=$target_version" );
359+
} elseif ( $staged_php_version !== null ) {
360+
// A PHP version was staged; restore it since the project's .env.slic.local may have overwritten it.
361+
putenv( "SLIC_PHP_VERSION=$staged_php_version" );
344362
}
345363

346364
// All the possible env files have been loaded, time to set the db image depending on the PHP version.

0 commit comments

Comments
 (0)