Library
Interface

Library\ServiceContainer\ServiceContainerInterface

interface ServiceContainerInterface

A simple service container interface

The "register" system will store a callback in the registry with a specific name to use when trying to get a service. The getter will throw an error by default if the service does not exist, except when using the FAIL_GRACEFULLY constant. Any service is overwritable by default but can be not using the $protected = true register argument.

Constants

FAIL_GRACEFULLY

Use this constant to NOT throw error when trying to get an unknown service

FAIL_WITH_ERROR

Use this constant to throw error when trying to get an unknown service

This is the default behavior.

Methods

mixed setProvider(string $name, array $provider)

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

mixed 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 61
public mixed 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

mixed

at line 71
public mixed 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

mixed

at line 81
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

at line 89
public mixed hasService(string $name)

Test if a service exists in the container

Parameters

string $name

Return Value

mixed

at line 97
public mixed unsetService(string $name)

Unset a service if it is not protected

Parameters

string $name

Return Value

mixed