Skip to content

Commit 80f76dd

Browse files
committed
proxy WIP
1 parent 9b9bfb4 commit 80f76dd

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: src/DI/Container.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class Container
3232
/** @var array[] type => (high, low, no) => services */
3333
protected array $wiring = [];
3434

35+
/** @var object[] service name => instance */
36+
protected array $lazy = [];
37+
3538
/** @var object[] service name => instance */
3639
private array $instances = [];
3740

@@ -215,7 +218,12 @@ public function createService(string $name): object
215218
if ($callback = ($this->factories[$name] ?? null)) {
216219
$service = $this->preventDeadLock($name, fn() => $callback());
217220
} elseif (isset($this->methods[$method])) {
218-
$service = $this->preventDeadLock($name, fn() => $this->$method());
221+
if (isset($this->lazy[$name])) {
222+
$rc = new \ReflectionClass($this->lazy[$name]);
223+
$service = $rc->newLazyProxy($this->$method(...));
224+
} else {
225+
$service = $this->preventDeadLock($name, fn() => $this->$method());
226+
}
219227
} else {
220228
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
221229
}

Diff for: src/DI/ContainerBuilder.php

+6
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ public function exportMeta(): array
367367
$all[$class][] = $name;
368368
}
369369
}
370+
371+
if ($def instanceof Definitions\ServiceDefinition && PHP_VERSION_ID >= 80400) {
372+
if (is_string($def->getEntity())) {
373+
$meta['lazy'][$name] = $def->getEntity();
374+
}
375+
}
370376
}
371377

372378
[$low, $high] = $this->autowiring->getClassList();

0 commit comments

Comments
 (0)