Skip to content

Commit 751d6f5

Browse files
committed
Make native proxies default for 8.4+ users and update tests accordingly.
1 parent c1a56f9 commit 751d6f5

13 files changed

+169
-180
lines changed

src/ContainerBuilder.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ class ContainerBuilder
6060
*/
6161
private ?string $proxyDirectory = null;
6262

63-
/**
64-
* If true, will enable the PHP 8.4+ native proxy generator.
65-
*/
66-
private bool $useNativeLazyObjects = false;
67-
6863
/**
6964
* If PHP-DI is wrapped in another container, this references the wrapper.
7065
*/
@@ -139,11 +134,9 @@ public function build()
139134
$source = new SourceCache($source, $this->sourceCacheNamespace);
140135
}
141136

142-
if ($this->useNativeLazyObjects) {
143-
$proxyFactory = new NativeProxyFactory();
144-
} else {
145-
$proxyFactory = new ProxyFactory($this->proxyDirectory);
146-
}
137+
$proxyFactory = (\PHP_VERSION_ID >= 80400)
138+
? new NativeProxyFactory()
139+
: new ProxyFactory($this->proxyDirectory);
147140

148141
$this->locked = true;
149142

@@ -262,30 +255,6 @@ public function writeProxiesToFile(bool $writeToFile, ?string $proxyDirectory =
262255
return $this;
263256
}
264257

265-
/**
266-
* Use PHP 8.4+'s native lazy object generation for lazy-loading proxies.
267-
*
268-
* @see https://php-di.org/doc/lazy-injection.html
269-
*
270-
* @param bool $useNativeLazyObjects If true, prefer native lazy objects if available.
271-
*/
272-
public function useNativeLazyObjects(bool $useNativeLazyObjects) : self
273-
{
274-
$this->ensureNotLocked();
275-
276-
if (\PHP_VERSION_ID < 80400 && $useNativeLazyObjects) {
277-
throw new \LogicException('Lazy loading proxies require PHP 8.4 or higher.');
278-
}
279-
280-
$this->useNativeLazyObjects = $useNativeLazyObjects;
281-
282-
if ($useNativeLazyObjects) {
283-
$this->proxyDirectory = null;
284-
}
285-
286-
return $this;
287-
}
288-
289258
/**
290259
* If PHP-DI's container is wrapped by another container, we can
291260
* set this so that PHP-DI will use the wrapper rather than itself for building objects.

src/Proxy/NativeProxyFactory.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public function createProxy(string $className, \Closure $createFunction) : objec
2828

2929
$reflector = new \ReflectionClass($className);
3030

31-
return $reflector->newLazyProxy(
32-
function () use ($createFunction) {
33-
return $createFunction();
34-
}
35-
);
31+
return $reflector->newLazyProxy($createFunction);
3632
}
3733

3834
public function generateProxyClass(string $className) : void

tests/IntegrationTest/CompiledContainerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DI\Test\IntegrationTest;
66

7+
use PHPUnit\Framework\Attributes\RequiresPhp;
78
use function DI\autowire;
89
use DI\ContainerBuilder;
910
use function DI\create;
@@ -170,6 +171,7 @@ public function the_compiled_container_can_extend_a_custom_class()
170171
* @test
171172
*/
172173
#[\PHPUnit\Framework\Attributes\Test]
174+
#[RequiresPhp('< 8.4')]
173175
public function proxy_classes_can_be_pregenerated_at_compile_time()
174176
{
175177
$builder = new ContainerBuilder;

tests/IntegrationTest/ContainerInjectOnTest.php

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,23 @@ public function test_inject_on_object_using_attributes(ContainerBuilder $builder
5656
self::assertInstanceOf(Implementation1::class, $obj->property2);
5757
self::assertInstanceOf(Class2::class, $obj->property3);
5858
self::assertEquals('bar', $obj->property4);
59+
5960
// Lazy injection
60-
/** @var LazyDependency|LazyLoadingInterface $proxy */
61-
$proxy = $obj->property5;
62-
self::assertInstanceOf(LazyDependency::class, $proxy);
63-
self::assertInstanceOf(LazyLoadingInterface::class, $proxy);
64-
self::assertFalse($proxy->isProxyInitialized());
61+
self::assertInstanceOf(LazyDependency::class, $obj->property5);
62+
63+
if (PHP_VERSION_ID >= 80400) {
64+
self::assertTrue(
65+
new \ReflectionClass(LazyDependency::class)->isUninitializedLazyObject(
66+
$obj->property5
67+
)
68+
);
69+
} else {
70+
/** @var LazyDependency|LazyLoadingInterface $proxy */
71+
$proxy = $obj->property5;
72+
73+
self::assertInstanceOf(LazyLoadingInterface::class, $proxy);
74+
self::assertFalse($proxy->isProxyInitialized());
75+
}
6576

6677
// Test method injections
6778

@@ -72,13 +83,25 @@ public function test_inject_on_object_using_attributes(ContainerBuilder $builder
7283
// Method 3 (defining parameters with the attribute)
7384
self::assertInstanceOf(Class2::class, $obj->method3Param1);
7485
self::assertEquals('bar', $obj->method3Param2);
86+
7587
// Method 4 (lazy)
7688
self::assertInstanceOf(LazyDependency::class, $obj->method4Param1);
77-
self::assertInstanceOf(LazyLoadingInterface::class, $obj->method4Param1);
78-
// Lazy injection
79-
/** @var LazyDependency|LazyLoadingInterface $proxy */
80-
$proxy = $obj->method4Param1;
81-
self::assertFalse($proxy->isProxyInitialized());
89+
90+
if (PHP_VERSION_ID >= 80400) {
91+
self::assertTrue(
92+
new \ReflectionClass(LazyDependency::class)->isUninitializedLazyObject(
93+
$obj->method4Param1
94+
)
95+
);
96+
} else {
97+
self::assertInstanceOf(LazyLoadingInterface::class, $obj->method4Param1);
98+
99+
// Lazy injection
100+
/** @var LazyDependency|LazyLoadingInterface $proxy */
101+
$proxy = $obj->method4Param1;
102+
103+
self::assertFalse($proxy->isProxyInitialized());
104+
}
82105
}
83106

84107
/**
@@ -127,12 +150,22 @@ public function test_inject_on_object_using_config(ContainerBuilder $builder)
127150
self::assertInstanceOf(Implementation1::class, $obj->property2);
128151
self::assertInstanceOf(Class2::class, $obj->property3);
129152
self::assertEquals('bar', $obj->property4);
130-
// Lazy injection
131-
/** @var LazyDependency|LazyLoadingInterface $proxy */
132-
$proxy = $obj->property5;
133-
self::assertInstanceOf(LazyDependency::class, $proxy);
134-
self::assertInstanceOf(LazyLoadingInterface::class, $proxy);
135-
self::assertFalse($proxy->isProxyInitialized());
153+
154+
self::assertInstanceOf(LazyDependency::class, $obj->property5);
155+
156+
if (PHP_VERSION_ID >= 80400) {
157+
self::assertTrue(
158+
new \ReflectionClass(LazyDependency::class)->isUninitializedLazyObject(
159+
$obj->property5
160+
)
161+
);
162+
} else {
163+
/** @var LazyDependency|LazyLoadingInterface $proxy */
164+
$proxy = $obj->property5;
165+
166+
self::assertInstanceOf(LazyLoadingInterface::class, $proxy);
167+
self::assertFalse($proxy->isProxyInitialized());
168+
}
136169

137170
// Test method injections
138171

@@ -143,13 +176,23 @@ public function test_inject_on_object_using_config(ContainerBuilder $builder)
143176
// Method 3 (defining parameters with the attribute)
144177
self::assertInstanceOf(Class2::class, $obj->method3Param1);
145178
self::assertEquals('bar', $obj->method3Param2);
179+
146180
// Method 4 (lazy)
147181
self::assertInstanceOf(LazyDependency::class, $obj->method4Param1);
148-
self::assertInstanceOf(LazyLoadingInterface::class, $obj->method4Param1);
149-
// Lazy injection
150-
/** @var LazyDependency|LazyLoadingInterface $proxy */
151-
$proxy = $obj->method4Param1;
152-
self::assertFalse($proxy->isProxyInitialized());
182+
183+
if (PHP_VERSION_ID >= 80400) {
184+
self::assertTrue(
185+
new \ReflectionClass(LazyDependency::class)->isUninitializedLazyObject(
186+
$obj->method4Param1
187+
)
188+
);
189+
} else {
190+
/** @var LazyDependency|LazyLoadingInterface $proxy */
191+
$proxy = $obj->method4Param1;
192+
193+
self::assertInstanceOf(LazyLoadingInterface::class, $proxy);
194+
self::assertFalse($proxy->isProxyInitialized());
195+
}
153196
}
154197

155198
/**

tests/IntegrationTest/Definitions/AttributeTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ public function test_constructor_injection(ContainerBuilder $builder)
6262
self::assertNull($object->typedOptionalValueDefaultNull);
6363
self::assertEquals('bar', $object->value);
6464
self::assertInstanceOf(\stdClass::class, $object->lazyService);
65-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
66-
self::assertFalse($object->lazyService->isProxyInitialized());
65+
66+
if (PHP_VERSION_ID < 80400) {
67+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
68+
self::assertFalse($object->lazyService->isProxyInitialized());
69+
}
70+
6771
self::assertSame($container->get('attribute'), $object->attribute);
6872
self::assertEquals('hello', $object->optionalValue);
6973
}
@@ -101,8 +105,11 @@ public function test_property_injection(ContainerBuilder $builder)
101105
self::assertEquals('bar', $object->value2);
102106
self::assertInstanceOf(\stdClass::class, $object->entry);
103107
self::assertInstanceOf(\stdClass::class, $object->lazyService);
104-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
105-
self::assertFalse($object->lazyService->isProxyInitialized());
108+
109+
if (PHP_VERSION_ID < 80400) {
110+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
111+
self::assertFalse($object->lazyService->isProxyInitialized());
112+
}
106113
}
107114

108115
/**
@@ -126,8 +133,12 @@ public function test_method_injection(ContainerBuilder $builder)
126133
self::assertNull($object->typedOptionalValueDefaultNull);
127134
self::assertEquals('bar', $object->value);
128135
self::assertInstanceOf(\stdClass::class, $object->lazyService);
129-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
130-
self::assertFalse($object->lazyService->isProxyInitialized());
136+
137+
if (PHP_VERSION_ID < 80400) {
138+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
139+
self::assertFalse($object->lazyService->isProxyInitialized());
140+
}
141+
131142
self::assertSame($container->get('attribute'), $object->attribute);
132143
self::assertEquals('hello', $object->optionalValue);
133144
}

tests/IntegrationTest/Definitions/AutowireDefinitionTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ public function test_constructor_injection(ContainerBuilder $builder)
6464
self::assertNull($object->typedOptionalValue);
6565
self::assertEquals('bar', $object->value);
6666
self::assertInstanceOf(LazyService::class, $object->lazyService);
67-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
68-
self::assertFalse($object->lazyService->isProxyInitialized());
67+
68+
if (PHP_VERSION_ID < 80400) {
69+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
70+
self::assertFalse($object->lazyService->isProxyInitialized());
71+
}
72+
6973
self::assertNull($object->unknownTypedAndOptional);
7074
self::assertEquals('hello', $object->optionalValue);
7175
}
@@ -358,10 +362,17 @@ public function test_autowire_lazy_object(ContainerBuilder $builder)
358362

359363
$this->assertEntryIsCompiled($container, NullableConstructorParameter::class);
360364
self::assertInstanceOf(NullableConstructorParameter::class, $object);
361-
self::assertInstanceOf(LazyLoadingInterface::class, $object);
362-
self::assertFalse($object->isProxyInitialized());
365+
366+
if (PHP_VERSION_ID < 80400) {
367+
self::assertInstanceOf(LazyLoadingInterface::class, $object);
368+
self::assertFalse($object->isProxyInitialized());
369+
}
370+
363371
self::assertEquals('bar', $object->bar);
364-
self::assertTrue($object->isProxyInitialized());
372+
373+
if (PHP_VERSION_ID < 80400) {
374+
self::assertTrue($object->isProxyInitialized());
375+
}
365376
}
366377

367378
/**

tests/IntegrationTest/Definitions/CreateDefinitionTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ public function test_constructor_injection(ContainerBuilder $builder)
8585
self::assertEquals(new \stdClass, $object->typedValue);
8686
self::assertEquals(new \stdClass, $object->typedOptionalValue);
8787
self::assertInstanceOf(\stdClass::class, $object->lazyService);
88-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
89-
self::assertFalse($object->lazyService->isProxyInitialized());
88+
89+
if (PHP_VERSION_ID < 80400) {
90+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
91+
self::assertFalse($object->lazyService->isProxyInitialized());
92+
}
93+
9094
self::assertEquals('hello', $object->optionalValue);
9195
}
9296

@@ -114,8 +118,11 @@ public function test_property_injection(ContainerBuilder $builder)
114118
self::assertEquals('foo', $object->value);
115119
self::assertEquals('bar', $object->entry);
116120
self::assertInstanceOf(\stdClass::class, $object->lazyService);
117-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
118-
self::assertFalse($object->lazyService->isProxyInitialized());
121+
122+
if (PHP_VERSION_ID < 80400) {
123+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
124+
self::assertFalse($object->lazyService->isProxyInitialized());
125+
}
119126
}
120127

121128
/**
@@ -146,8 +153,12 @@ public function test_method_injection(ContainerBuilder $builder)
146153
self::assertEquals(new \stdClass, $object->typedValue);
147154
self::assertEquals(new \stdClass, $object->typedOptionalValue);
148155
self::assertInstanceOf(\stdClass::class, $object->lazyService);
149-
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
150-
self::assertFalse($object->lazyService->isProxyInitialized());
156+
157+
if (PHP_VERSION_ID < 80400) {
158+
self::assertInstanceOf(LazyLoadingInterface::class, $object->lazyService);
159+
self::assertFalse($object->lazyService->isProxyInitialized());
160+
}
161+
151162
self::assertEquals('hello', $object->optionalValue);
152163
}
153164

@@ -249,10 +260,17 @@ public function test_create_lazy_object(ContainerBuilder $builder)
249260

250261
self::assertEntryIsCompiled($container, Property::class);
251262
self::assertInstanceOf(Property::class, $object);
252-
self::assertInstanceOf(LazyLoadingInterface::class, $object);
253-
self::assertFalse($object->isProxyInitialized());
263+
264+
if (PHP_VERSION_ID < 80400) {
265+
self::assertInstanceOf(LazyLoadingInterface::class, $object);
266+
self::assertFalse($object->isProxyInitialized());
267+
}
268+
254269
self::assertEquals('bar', $object->foo);
255-
self::assertTrue($object->isProxyInitialized());
270+
271+
if (PHP_VERSION_ID < 80400) {
272+
self::assertTrue($object->isProxyInitialized());
273+
}
256274
}
257275

258276
/**

0 commit comments

Comments
 (0)