Skip to content

Commit 2eed6d3

Browse files
committed
feat: Add a configuration toggle for lazy objects in DI
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent a10182f commit 2eed6d3

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

config/config.sample.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,4 +2743,12 @@
27432743
* Defaults to true.
27442744
*/
27452745
'files.trash.delete' => true,
2746+
2747+
/**
2748+
* Enable lazy objects feature from PHP 8.4 to be used in the Dependency Injection.
2749+
* Should improve performances by avoiding buiding unused objects.
2750+
*
2751+
* Defaults to false.
2752+
*/
2753+
'enable_lazy_objects' => false,
27462754
];

lib/base.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ public static function init(): void {
618618
}
619619
$loaderEnd = microtime(true);
620620

621+
// Enable lazy loading if activated
622+
\OC\AppFramework\Utility\SimpleContainer::$useLazyObjects = (bool)self::$config->getValue('enable_lazy_objects');
623+
621624
// setup the basic server
622625
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
623626
self::$server->boot();

lib/private/AppFramework/Utility/SimpleContainer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* SimpleContainer is a simple implementation of a container on basis of Pimple
2525
*/
2626
class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
27+
public static bool $useLazyObjects = false;
28+
2729
private Container $container;
2830

2931
public function __construct() {
@@ -58,7 +60,7 @@ private function buildClass(ReflectionClass $class): object {
5860
/* No constructor, return a instance directly */
5961
return $class->newInstance();
6062
}
61-
if (PHP_VERSION_ID >= 80400) {
63+
if (PHP_VERSION_ID >= 80400 && self::$useLazyObjects) {
6264
/* For PHP>=8.4, use a lazy ghost to delay constructor and dependency resolving */
6365
/** @psalm-suppress UndefinedMethod */
6466
return $class->newLazyGhost(function (object $object) use ($constructor): void {

0 commit comments

Comments
 (0)