Skip to content

[DependencyInjection] Fix proxying services defined with an abstract class and a factory #61121

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
Jul 15, 2025
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 @@ -197,7 +197,7 @@ public function getProxyClass(Definition $definition, bool $asGhostObject, ?\Ref
return $class->name;
}

if (!$definition->hasTag('proxy') && !$class->isInterface()) {
if (!$definition->hasTag('proxy') && !$class->isAbstract()) {
$parent = $class;
do {
$extendsInternalClass = $parent->isInternal();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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;

abstract class AbstractSayClass
{
abstract public function say(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\LazyProxy\Instantiator;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\LazyServiceInstantiator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractSayClass;

class LazyServiceInstantiatorTest extends TestCase
{
public function testInstantiateAbstractClassProxy()
{
$instantiator = new LazyServiceInstantiator();
$instance = new class extends AbstractSayClass {
public int $calls = 0;

public function say(): string
{
++$this->calls;

return 'Hello from the abstract class!';
}
};

$definition = (new Definition(AbstractSayClass::class))
->setLazy(true);

$proxy = $instantiator->instantiateProxy(new Container(), $definition, 'foo', fn () => $instance);

$this->assertSame(0, $instance->calls);
$this->assertSame('Hello from the abstract class!', $proxy->say());
$this->assertSame(1, $instance->calls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\LazyServiceDumper;
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractSayClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ReadOnlyClass;

class LazyServiceDumperTest extends TestCase
Expand All @@ -40,6 +41,16 @@ public function testFinalClassInterface()
$this->assertStringContainsString('function get(', $dumper->getProxyCode($definition));
}

public function testAbstractClass()
{
$dumper = new LazyServiceDumper();
$definition = (new Definition(AbstractSayClass::class))
->setLazy(true);

$this->assertTrue($dumper->isProxyCandidate($definition));
$this->assertNotSame(AbstractSayClass::class, $dumper->getProxyClass($definition, false));
}

public function testInvalidClass()
{
$dumper = new LazyServiceDumper();
Expand Down
Loading
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