Skip to content

Commit

Permalink
proxy WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 25, 2024
1 parent 9b9bfb4 commit 933ce25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Container

/** @var array<string, \Closure> service name => \Closure */
private array $factories = [];
private array $lazy = [];


public function __construct(array $params = [])
Expand Down Expand Up @@ -215,7 +216,12 @@ public function createService(string $name): object
if ($callback = ($this->factories[$name] ?? null)) {
$service = $this->preventDeadLock($name, fn() => $callback());
} elseif (isset($this->methods[$method])) {
$service = $this->preventDeadLock($name, fn() => $this->$method());
if (isset($this->lazy[$name])) {
$rc = new \ReflectionClass($this->lazy[$name]);
$service = $rc->newLazyProxy($this->$method(...));
} else {
$service = $this->preventDeadLock($name, fn() => $this->$method());
}
} else {
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
}
Expand Down
6 changes: 6 additions & 0 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ public function exportMeta(): array
$all[$class][] = $name;
}
}

if ($def instanceof Definitions\ServiceDefinition && PHP_VERSION_ID >= 80400) {
if (is_string($def->getEntity())) {
$meta['lazy'][$name] = $def->getEntity();
}
}
}

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

0 comments on commit 933ce25

Please sign in to comment.