Skip to content

Commit 6610af2

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 dbfc0e5 commit 6610af2

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

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
@@ -632,6 +632,9 @@ public static function init(): void {
632632
exit();
633633
}
634634

635+
// Enable lazy loading if activated
636+
\OC\AppFramework\Utility\SimpleContainer::$useLazyObjects = (bool)self::$config->getValue('enable_lazy_objects');
637+
635638
// setup the basic server
636639
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
637640
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)