8000 minor #47176 [DependencyInjection] Container building optimization (s… · symfony/symfony@63c554b · GitHub
[go: up one dir, main page]

Skip to content

Commit 63c554b

Browse files
committed
minor #47176 [DependencyInjection] Container building optimization (sveneld)
This PR was merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Container building optimization | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | License | MIT When you have to much env variables and many services in your container, container compilаtion can take much time due to the function stripos which is called much time. It happens because method freezeAfterProcessing try to find env placeholders in config, but make it in two foreach. So i offer to get all possible placeholders with preg_match_all and do not use stripos for each placeholed. Same situation with method resolveEnvPlaceholders. But in this method we should left stripos for hard cases when in value could be something like this 'env_1418b707a69b8575_const_HOST_74bcc04c4799f2ca10fb754fe5581c89env_1418b707a69b8575_const_CORE_URL_91ef68034c1e9e8b58c13725d3bafdc0' In my project it helps me to save 40 seconds in container building. Related to my previous fix #44876 Commits ------- c886e2c [DependencyInjection] Container building optimization
2 parents 38678dc + c886e2c commit 63c554b

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ public function freezeAfterProcessing(Extension $extension, ContainerBuilder $co
117117
// serialize config and container to catch env vars nested in object graphs
118118
$config = serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all());
119119

120+
if (false === stripos($config, 'env_')) {
121+
return;
122+
}
123+
124+
preg_match_all('/env_[a-f0-9]{16}_\w+_[a-f0-9]{32}/Ui', $config, $matches);
125+
$usedPlaceholders = array_flip($matches[0]);
120126
foreach (parent::getEnvPlaceholders() as $env => $placeholders) {
121127
foreach ($placeholders as $placeholder) {
122-
if (false !== stripos($config, $placeholder)) {
128+
if (isset($usedPlaceholders[$placeholder])) {
123129
$this->processedEnvPlaceholders[$env] = $placeholders;
124130
break;
125131
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,15 +1358,17 @@ public function resolveEnvPlaceholders(mixed $value, string|bool $format = null,
13581358
return $result;
13591359
}
13601360

1361-
if (!\is_string($value) || 38 > \strlen($value) || !preg_match('/env[_(]/i', $value)) {
1361+
if (!\is_string($value) || 38 > \strlen($value) || false === stripos($value, 'env_')) {
13621362
return $value;
13631363
}
13641364
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
13651365

13661366
$completed = false;
1367+
preg_match_all('/env_[a-f0-9]{16}_\w+_[a-f0-9]{32}/Ui', $value, $matches);
1368+
$usedPlaceholders = array_flip($matches[0]);
13671369
foreach ($envPlaceholders as $env => $placeholders) {
13681370
foreach ($placeholders as $placeholder) {
1369-
if (false !== stripos($value, $placeholder)) {
1371+
if (isset($usedPlaceholders[$placeholder])) {
13701372
if (true === $format) {
13711373
$resolved = $bag->escapeValue($this->getEnv($env));
13721374
} else {

src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ public function testConcatenatedEnvInConfig()
153153
$this->assertSame(['scalar_node' => $expected], $container->resolveEnvPlaceholders($ext->getConfig()));
154154
}
155155

156+
public function testSurroundedEnvInConfig()
157+
{
158+
$container = new ContainerBuilder();
159+
$container->registerExtension($ext = new EnvExtension());
160+
$container->prependExtensionConfig('env_extension', [
161+
'scalar_node' => $expected = 'foo%env(BAR)%baz',
162+
]);
163+
164+
$this->doProcess($container);
165+
166+
$this->assertSame(['scalar_node' => $expected], $container->resolveEnvPlaceholders($ext->getConfig()));
167+
}
168+
156169
public function testEnvIsIncompatibleWithArrayNode()
157170
{
158171
$this->expectException(InvalidConfigurationException::class);

0 commit comments

Comments
 (0)
0