Skip to content

Commit d45bd61

Browse files
committed
[DependencyInjection] Respect original service class when a proxy is defined
1 parent 9d3b7c6 commit d45bd61

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,13 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
334334
$value = $attribute->buildDefinition($value, $type, $parameter);
335335
$value = $this->doProcessValue($value);
336336
} elseif ($lazy = $attribute->lazy) {
337+
$value ??= $getValue();
338+
if ($this->container->has($value->getType())) {
339+
$type = $this->container->findDefinition($value->getType())->getClass();
340+
}
337341
$definition = (new Definition($type))
338342
->setFactory('current')
339-
->setArguments([[$value ??= $getValue()]])
343+
->setArguments([[$value]])
340344
->setLazy(true);
341345

342346
if (!\is_array($lazy)) {

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use PHPUnit\Framework\TestCase;
1919
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
20+
use Symfony\Component\Config\FileLocator;
2021
use Symfony\Component\Config\Resource\DirectoryResource;
2122
use Symfony\Component\Config\Resource\FileResource;
2223
use Symfony\Component\Config\Resource\ResourceInterface;
@@ -44,6 +45,8 @@
4445
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
4546
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
4647
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
48+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
49+
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
4750
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
4851
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
4952
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
@@ -56,6 +59,7 @@
5659
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
5760
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
5861
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
62+
use Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
5963
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
6064
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
6165
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;
@@ -2125,6 +2129,35 @@ public function testLazyClosure()
21252129
$this->assertSame(1 + $cloned, Foo::$counter);
21262130
$this->assertSame(1, (new \ReflectionFunction($container->get('closure')))->getNumberOfParameters());
21272131
}
2132+
2133+
public function testProxyAndInheritance()
2134+
{
2135+
$container = new ContainerBuilder();
2136+
2137+
$phpLoader = new PhpFileLoader($container, new FileLocator());
2138+
$instanceof = [];
2139+
$configurator = new ContainerConfigurator($container, $phpLoader, $instanceof, __DIR__, __FILE__);
2140+
2141+
$services = $configurator->services();
2142+
$services
2143+
->defaults()
2144+
->autowire()
2145+
->autoconfigure()
2146+
->public()
2147+
2148+
->load('Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance\\', __DIR__.'/Fixtures/ProxyAndInheritance/*')
2149+
;
2150+
2151+
$services->set(
2152+
ProxyAndInheritance\Application::class,
2153+
ProxyAndInheritance\RepackedApplication::class,
2154+
);
2155+
2156+
$container->compile(true);
2157+
2158+
$foo = $container->get(ProxyAndInheritance\Foo::class);
2159+
$this->assertSame('my-app', $foo->getApplicationName());
2160+
}
21282161
}
21292162

21302163
class FooClass
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
13+
14+
class Application
15+
{
16+
public function __construct(
17+
private Foo $foo,
18+
private string $name = 'my-app',
19+
) {
20+
}
21+
22+
public function getName(): string
23+
{
24+
return $this->name;
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
13+
14+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
15+
16+
class Foo
17+
{
18+
public function __construct(
19+
#[Autowire(lazy: true)]
20+
private Application $application,
21+
) {
22+
}
23+
24+
public function getApplicationName(): string
25+
{
26+
return $this->application->getName();
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
13+
14+
class RepackedApplication extends Application
15+
{
16+
}

0 commit comments

Comments
 (0)