Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita4c9b4f

Browse files
committed
[DependencyInjection] Respect original service class when a proxy is defined
1 parentab6c38d commita4c9b4f

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

‎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 = (newDefinition($type))
338342
->setFactory('current')
339-
->setArguments([[$value ??=$getValue()]])
343+
->setArguments([[$value]])
340344
->setLazy(true);
341345

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

‎Tests/ContainerBuilderTest.php‎

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

1818
usePHPUnit\Framework\TestCase;
1919
useSymfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
20+
useSymfony\Component\Config\FileLocator;
2021
useSymfony\Component\Config\Resource\DirectoryResource;
2122
useSymfony\Component\Config\Resource\FileResource;
2223
useSymfony\Component\Config\Resource\ResourceInterface;
@@ -44,6 +45,8 @@
4445
useSymfony\Component\DependencyInjection\Extension\ExtensionInterface;
4546
useSymfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
4647
useSymfony\Component\DependencyInjection\Loader\ClosureLoader;
48+
useSymfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
49+
useSymfony\Component\DependencyInjection\Loader\PhpFileLoader;
4750
useSymfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
4851
useSymfony\Component\DependencyInjection\ParameterBag\ParameterBag;
4952
useSymfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
@@ -56,6 +59,7 @@
5659
useSymfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
5760
useSymfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
5861
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
62+
useSymfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
5963
useSymfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
6064
useSymfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
6165
useSymfony\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+
publicfunctiontestProxyAndInheritance()
2134+
{
2135+
$container =newContainerBuilder();
2136+
2137+
$phpLoader =newPhpFileLoader($container,newFileLocator());
2138+
$instanceof = [];
2139+
$configurator =newContainerConfigurator($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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
13+
14+
class Application
15+
{
16+
publicfunction__construct(
17+
privateFoo$foo,
18+
privatestring$name ='my-app',
19+
) {
20+
}
21+
22+
publicfunctiongetName():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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
13+
14+
useSymfony\Component\DependencyInjection\Attribute\Autowire;
15+
16+
class Foo
17+
{
18+
publicfunction__construct(
19+
#[Autowire(lazy:true)]
20+
privateApplication$application,
21+
) {
22+
}
23+
24+
publicfunctiongetApplicationName():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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
13+
14+
class RepackedApplicationextends Application
15+
{
16+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp