Skip to content

Commit

Permalink
Revert "Ensure consistency between constant and env variable in gener…
Browse files Browse the repository at this point in the history
…ated cache"

This reverts commit dc86bff.
  • Loading branch information
amiut committed Mar 12, 2024
1 parent dc86bff commit 78846d2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
17 changes: 7 additions & 10 deletions src/Env/WordPressEnvBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ PLUGIN_CONFIG_TWO=2
PLUGIN_CONFIG_THREE=true
PLUGIN_CONFIG_FOUR=4
PLUGIN_CONFIG_FIVE=5
AUTH_COOKIE=BAD&Auth<Cookie
7 changes: 0 additions & 7 deletions tests/integration/Env/WordPressEnvBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function testLoadFile(): void
static::assertSame('xxx_', $bridge->read('DB_TABLE_PREFIX'));
static::assertSame('wp_user', $bridge->read('DB_USER'));
static::assertSame('', $bridge->read('COOKIE_DOMAIN'));
static::assertSame('BAD&amp;Auth', $bridge->read('AUTH_COOKIE'));
}

/**
Expand Down Expand Up @@ -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&amp;Auth', AUTH_COOKIE);
static::assertSame('BAD&amp;Auth', getenv('AUTH_COOKIE'));
static::assertSame('BAD&amp;Auth', $_ENV['AUTH_COOKIE'] ?? null);
static::assertSame('BAD&amp;Auth', $_SERVER['AUTH_COOKIE'] ?? null);
}

/**
Expand Down

0 comments on commit 78846d2

Please sign in to comment.