Download the PHP package aatis/dependency-injection without Composer
On this page you can find all versions of the php package aatis/dependency-injection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dependency-injection
Aatis DI
Installation
Usage
Requirements
Set the environment variable APP_ENV
to the name of the environment you want to use.
Create the container builder with the context of your app ($_SERVER
).
Exclude files
Precise the files that are not services.
Service config
You can manage in which environment your service must be loaded and the arguments to pass to the constructor.
You can also precise the class to use for the dependency when it is an interface.
Finally, you can give extra tags to any service.
environment and tags are optional
the key of an argument must have the same name as in the constructor
Interface into constructor
When an interface is requested into the constructor of a service, the DI will try to find a service implementing this interface into your app.
If multiple services implement the interface, the DI will pick the first one found or an already instancied service implementing the interface.
If you want to use a specific service, don't forget to declare it into the declaration of the service.
Otherwise if your want to use a specific service of the vendor, do the previous step and precise it into the includes_services
part of the config.
Env variable into constructor
You can request for a env variable directly into the constructor of a service.
the name of the variable must start with $_ and be followed by the env variable name in lowercase
Container uses
Get and Set
With the container, you can get and set any service / env variable you want with the methods get()
and set()
of the container.
However, to set a service, you must give an instance of the Service
class. You can create it with the ServiceFactory
service.
APP_ENV_VARNAME must start with "APP"
Get by tag(s)
You can get services by tag(s) with the getByTag()
and getByTags
method of the Container
.
You can also request to get Service
instances instead of the instance of the services with precise true
in the second argument.
Get by interface(s)
You can get services by interface(s) with the getByInterface()
and getByInterfaces
method of the Container
.
Like tags, you can also request to get Service
instances instead of the instance of the services with precise true
in the second argument.
ServiceInstanciator
You can use the ServiceInstanciator
service and the setInstance()
method of the Service
class to instanciate a service into the container.
You can choose between two methods to instanciate a service. For the first one, you must inform the arguments to pass to the constructor into the config. For the second one, you must create the instance yourself.
$service = $container->get(ServiceFactory::class)->create('Namespace\To\The\Service');
// Method 1
$instance = $container->get(ServiceInstanciator::class)->instanciate($service)
// Method 2
$instance = new Namespace\To\The\Service($arg1, $arg2, ...);
$service->setInstance($instance);
$container->set('Namespace\To\The\Service', $service);