Skip to main content

Introspection

The Dirthara\Container\Contract\Inspector interface answers what the container holds and why it resolves an entry the way it does, such as for a debug command or an error page. Nothing it does builds an entry.

List the registered entries​

registered() returns the identifiers registered with bind(), singleton(), scoped(), instance(), and scopedInstance(), sorted, including the container's own entries such as Psr\Container\ContainerInterface. An entry that only comes from an attribute or from autowiring is not registered, so it is not listed.

foreach ($container->registered() as $id) {
echo $id, PHP_EOL;
}

Describe an entry​

describe() returns an EntryDescription of how the container would resolve an identifier, or null when it has no entry for it:

$description = $container->describe(UserRepository::class);

$description->source; // EntrySource::Attribute
$description->lifetime; // Lifetime::Singleton
$description->concrete; // DatabaseUserRepository::class
$description->decorators; // [CachingUserRepository::class]
PropertyTypeMeaning
idstringThe identifier that was described.
sourceEntrySourceWhere the entry comes from; see below.
lifetime?LifetimeLifetime::Transient, Lifetime::Singleton, or Lifetime::Scoped, and null for an instance or a delegate.
concrete?stringThe entry or class it resolves to, and null for a factory, an instance, or a delegate.
factoryboolWhether a factory builds it.
lazyboolWhether the identifier is a class the container builds lazily.
tagslist<string>The tags it has, in the order they were first added.
decoratorslist<class-string>The decorators its #[DecoratedBy] attributes name.
extendersintHow many extenders are registered for it.

source follows the order in which the container looks an identifier up:

EntrySourceThe entry is
ScopedInstanceA value registered with scopedInstance() for the current scope.
InstanceA value registered with instance().
BindingRegistered with bind(), singleton(), or scoped().
DelegateFound in a delegate container.
AttributeDeclared with #[BoundTo], #[Singleton], or #[Scoped] on the type.
AutowiredAn instantiable class nothing is registered for.

describe() reads a type's attributes, so an attribute used incorrectly throws the same InvalidAttributeException as resolving the type would. Both enums are string-backed, with values such as 'scoped-instance' and 'singleton', for output that has to be text.