Skip to content

Commit 21386a2

Browse files
committed
Add bool env processor when not defined
Fixes #53918
1 parent b167190 commit 21386a2

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add argument `$prepend` to `ContainerConfigurator::extension()` to prepend the configuration instead of appending it
99
* Have `ServiceLocator` implement `ServiceCollectionInterface`
1010
* Add `#[Lazy]` attribute as shortcut for `#[Autowire(lazy: [bool|string])]` and `#[Autoconfigure(lazy: [bool|string])]`
11+
* Add bool env var processor when needed
1112

1213
7.0
1314
---

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14+
use ReflectionNamedType;
1415
use Symfony\Component\Config\Resource\ClassExistenceResource;
1516
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1617
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
@@ -308,6 +309,11 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
308309

309310
foreach ($attributes as $attribute) {
310311
$attribute = $attribute->newInstance();
312+
313+
if ($parameter->getType() instanceof ReflectionNamedType && $parameter->getType()->getName() === 'bool' && preg_match('/^%env\((?<name>\w+)\)%$/', $attribute->value, $matches) === 1) {
314+
$attribute = new Autowire(env: 'bool:' . $matches['name']);
315+
}
316+
311317
$invalidBehavior = $parameter->allowsNull() ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE;
312318

313319
try {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,4 +1393,17 @@ public function testLazyNotCompatibleWithAutowire()
13931393
$this->assertSame('Using both attributes #[Lazy] and #[Autowire] on an argument is not allowed; use the "lazy" parameter of #[Autowire] instead.', $e->getMessage());
13941394
}
13951395
}
1396+
1397+
public function testAutowireAttributeBoolParameterWithoutBoolEnvProcessor()
1398+
{
1399+
$container = new ContainerBuilder();
1400+
1401+
$container->register('foo', AutowireAttributeEnvBool::class)->setAutowired(true);
1402+
1403+
(new AutowirePass())->process($container);
1404+
1405+
$definition = $container->getDefinition('foo');
1406+
1407+
$this->assertSame('%env(bool:ENABLED)%', $container->resolveEnvPlaceholders($definition->getArguments()[0]));
1408+
}
13961409
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ public function __construct(
8181
}
8282
}
8383

84+
class AutowireAttributeEnvBool
85+
{
86+
public function __construct(
87+
#[Autowire(env: 'ENABLED')]
88+
public bool $enabled,
89+
) {
90+
}
91+
}
92+
8493
interface AsDecoratorInterface
8594
{
8695
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy