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\ContainerInterfaceReturns the underlying service container.
DIContainerInterface::registerService()
abstract public function registerService(string $id, object|string $classOrInstance): voidRegisters 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
| Nombre | Tipo | Descripción |
|---|---|---|
| $id | string | |
| $classOrInstance | object|string |
DIContainerInterface::compileContainer()
abstract public function compileContainer(): voidCompiles the underlying container, freezing its service definitions.
DIContainerInterface::get()
abstract public function get(string $id): ?mixedGet 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
| Nombre | Tipo | Descripción |
|---|---|---|
| $id | string | Service 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): boolChecks whether an identifier is resolvable: either already registered, or an existing class that can be auto-wired.
Parameters
| Nombre | Tipo | Descripción |
|---|---|---|
| $id | string |
DIContainerInterface::resolve()
abstract public function resolve(string $className, bool $singleton = true): ?mixedResolve 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
| Nombre | Tipo | Descripción |
|---|---|---|
| $className | string | Fully qualified class name |
| $singleton | bool | Register as singleton (default: true) |
Returns
Class instance
Throws
ContainerExceptionInterface Error while resolving the entry.
DIContainerInterface::tryGet()
abstract public function tryGet(string $id): ?mixedGet a service or return null if not available. Unlike get(), this won't throw or auto-resolve.
Parameters
| Nombre | Tipo | Descripción |
|---|---|---|
| $id | string | Service identifier |
Returns
Service instance or null