Skip to content

Commit 82c5b62

Browse files
[DependencyInjection] Fix proxying services defined with an abstract class and a factory
1 parent 8a2c38a commit 82c5b62

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

LazyProxy/PhpDumper/LazyServiceDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function getProxyClass(Definition $definition, bool $asGhostObject, ?\Ref
197197
return $class->name;
198198
}
199199

200-
if (!$definition->hasTag('proxy') && !$class->isInterface()) {
200+
if (!$definition->hasTag('proxy') && !$class->isAbstract()) {
201201
$parent = $class;
202202
do {
203203
$extendsInternalClass = $parent->isInternal();

Tests/Fixtures/AbstractSayClass.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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;
13+
14+
abstract class AbstractSayClass
15+
{
16+
abstract public function say(): string;
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\LazyProxy\Instantiator;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Container;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\LazyServiceInstantiator;
18+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractSayClass;
19+
20+
class LazyServiceInstantiatorTest extends TestCase
21+
{
22+
public function testInstantiateAbstractClassProxy()
23+
{
24+
$instantiator = new LazyServiceInstantiator();
25+
$instance = new class extends AbstractSayClass {
26+
public int $calls = 0;
27+
28+
public function say(): string
29+
{
30+
++$this->calls;
31+
32+
return 'Hello from the abstract class!';
33+
}
34+
};
35+
36+
$definition = (new Definition(AbstractSayClass::class))
37+
->setLazy(true);
38+
39+
$proxy = $instantiator->instantiateProxy(new Container(), $definition, 'foo', fn () => $instance);
40+
41+
$this->assertSame(0, $instance->calls);
42+
$this->assertSame('Hello from the abstract class!', $proxy->say());
43+
$this->assertSame(1, $instance->calls);
44+
}
45+
}

Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1818
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\LazyServiceDumper;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractSayClass;
1920
use Symfony\Component\DependencyInjection\Tests\Fixtures\ReadOnlyClass;
2021

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

44+
public function testAbstractClass()
45+
{
46+
$dumper = new LazyServiceDumper();
47+
$definition = (new Definition(AbstractSayClass::class))
48+
->setLazy(true);
49+
50+
$this->assertTrue($dumper->isProxyCandidate($definition));
51+
$this->assertNotSame(AbstractSayClass::class, $dumper->getProxyClass($definition, false));
52+
}
53+
4354
public function testInvalidClass()
4455
{
4556
$dumper = new LazyServiceDumper();

0 commit comments

Comments
 (0)