Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection][Config] Use several placeholder unique prefixes for dynamic placeholder values #37511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class BaseNode implements NodeInterface
{
const DEFAULT_PATH_SEPARATOR = '.';

private static $placeholderUniquePrefix;
private static $placeholderUniquePrefixes = [];
private static $placeholders = [];

protected $name;
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function setPlaceholder(string $placeholder, array $values): void
}

/**
* Sets a common prefix for dynamic placeholder values.
* Adds a common prefix for dynamic placeholder values.
*
* Matching configuration values will be skipped from being processed and are returned as is, thus preserving the
* placeholder. An exact match provided by {@see setPlaceholder()} might take precedence.
Expand All @@ -83,7 +83,7 @@ public static function setPlaceholder(string $placeholder, array $values): void
*/
public static function setPlaceholderUniquePrefix(string $prefix): void
{
self::$placeholderUniquePrefix = $prefix;
self::$placeholderUniquePrefixes[] = $prefix;
}

/**
Expand All @@ -93,7 +93,7 @@ public static function setPlaceholderUniquePrefix(string $prefix): void
*/
public static function resetPlaceholders(): void
{
self::$placeholderUniquePrefix = null;
self::$placeholderUniquePrefixes = [];
self::$placeholders = [];
}

Expand Down Expand Up @@ -513,8 +513,10 @@ private static function resolvePlaceholderValue($value)
return self::$placeholders[$value];
}

if (self::$placeholderUniquePrefix && 0 === strpos($value, self::$placeholderUniquePrefix)) {
return [];
foreach (self::$placeholderUniquePrefixes as $placeholderUniquePrefix) {
if (0 === strpos($value, $placeholderUniquePrefix)) {
return [];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public function process(ContainerBuilder $container)
$container->getParameterBag()->mergeEnvPlaceholders($resolvingBag);
}

throw $e;
} finally {
if ($configAvailable) {
BaseNode::resetPlaceholders();
}

throw $e;
}

if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
Expand All @@ -95,6 +95,10 @@ public function process(ContainerBuilder $container)
$container->getParameterBag()->add($parameters);
}

if ($configAvailable) {
BaseNode::resetPlaceholders();
}

$container->addDefinitions($definitions);
$container->addAliases($aliases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Resource\FileResource;
Expand Down Expand Up @@ -128,6 +129,23 @@ public function testThrowingExtensionsGetMergedBag()

$this->assertSame(['FOO'], array_keys($container->getParameterBag()->getEnvPlaceholders()));
}

public function testReuseEnvPlaceholderGeneratedByPreviousExtension()
{
if (!property_exists(BaseNode::class, 'placeholderUniquePrefixes')) {
$this->markTestSkipped('This test requires symfony/config ^4.4.11|^5.0.11|^5.1.3');
}

$container = new ContainerBuilder();
$container->registerExtension(new FooExtension());
$container->registerExtension(new TestCccExtension());
$container->prependExtensionConfig('foo', ['bool_node' => '%env(bool:MY_ENV_VAR)%']);
$container->prependExtensionConfig('test_ccc', ['bool_node' => '%env(bool:MY_ENV_VAR)%']);

(new MergeExtensionConfigurationPass())->process($container);

$this->addToAssertionCount(1);
}
}

class FooConfiguration implements ConfigurationInterface
Expand All @@ -139,6 +157,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('bar')->end()
->scalarNode('baz')->end()
->booleanNode('bool_node')->end()
->end();

return $treeBuilder;
Expand Down Expand Up @@ -166,6 +185,8 @@ public function load(array $configs, ContainerBuilder $container)
$container->getParameterBag()->get('env(BOZ)');
$container->resolveEnvPlaceholders($config['baz']);
}

$container->setParameter('foo.param', 'ccc');
}
}

Expand Down Expand Up @@ -194,3 +215,36 @@ public function load(array $configs, ContainerBuilder $container)
throw new \Exception();
}
}

final class TestCccConfiguration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('test_ccc');
$treeBuilder->getRootNode()
->children()
->booleanNode('bool_node')->end()
->end();

return $treeBuilder;
}
}

final class TestCccExtension extends Extension
{
public function getAlias(): string
{
return 'test_ccc';
}

public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface
{
return new TestCccConfiguration();
}

public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$this->processConfiguration($configuration, $configs);
}
}