Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
$value = $this->doProcessValue($value);
} elseif ($lazy = $attribute->lazy) {
$value ??= $getValue();
if ($this->container->has($value->getType())) {
$type = $this->container->findDefinition($value->getType())->getClass();
}
$type = $this->resolveProxyType($type, $value->getType());
$definition = (new Definition($type))
->setFactory('current')
->setArguments([[$value]])
Expand Down Expand Up @@ -758,4 +756,30 @@ private function getCombinedAlias(string $type, ?string $name = null): ?string

return $alias;
}

/**
* Resolves the class name that should be proxied for a lazy service.
*
* @param string $originalType The original parameter type-hint (e.g., the interface)
* @param string $serviceId The service ID the type-hint resolved to (e.g., the alias)
*/
private function resolveProxyType(string $originalType, string $serviceId): string
{
if (!$this->container->has($serviceId)) {
return $originalType;
}

$resolvedType = $this->container->findDefinition($serviceId)->getClass();
$resolvedType = $this->container->getParameterBag()->resolveValue($resolvedType);

if (!$resolvedType || !class_exists($resolvedType, false) && !interface_exists($resolvedType, false)) {
return $originalType;
}

if (\PHP_VERSION_ID < 80400 && $this->container->getReflectionClass($resolvedType, false)->isFinal()) {
return $originalType;
}

return $resolvedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1411,4 +1411,45 @@ public function testAutowireAttributeWithEnvVar()
$this->assertSame('%env(bool:ENABLED)%', $container->resolveEnvPlaceholders($definition->getArguments()[0]));
$this->assertSame('%env(default::OPTIONAL)%', $container->resolveEnvPlaceholders($definition->getArguments()[1]));
}

public function testLazyProxyForInterfaceWithFinalImplementation()
{
$container = new ContainerBuilder();
$container->register('final_impl', FinalLazyProxyImplementation::class);
$container->setAlias(LazyProxyTestInterface::class, 'final_impl');

$container->register(LazyProxyInterfaceConsumer::class)
->setAutoconfigured(true)
->setAutowired(true)
->setPublic(true);

$container->compile();

$service = $container->get(LazyProxyInterfaceConsumer::class);
$this->assertInstanceOf(LazyProxyInterfaceConsumer::class, $service);

// Trigger lazy load
$dep = $service->getDep()->getSelf();
$this->assertInstanceOf(FinalLazyProxyImplementation::class, $dep);
}

public function testLazyProxyWithClassInheritance()
{
$container = new ContainerBuilder();
$container->register(BaseLazyProxyClass::class, ExtendedLazyProxyClass::class);

$container->register(LazyProxyInheritanceConsumer::class)
->setAutoconfigured(true)
->setAutowired(true)
->setPublic(true);

$container->compile();

$service = $container->get(LazyProxyInheritanceConsumer::class);
$this->assertInstanceOf(LazyProxyInheritanceConsumer::class, $service);

// Trigger lazy load
$dep = $service->getDependency()->getSelf();
$this->assertInstanceOf(ExtendedLazyProxyClass::class, $dep);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,56 @@ public static function staticCreateFooWithParam(mixed $someParam): MyInlineServi
return new MyInlineService($someParam);
}
}

interface LazyProxyTestInterface
{
public function getSelf(): self;
}

final class FinalLazyProxyImplementation implements LazyProxyTestInterface
{
public function getSelf(): self
{
return $this;
}
}

class BaseLazyProxyClass
{
public function getSelf(): self
{
return $this;
}
}

class ExtendedLazyProxyClass extends BaseLazyProxyClass
{
public function getSelf(): self
{
return $this;
}
}

class LazyProxyInterfaceConsumer
{
public function __construct(#[Autowire(lazy: true)] private readonly LazyProxyTestInterface $dep)
{
}

public function getDep(): LazyProxyTestInterface
{
return $this->dep;
}
}

class LazyProxyInheritanceConsumer
{
public function __construct(#[Autowire(lazy: true)] private readonly BaseLazyProxyClass $dep)
{
}

public function getDependency(): BaseLazyProxyClass
{
return $this->dep;
}
}
Loading