Skip to content

Commit 06a9ef1

Browse files
committed
Make proxyDir and proxyNs nullable and optional
When using native lazy objects, it should be possible to omit these arguments, hence the default value. Also, when using native lazy objects, one should not have to configure the corresponding Configuration attributes, which means EntityManager__construct() should be able to pass null to this class, hence the nullability. Fixes #11997
1 parent da697f2 commit 06a9ef1

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Proxy/ProxyFactory.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function __serialize(): array
132132
/** @var array<class-string, Closure> */
133133
private array $proxyFactories = [];
134134

135+
private readonly string $proxyDir;
136+
private readonly string $proxyNs;
137+
135138
/**
136139
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
137140
* connected to the given <tt>EntityManager</tt>.
@@ -143,8 +146,8 @@ public function __serialize(): array
143146
*/
144147
public function __construct(
145148
private readonly EntityManagerInterface $em,
146-
private readonly string $proxyDir,
147-
private readonly string $proxyNs,
149+
string|null $proxyDir = null,
150+
string|null $proxyNs = null,
148151
bool|int $autoGenerate = self::AUTOGENERATE_NEVER,
149152
) {
150153
if (! $proxyDir && ! $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
@@ -159,6 +162,17 @@ public function __construct(
159162
throw ORMInvalidArgumentException::invalidAutoGenerateMode($autoGenerate);
160163
}
161164

165+
if ($proxyDir === null && $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
166+
$proxyDir = '';
167+
}
168+
169+
if ($proxyNs === null && $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
170+
$proxyNs = '';
171+
}
172+
173+
$this->proxyDir = $proxyDir;
174+
$this->proxyNs = $proxyNs;
175+
162176
$this->uow = $em->getUnitOfWork();
163177
$this->autoGenerate = (int) $autoGenerate;
164178
$this->identifierFlattener = new IdentifierFlattener($this->uow, $em->getMetadataFactory());

tests/Tests/ORM/Proxy/ProxyFactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
2121
use Doctrine\Tests\OrmTestCase;
2222
use PHPUnit\Framework\Attributes\Group;
23+
use PHPUnit\Framework\Attributes\RequiresPhp;
24+
use ReflectionClass;
2325
use ReflectionProperty;
2426
use stdClass;
2527

@@ -209,6 +211,24 @@ public function testProxyClonesParentFields(): void
209211
self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned');
210212
self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned');
211213
}
214+
215+
#[RequiresPhp('8.4')]
216+
public function testProxyFactoryAcceptsNullProxyArgsWhenNativeLazyObjectsAreEnabled(): void
217+
{
218+
$this->emMock->getConfiguration()->enableNativeLazyObjects(true);
219+
$this->proxyFactory = new ProxyFactory(
220+
$this->emMock,
221+
null,
222+
null,
223+
);
224+
$proxy = $this->proxyFactory->getProxy(
225+
ECommerceFeature::class,
226+
['id' => 42],
227+
);
228+
$reflection = new ReflectionClass($proxy);
229+
230+
self::assertTrue($reflection->isUninitializedLazyObject($proxy));
231+
}
212232
}
213233

214234
abstract class AbstractClass

0 commit comments

Comments
 (0)