Skip to content

Cast env vars to null or bool when referencing them using #[Autowire(env: '...')] depending on the signature of the corresponding parameter #53971

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 1 commit into from
Apr 13, 2024
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* Make `ContainerBuilder::registerAttributeForAutoconfiguration()` propagate to attribute classes that extend the registered class
* Add argument `$prepend` to `FileLoader::construct()` to prepend loaded configuration instead of appending it
* [BC BREAK] When used in the `prependExtension()` method, the `ContainerConfigurator::import()` method now prepends the configuration instead of appending it
* Cast env vars to null or bool when referencing them using `#[Autowire(env: '...')]` depending on the signature of the corresponding parameter

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a

foreach ($attributes as $attribute) {
$attribute = $attribute->newInstance();
$value = $attribute instanceof Autowire ? $attribute->value : null;

if (\is_string($value) && str_starts_with($value, '%env(') && str_ends_with($value, ')%')) {
if ($parameter->getType() instanceof \ReflectionNamedType && 'bool' === $parameter->getType()->getName() && !str_starts_with($value, '%env(bool:')) {
$attribute = new Autowire(substr_replace($value, 'bool:', 5, 0));
}
if ($parameter->isDefaultValueAvailable() && $parameter->allowsNull() && null === $parameter->getDefaultValue() && !preg_match('/(^|:)default:/', $value)) {
$attribute = new Autowire(substr_replace($value, 'default::', 5, 0));
}
}

$invalidBehavior = $parameter->allowsNull() ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1397,4 +1397,18 @@ public function testLazyNotCompatibleWithAutowire()
$this->assertSame('Using both attributes #[Lazy] and #[Autowire] on an argument is not allowed; use the "lazy" parameter of #[Autowire] instead.', $e->getMessage());
}
}

public function testAutowireAttributeWithEnvVar()
{
$container = new ContainerBuilder();

$container->register('foo', AutowireAttributeEnv::class)->setAutowired(true);

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

$definition = $container->getDefinition('foo');

$this->assertSame('%env(bool:ENABLED)%', $container->resolveEnvPlaceholders($definition->getArguments()[0]));
$this->assertSame('%env(default::OPTIONAL)%', $container->resolveEnvPlaceholders($definition->getArguments()[1]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ public function __construct(
}
}

class AutowireAttributeEnv
{
public function __construct(
#[Autowire(env: 'ENABLED')]
public bool $enabled,
#[Autowire(env: 'OPTIONAL')]
public ?string $optional = null,
) {
}
}

interface AsDecoratorInterface
{
}
Expand Down
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