Skip to content

Commit

Permalink
Suggestion for lazy service (#355)
Browse files Browse the repository at this point in the history
* improve

* Fix psalm
  • Loading branch information
vjik authored Apr 16, 2024
1 parent fedff5e commit 51fefa2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<InvalidCatch>
<errorLevel type="suppress">
<referencedClass name="Psr\Container\NotFoundExceptionInterface" />
Expand Down
48 changes: 37 additions & 11 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

/**
* Container implements a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) container.
*
* @psalm-import-type MethodOrPropertyItem from ArrayDefinition
*/
final class Container implements ContainerInterface
{
Expand Down Expand Up @@ -202,7 +204,6 @@ public function get(string $id)
*/
private function addDefinition(string $id, mixed $definition): void
{
/** @var mixed $definition */
[$definition, $meta] = DefinitionParser::parse($definition);
if ($this->validate) {
$this->validateDefinition($definition, $id);
Expand Down Expand Up @@ -630,11 +631,42 @@ private function buildProvider(mixed $provider): ServiceProviderInterface

private function decorateLazy(string $id, mixed $definition): DefinitionInterface
{
if (class_exists($id) || interface_exists($id)) {
$class = $id;
} elseif (is_array($definition) && array_key_exists(ArrayDefinition::CLASS_NAME, $definition)) {
$class = (string) $definition[ArrayDefinition::CLASS_NAME];
$class = class_exists($id) || interface_exists($id) ? $id : null;

if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) {
/**
* @psalm-var array{
* class: class-string|null,
* '__construct()': array,
* methodsAndProperties: array<string, MethodOrPropertyItem>
* } $definition
*/
if (empty($class)) {
$class = $definition[ArrayDefinition::CLASS_NAME];
}
$this->checkClassOnNullForLazyService($class, $id, $definition);
$preparedDefinition = ArrayDefinition::fromPreparedData(
$definition[ArrayDefinition::CLASS_NAME] ?? $class,
$definition[ArrayDefinition::CONSTRUCTOR],
$definition['methodsAndProperties'],
);
} else {
$this->checkClassOnNullForLazyService($class, $id, $definition);
$preparedDefinition = $definition;
}



return new LazyDefinition($preparedDefinition, $class);
}

/**
* @psalm-param class-string|null $class
* @psalm-assert class-string $class
*/
private function checkClassOnNullForLazyService(?string $class, string $id, mixed $definition): void
{
if (empty($class)) {
throw new InvalidConfigException(
sprintf(
'Invalid definition: lazy services are only available with array definitions or references. Got type "%s" for definition ID: "%s"',
Expand All @@ -643,11 +675,5 @@ private function decorateLazy(string $id, mixed $definition): DefinitionInterfac
)
);
}

/**
* @var class-string $class
*/

return new LazyDefinition($definition, $class);
}
}

0 comments on commit 51fefa2

Please sign in to comment.