From 78846d2c186d4413a7338f8f161c1cd86adce791 Mon Sep 17 00:00:00 2001 From: Amin Date: Tue, 12 Mar 2024 13:27:19 +0100 Subject: [PATCH] Revert "Ensure consistency between constant and env variable in generated cache" This reverts commit dc86bff7f760457b4aa7f6d536452745cab5df12. --- src/Env/WordPressEnvBridge.php | 17 +++++++---------- tests/fixtures/example.env | 1 - .../integration/Env/WordPressEnvBridgeTest.php | 7 ------- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/Env/WordPressEnvBridge.php b/src/Env/WordPressEnvBridge.php index dd0b9c5..5576d76 100644 --- a/src/Env/WordPressEnvBridge.php +++ b/src/Env/WordPressEnvBridge.php @@ -565,17 +565,15 @@ public function dumpCached(string $file): bool $symfonyLoaded and $content .= "putenv('SYMFONY_DOTENV_VARS={$symfonyLoaded}');\n\n"; foreach (self::$cache as $key => list($value, $filtered)) { - $slashed = addslashes($value); - $preparedValue = $value !== $filtered - ? $filtered - : $slashed; - + $slashed = str_replace("'", "\'", $value); // For defined constants, dump the `define` with filtered value, if any. if ( in_array($key, $this->definedConstants, true) || array_key_exists($key, self::WP_CONSTANTS) ) { - $define = var_export($preparedValue, true); // phpcs:ignore + $define = $value !== $filtered + ? var_export($filtered, true) // phpcs:ignore + : "'{$slashed}'"; $content .= "define('{$key}', {$define});\n"; } @@ -586,10 +584,9 @@ public function dumpCached(string $file): bool } // For env loaded from file, dump the variable definition. - $content .= "putenv('{$key}={$preparedValue}');\n"; - $content .= "\$_ENV['{$key}'] = '{$preparedValue}';\n"; - (strpos($key, 'HTTP_') !== 0) - and $content .= "\$_SERVER['{$key}'] = '{$preparedValue}';\n\n"; + $content .= "putenv('{$key}={$slashed}');\n"; + $content .= "\$_ENV['{$key}'] = '{$slashed}';\n"; + (strpos($key, 'HTTP_') !== 0) and $content .= "\$_SERVER['{$key}'] = '{$slashed}';\n\n"; } $content .= sprintf("return %s;\n", var_export(static::$cache, true)); // phpcs:ignore diff --git a/tests/fixtures/example.env b/tests/fixtures/example.env index 251940a..10bfa22 100644 --- a/tests/fixtures/example.env +++ b/tests/fixtures/example.env @@ -21,4 +21,3 @@ PLUGIN_CONFIG_TWO=2 PLUGIN_CONFIG_THREE=true PLUGIN_CONFIG_FOUR=4 PLUGIN_CONFIG_FIVE=5 -AUTH_COOKIE=BAD&Authread('DB_TABLE_PREFIX')); static::assertSame('wp_user', $bridge->read('DB_USER')); static::assertSame('', $bridge->read('COOKIE_DOMAIN')); - static::assertSame('BAD&Auth', $bridge->read('AUTH_COOKIE')); } /** @@ -418,12 +417,6 @@ function (): array { static::assertSame('localhost', $cachedBridge->read('DB_HOST')); // ...and it should still be able to read things from actual env set after cache was built static::assertSame('XYZ', $cachedBridge->read('XYZ')); - - // Test consitency of defined constansts, getenv, $_ENV and $_SERVER in filtered variables - static::assertSame('BAD&Auth', AUTH_COOKIE); - static::assertSame('BAD&Auth', getenv('AUTH_COOKIE')); - static::assertSame('BAD&Auth', $_ENV['AUTH_COOKIE'] ?? null); - static::assertSame('BAD&Auth', $_SERVER['AUTH_COOKIE'] ?? null); } /**