Darken\Service\ContainerService

class

A lightweight service container for managing dependencies. This class allows for registering and resolving services or objects, enabling dependency injection and improved application modularity.

Methods

Method Details

public array definitions (bool $system) ContainerService.php#27
$system bool
Return array
public self register (string|object|callable $objectOrName, object|array|null $objectOrParams, bool $system) ContainerService.php#51

Registers a container by name or object. Usage examples:

$container->register(new FooBar());
$container->register(FooBar::class);
$container->register(FooBarInterface::class, new FooBar());
$container->register(FooBarInterface::class, ['param1' => 'value1', 'param2' => 'value2']);
$container->register(FooBarInterface::class, fn() => new FooBar());
$objectOrName string|object|callable
$objectOrParams object|array|null
$system bool
Return self
public object resolve (string $name) ContainerService.php#78

Resolves a container by its name. If the container is callable, it will be executed and replaced with its result. If it is an array, it will be used to instantiate an object of the specified class.

$name string
Return object
public object ensure (object|string|array $name, string|null $instanceOf) ContainerService.php#109

Ensure input is an object, resolving it if it's a string.

  1. If the input is an object, return it.
  2. If the input is a string and the container is registered, resolve it.
  3. If the input is a string and the container is not registered, create it (but don't register it).
$name object|string|array
$instanceOf string|null
Return object
public bool has (string $name) ContainerService.php#134

Checks if a container is registered.

$name string
Return bool
public self remove (string $name) ContainerService.php#142

Removes a container from the registry.

$name string
Return self
public object create (string $className, array $params) ContainerService.php#155

Creates an object of the specified class with the provided parameters. This method uses reflection to handle classes with constructors that require parameters.

$className string
$params array
Return object