Skip to content

[DependencyInjection] Respect original service class when a proxy is defined #60766

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
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 @@ -334,9 +334,13 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
$value = $attribute->buildDefinition($value, $type, $parameter);
$value = $this->doProcessValue($value);
} elseif ($lazy = $attribute->lazy) {
$value ??= $getValue();
if ($this->container->has($value->getType())) {
$type = $this->container->findDefinition($value->getType())->getClass();
}
$definition = (new Definition($type))
->setFactory('current')
->setArguments([[$value ??= $getValue()]])
->setArguments([[$value]])
->setLazy(true);

if (!\is_array($lazy)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\ResourceInterface;
Expand Down Expand Up @@ -44,6 +45,8 @@
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
Expand All @@ -56,6 +59,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;
Expand Down Expand Up @@ -2125,6 +2129,35 @@ public function testLazyClosure()
$this->assertSame(1 + $cloned, Foo::$counter);
$this->assertSame(1, (new \ReflectionFunction($container->get('closure')))->getNumberOfParameters());
}

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

$phpLoader = new PhpFileLoader($container, new FileLocator());
$instanceof = [];
$configurator = new ContainerConfigurator($container, $phpLoader, $instanceof, __DIR__, __FILE__);

$services = $configurator->services();
$services
->defaults()
->autowire()
->autoconfigure()
->public()

->load('Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance\\', __DIR__.'/Fixtures/ProxyAndInheritance/*')
;

$services->set(
ProxyAndInheritance\Application::class,
ProxyAndInheritance\RepackedApplication::class,
);

$container->compile(true);

$foo = $container->get(ProxyAndInheritance\Foo::class);
$this->assertSame('my-app', $foo->getApplicationName());
}
}

class FooClass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;

class Application
{
public function __construct(
private Foo $foo,
private string $name = 'my-app',
) {
}

public function getName(): string
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;

use Symfony\Component\DependencyInjection\Attribute\Autowire;

class Foo
{
public function __construct(
#[Autowire(lazy: true)]
private Application $application,
) {
}

public function getApplicationName(): string
{
return $this->application->getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\ProxyAndInheritance;

class RepackedApplication extends Application
{
}
Loading