Skip to content

Commit

Permalink
reset env vars on kernel.reset
Browse files Browse the repository at this point in the history
  • Loading branch information
faizanakram99 committed Apr 18, 2024
1 parent 13ab9eb commit 1d83e46
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -287,6 +287,8 @@ public function reset(): void
continue;
}
}

$this->envCache = [];
}

/**
Expand Down
Expand Up @@ -774,6 +774,14 @@ public function compile(bool $resolveEnvPlaceholders = false): void
}
}

public function reset(): void
{
parent::reset();

$this->envPlaceholders = [];
$this->envCounters = [];
}

public function getServiceIds(): array
{
return array_map('strval', array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliasDefinitions), parent::getServiceIds())));
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Expand Up @@ -14,15 +14,18 @@
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Contracts\Service\ResetInterface;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class EnvVarProcessor implements EnvVarProcessorInterface
class EnvVarProcessor implements EnvVarProcessorInterface, ResetInterface
{
private ContainerInterface $container;
/** @var \Traversable<EnvVarLoaderInterface> */
private \Traversable $loaders;
/** @var \Traversable<EnvVarLoaderInterface> */
private \Traversable $originalLoaders;
private array $loadedVars = [];

/**
Expand All @@ -31,7 +34,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
public function __construct(ContainerInterface $container, ?\Traversable $loaders = null)
{
$this->container = $container;
$this->loaders = $loaders ?? new \ArrayIterator();
$this->originalLoaders = $this->loaders = $loaders ?? new \ArrayIterator();
}

public static function getProvidedTypes(): array
Expand Down Expand Up @@ -366,4 +369,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed

throw new RuntimeException(sprintf('Unsupported env var prefix "%s" for env name "%s".', $prefix, $name));
}

public function reset(): void
{
$this->loadedVars = [];
$this->loaders = $this->originalLoaders;
}
}
Expand Up @@ -687,6 +687,25 @@ public function testGetEnvCountersWithEnum()
$this->assertSame($expected, $config->getEnvCounters());
}

public function testResetResetsEnvCounters()
{
$bag = new EnvPlaceholderParameterBag();
$config = new ContainerBuilder($bag);
$config->resolveEnvPlaceholders([
$bag->get('env(Bar)'),
]);

$expectedBeforeReset = ['Bar' => 1];

$this->assertSame($expectedBeforeReset, $config->getEnvCounters());

$config->reset();

$expectedAfterReset = ['Bar' => 0];

$this->assertSame($expectedAfterReset, $config->getEnvCounters());
}

public function testCreateServiceWithAbstractArgument()
{
$builder = new ContainerBuilder();
Expand Down
Expand Up @@ -136,6 +136,68 @@ public function testGetEnvBool($value, $processed)
$this->assertSame($processed, $result);
}

public function testGetEnvCachesEnv()
{
$_ENV['FOO'] = '';

$GLOBALS['ENV_FOO'] = 'value';

$loaders = function () {
yield new class() implements EnvVarLoaderInterface {
public function loadEnvVars(): array
{
return ['FOO' => $GLOBALS['ENV_FOO']];
}
};
};

$processor = new EnvVarProcessor(new Container(), new RewindableGenerator($loaders, 1));

$noop = function () {};

$result = $processor->getEnv('string', 'FOO', $noop);
$this->assertSame('value', $result);

$GLOBALS['ENV_FOO'] = 'new value';

$result = $processor->getEnv('string', 'FOO', $noop);
$this->assertSame('value', $result);

unset($_ENV['FOO'], $GLOBALS['env_processor_call_count']);
}

public function testReset()
{
$_ENV['FOO'] = '';

$GLOBALS['ENV_FOO'] = 'value';

$loaders = function () {
yield new class() implements EnvVarLoaderInterface {
public function loadEnvVars(): array
{
return ['FOO' => $GLOBALS['ENV_FOO']];
}
};
};

$processor = new EnvVarProcessor(new Container(), new RewindableGenerator($loaders, 1));

$noop = function () {};

$result = $processor->getEnv('string', 'FOO', $noop);
$this->assertSame('value', $result);

$GLOBALS['ENV_FOO'] = 'new value';

$processor->reset();

$result = $processor->getEnv('string', 'FOO', $noop);
$this->assertSame('new value', $result);

unset($_ENV['FOO'], $GLOBALS['env_processor_call_count']);
}

/**
* @dataProvider validBools
*/
Expand Down Expand Up @@ -625,7 +687,7 @@ public static function validNullables()
['null', 'null'],
['Null', 'Null'],
['NULL', 'NULL'],
];
];
}

public function testRequireMissingFile()
Expand Down

0 comments on commit 1d83e46

Please sign in to comment.