Skip to content

Commit d882945

Browse files
authored
Merge pull request doctrine#1888 from eltharin/config_php_lazy
add configuration for php native lazy object
2 parents 0ebf99d + fd34f18 commit d882945

File tree

9 files changed

+148
-0
lines changed

9 files changed

+148
-0
lines changed

config/schema/doctrine-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<xsd:attribute name="proxy-namespace" type="xsd:string" />
151151
<xsd:attribute name="auto-generate-proxy-classes" type="xsd:string" default="false" />
152152
<xsd:attribute name="enable-lazy-ghost-objects" type="xsd:boolean" />
153+
<xsd:attribute name="enable-native-lazy-objects" type="xsd:boolean" />
153154
<xsd:attributeGroup ref="entity-manager-config" />
154155
</xsd:complexType>
155156

docs/en/configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ Configuration Reference
254254
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
255255
default_repository_class: Doctrine\ORM\EntityRepository
256256
auto_mapping: false
257+
# Opt-in to PHP native lazy objects
258+
enable_native_lazy_objects: false
257259
# Opt-in to new mapping driver mode as of Doctrine ORM 2.16, https://github.com/doctrine/orm/pull/10455
258260
report_fields_where_declared: false
259261
# 0pt-in to the new mapping driver mode as of Doctrine ORM 2.14. See https://github.com/doctrine/orm/pull/6728.

src/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,10 @@ private function getOrmEntityManagersNode(): ArrayNodeDefinition
652652
->scalarNode('class_metadata_factory_name')->defaultValue(ClassMetadataFactory::class)->end()
653653
->scalarNode('default_repository_class')->defaultValue(EntityRepository::class)->end()
654654
->scalarNode('auto_mapping')->defaultFalse()->end()
655+
->booleanNode('enable_native_lazy_objects')
656+
->defaultFalse()
657+
->info('Enables the new native implementation of PHP lazy objects instead of generated proxies')
658+
->end()
655659
->scalarNode('naming_strategy')->defaultValue('doctrine.orm.naming_strategy.default')->end()
656660
->scalarNode('quote_strategy')->defaultValue('doctrine.orm.quote_strategy.default')->end()
657661
->scalarNode('typed_field_mapper')->defaultValue('doctrine.orm.typed_field_mapper.default')->end()

src/DependencyInjection/DoctrineExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver as LegacyStaticPHPDriver;
3030
use Doctrine\ORM\Mapping\Embeddable;
3131
use Doctrine\ORM\Mapping\Entity;
32+
use Doctrine\ORM\Mapping\LegacyReflectionFields;
3233
use Doctrine\ORM\Mapping\MappedSuperclass;
3334
use Doctrine\ORM\Proxy\Autoloader;
3435
use Doctrine\ORM\Proxy\ProxyFactory;
@@ -78,6 +79,8 @@
7879
use function str_replace;
7980
use function trigger_deprecation;
8081

82+
use const PHP_VERSION_ID;
83+
8184
/**
8285
* DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
8386
*
@@ -698,6 +701,10 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
698701
'setIdentityGenerationPreferences' => $entityManager['identity_generation_preferences'],
699702
];
700703

704+
if (PHP_VERSION_ID >= 80400 && class_exists(LegacyReflectionFields::class)) {
705+
$methods['enableNativeLazyObjects'] = $entityManager['enable_native_lazy_objects'];
706+
}
707+
701708
if (isset($entityManager['fetch_mode_subselect_batch_size'])) {
702709
$methods['setEagerFetchBatchSize'] = $entityManager['fetch_mode_subselect_batch_size'];
703710
}

tests/DependencyInjection/AbstractDoctrineExtensionTestCase.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Doctrine\ORM\Mapping\ClassMetadata;
2121
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
2222
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
23+
use Doctrine\ORM\Mapping\LegacyReflectionFields;
2324
use Doctrine\ORM\Proxy\ProxyFactory;
2425
use Generator;
2526
use InvalidArgumentException;
@@ -55,6 +56,7 @@
5556
use function uniqid;
5657

5758
use const DIRECTORY_SEPARATOR;
59+
use const PHP_VERSION_ID;
5860

5961
abstract class AbstractDoctrineExtensionTestCase extends TestCase
6062
{
@@ -1433,6 +1435,86 @@ public function testDisableSchemaValidation(): void
14331435
$this->assertFalse($collectorDefinition->getArguments()[1]);
14341436
}
14351437

1438+
public function testNativeLazyObjectsWithoutConfig(): void
1439+
{
1440+
if (! interface_exists(EntityManagerInterface::class)) {
1441+
self::markTestSkipped('This test requires ORM');
1442+
}
1443+
1444+
if (! class_exists(LegacyReflectionFields::class)) {
1445+
self::markTestSkipped('This test requires ORM 3.4+');
1446+
}
1447+
1448+
if (PHP_VERSION_ID < 80400) {
1449+
self::markTestSkipped('This test requires PHP 8.4+');
1450+
}
1451+
1452+
$container = $this->loadContainer('orm_filters');
1453+
$entityManager = $container->get('doctrine.orm.entity_manager');
1454+
1455+
$this->assertFalse($entityManager->getConfiguration()->isNativeLazyObjectsEnabled());
1456+
}
1457+
1458+
public function testNativeLazyObjectsWithConfigTrue(): void
1459+
{
1460+
if (! interface_exists(EntityManagerInterface::class)) {
1461+
self::markTestSkipped('This test requires ORM');
1462+
}
1463+
1464+
if (! class_exists(LegacyReflectionFields::class)) {
1465+
self::markTestSkipped('This test requires ORM 3.4+');
1466+
}
1467+
1468+
if (PHP_VERSION_ID < 80400) {
1469+
self::markTestSkipped('This test requires PHP 8.4+');
1470+
}
1471+
1472+
$container = $this->loadContainer('orm_native_lazy_objects_enable');
1473+
$entityManager = $container->get('doctrine.orm.entity_manager');
1474+
1475+
$this->assertTrue($entityManager->getConfiguration()->isNativeLazyObjectsEnabled());
1476+
}
1477+
1478+
public function testNativeLazyObjectsWithConfigFalse(): void
1479+
{
1480+
if (! interface_exists(EntityManagerInterface::class)) {
1481+
self::markTestSkipped('This test requires ORM');
1482+
}
1483+
1484+
if (! class_exists(LegacyReflectionFields::class)) {
1485+
self::markTestSkipped('This test requires ORM 3.4+');
1486+
}
1487+
1488+
if (PHP_VERSION_ID < 80400) {
1489+
self::markTestSkipped('This test requires PHP 8.4+');
1490+
}
1491+
1492+
$container = $this->loadContainer('orm_native_lazy_objects_disable');
1493+
$entityManager = $container->get('doctrine.orm.entity_manager');
1494+
1495+
$this->assertFalse($entityManager->getConfiguration()->isNativeLazyObjectsEnabled());
1496+
}
1497+
1498+
public function testNativeLazyObjectsWithBadPHP(): void
1499+
{
1500+
if (! interface_exists(EntityManagerInterface::class)) {
1501+
self::markTestSkipped('This test requires ORM');
1502+
}
1503+
1504+
if (! class_exists(LegacyReflectionFields::class)) {
1505+
self::markTestSkipped('This test requires ORM 3.4+');
1506+
}
1507+
1508+
if (PHP_VERSION_ID >= 80400) {
1509+
self::markTestSkipped('This test requires PHP 8.3 or less');
1510+
}
1511+
1512+
$container = $this->loadContainer('orm_native_lazy_objects_enable');
1513+
$entityManager = $container->get('doctrine.orm.entity_manager');
1514+
1515+
$this->assertFalse($entityManager->getConfiguration()->isNativeLazyObjectsEnabled());
1516+
}
1517+
14361518
/** @param list<string> $bundles */
14371519
private function loadContainer(
14381520
string $fixture,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:srv="http://symfony.com/schema/dic/services"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
8+
9+
<config>
10+
<dbal default-connection="default">
11+
<connection name="default" dbname="db" server-version="8.0.31" />
12+
</dbal>
13+
14+
<orm enable-native-lazy-objects="false">
15+
</orm>
16+
</config>
17+
</srv:container>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:srv="http://symfony.com/schema/dic/services"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
8+
9+
<config>
10+
<dbal default-connection="default">
11+
<connection name="default" dbname="db" server-version="8.0.31" />
12+
</dbal>
13+
14+
<orm enable-native-lazy-objects="true">
15+
</orm>
16+
</config>
17+
</srv:container>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
doctrine:
2+
dbal:
3+
default_connection: default
4+
connections:
5+
default:
6+
dbname: db
7+
8+
orm:
9+
enable_native_lazy_objects: false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
doctrine:
2+
dbal:
3+
default_connection: default
4+
connections:
5+
default:
6+
dbname: db
7+
8+
orm:
9+
enable_native_lazy_objects: true

0 commit comments

Comments
 (0)