Skip to content

InstalledComponents

The components a ComponentInstaller built, keyed by role.

The point of handing these back rather than writing them into their owner is that the owner then assigns them itself, by name and with a declared type:

php $this->routing = $installed->need('routing', Routing::class);

That line is checked statically and breaks at compile time if the property is renamed or retyped. The compiled configuration it ultimately came from names only the role, and cannot reach a property at all.

final readonly class InstalledComponents

Since4.0.0
SourceConfig/Factory/InstalledComponents.php

public function __construct(array<string, object> $components): mixed

Keyed by role.

ParameterTypeDescription
$componentsarray``<``string``, ``object``>Keyed by role.

Returns mixed

MethodDescription
has(string $role): boolWhether a role was built.
need(string $role, class-string<T> $expected): TThe component for a required role.
optional(string $role, class-string<T> $expected): ?TThe component for an optional role, or null when the configuration declares none.
roles(): array<int, string>The roles that were built.

public function has(string $role): bool

Whether a role was built.

ParameterTypeDescription
$rolestring

Returns bool

public function need(string $role, class-string<T> $expected): T

The component for a required role.

ParameterTypeDescription
$rolestring
$expectedclass-string``<``T``>

Returns T

ThrowsWhen
ConfigurationExceptionWhen the role was not built, or was built as something else. Both mean the configuration and this code disagree about what a role is, which is worth saying rather than discovering through a type error on assignment.

public function optional(string $role, class-string<T> $expected): ?T

The component for an optional role, or null when the configuration declares none.

ParameterTypeDescription
$rolestring
$expectedclass-string``<``T``>

Returns ?``T

ThrowsWhen
ConfigurationExceptionWhen the role was built as something else. Absent is fine; present and wrong is still a configuration error.

public function roles(): array<int, string>

The roles that were built.

Returns array``<``int``, ``string``>