Library
Class

Library\ServiceContainer\ServiceContainer

class ServiceContainer implements ServiceContainerInterface

A simple service container with constructors

Methods

$this init(array $initial_services = array(), array $services_providers = array(), array $services_protected = array())

Initialize the service container system

$this setProvider(string $name, array $provider)

Define a service constructor like array( name , callback , protected ) or a closure

mixed getProvider(string $name)

Get a service constructor if it exists

bool hasProvider(string $name)

Test if a constructor exists for a service

$this setProtected(string $name)

Define a service as protected

bool isProtected(string $name)

Test if a service is protected

$this setService(string $name, object|callable $callback, bool $protected = false)

Register a new service called $name declared as NOT protected by default

mixed getService(string $name, array $arguments = array(), int $failure = self::FAIL_WITH_ERROR)

Get a service called $name throwing an error by default if it does not exist yet and can not be created

mixed hasService(string $name)

Test if a service exists in the container

mixed unsetService(string $name)

Unset a service if it is not protected

Details

at line 73
public $this init(array $initial_services = array(), array $services_providers = array(), array $services_protected = array())

Initialize the service container system

Parameters

array $initial_services
array $services_providers
array $services_protected

Return Value

$this

at line 111
public $this setProvider(string $name, array $provider)

Define a service constructor like array( name , callback , protected ) or a closure

Parameters

string $name
array $provider A service array constructor like array( name , callback , protected ) callable $provider A callback as a closure that must return the service object: function ($name, $arguments) {} ServiceProviderInterface $provider A \Library\ServiceContainer\ServiceProviderInterface instance

Return Value

$this

at line 126
public mixed getProvider(string $name)

Get a service constructor if it exists

Parameters

string $name

Return Value

mixed

at line 138
public bool hasProvider(string $name)

Test if a constructor exists for a service

Parameters

string $name

Return Value

bool

at line 149
public $this setProtected(string $name)

Define a service as protected

Parameters

string $name

Return Value

$this

at line 161
public bool isProtected(string $name)

Test if a service is protected

Parameters

string $name

Return Value

bool

at line 178
public $this setService(string $name, object|callable $callback, bool $protected = false)

Register a new service called $name declared as NOT protected by default

Parameters

string $name
object|callable $callback
bool $protected

Return Value

$this

Exceptions

ErrorException

at line 203
public mixed getService(string $name, array $arguments = array(), int $failure = self::FAIL_WITH_ERROR)

Get a service called $name throwing an error by default if it does not exist yet and can not be created

Parameters

string $name
array $arguments
int $failure

Return Value

mixed

Exceptions

ErrorException

at line 227
public mixed hasService(string $name)

Test if a service exists in the container

Parameters

string $name

Return Value

mixed

at line 239
public mixed unsetService(string $name)

Unset a service if it is not protected

Parameters

string $name

Return Value

mixed

Exceptions

ErrorException