Skip to content
docs

DIContainerInterface

Dependency Injection Container Interface. Provides service registration, retrieval, and auto-wiring capabilities. Extends the PSR-11 `ContainerInterface` so implementations are usable anywhere a standard PSR-11 container is expected.

DIContainerInterface::getContainer()

abstract public function getContainer(): Psr\Container\ContainerInterface

Returns the underlying service container.

DIContainerInterface::registerService()

abstract public function registerService(string $id, object|string $classOrInstance): void

Registers a service under the given identifier. Accepts either a class name (registered for later auto-wiring) or an already-built instance (set directly on the container). An explicit registration made here takes precedence over on-demand {@see resolve()} autowiring for the same identifier. This is the method a {@see \Milpa\app\Attributes\RegisterService} scanner calls after instantiating an annotated class.

Parameters

Parámetros de registerService()
NombreTipoDescripción
$idstring
$classOrInstanceobject|string

DIContainerInterface::compileContainer()

abstract public function compileContainer(): void

Compiles the underlying container, freezing its service definitions.

DIContainerInterface::get()

abstract public function get(string $id): ?mixed

Get a service from the container. If the service is not registered but the class exists, it will be auto-resolved and registered as a singleton.

Parameters

Parámetros de get()
NombreTipoDescripción
$idstringService identifier (usually FQCN)

Returns

Service instance

Throws

NotFoundExceptionInterface No entry was found for this identifier.

\Psr\Container\ContainerExceptionInterface Auto-resolution of the entry failed.

DIContainerInterface::has()

abstract public function has(string $id): bool

Checks whether an identifier is resolvable: either already registered, or an existing class that can be auto-wired.

Parameters

Parámetros de has()
NombreTipoDescripción
$idstring

DIContainerInterface::resolve()

abstract public function resolve(string $className, bool $singleton = true): ?mixed

Resolve a class with auto-wiring. Instantiates the class resolving constructor dependencies from the container. If $singleton is true, registers the instance for future use.

Parameters

Parámetros de resolve()
NombreTipoDescripción
$classNamestringFully qualified class name
$singletonboolRegister as singleton (default: true)

Returns

Class instance

Throws

ContainerExceptionInterface Error while resolving the entry.

DIContainerInterface::tryGet()

abstract public function tryGet(string $id): ?mixed

Get a service or return null if not available. Unlike get(), this won't throw or auto-resolve.

Parameters

Parámetros de tryGet()
NombreTipoDescripción
$idstringService identifier

Returns

Service instance or null