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 Dependency Injection
Installation
Dependencies
aatis/parameter-bag
(https://github.com/BatMaxou/aatis-parameter-bag)aatis/tag
(https://github.com/BatMaxou/aatis-tag)
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.
Finally, you can give extra tags to any service.
[!NOTE] The key of an argument must have the same name as in the constructor
[!NOTE] Tags have priority set to 0 by default. You can set it to any number you want. Services will be sorted by highest priority first when the
Container
return multiple services.[!NOTE] It is also possible to define a configuration for an abstract class. This configuration will be used for all the services extending this class, and will be merged with the configuration of the service itself if provided.
Interface into constructor
When an interface is requested into the constructor of a service, the Container
will try to find a service implementing this interface into your app.
[!NOTE] If multiple services implement the interface, the
Container
will pick the one with the highest priority. If many services implementing the interface share the highest priority, theContainer
will priorise an already instancied service. Otherwise, it will pick the first one found.
If you want to use a specific service, don't forget to declare it into the configuration of the service.
[!WARNING] 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.
[!NOTE] 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 (prefixed by @_
) you want with the methods get()
and set()
.
However, to set a service, you must give an instance of the Service
class.
You can create it with the ServiceFactory
service.
Get by tag
You can get services by tag using the ServiceTagBuilder
:
Get by interface
You can easily get services implementing an interface using ServiceTagBuilder
with the buildFromInterface()
method.
Get single service
You can get the Service
of a single service using ServiceTagOption::FROM_CLASS
and ServiceTagOption::SERVICE_TARGETED
options.
AsDefaultTaggedService
As said before, the priority of a tag is set to 0 by default, but when attaching the attribute AsDefaultTaggedService
to a service, you can set the priority to some or all of his tags to 1.
[!NOTE] If you set the priority of the service into the config, it will override the one set by this attribute.
Service Subscription
The service subscription feature allows a service to declaratively specify which other services it wants to use. This approach allow a service to retrieve and instanciate others conditionally.
Principle
A subscriber service must implement the ServiceSubscriberInterface
and define the static getSubscribedServices()
method. This method returns a list of tags identifying the services to subscribe.
[!NOTE] The $container will not provide a Container but a ServiceStack which is a restricted version of the container that only allows access to the services defined in
getSubscribedServices()
.
ServiceSubscriberTrait
To simplify implementation, you can use the ServiceSubscriberTrait
which provides a builtin provide()
method to retrieve and filter subscribed services.
Trait Configuration
The trait uses PHP templates for better type integration. You can specify the types of input and output services, as well as the context used for filtering.
provide(array $ctx): array
Retrieve all subscribed services and filters them according to the provided context. It returns an array of services that match the criteria.
pick(mixed $service, array $ctx): bool
(protected)
Filtering method to define which services should be selected according to the context.
[!NOTE] By default this method returns
true
, meaning all services are selected.
transformOut(mixed $service, array $ctx): mixed
(protected)
Transformation method to modify services before returning them.
[!NOTE] By default this method returns the service as is.
ServiceFactory
You can use the ServiceFactory
service to create a service instance.
[!CAUTION] If the package is properly configured, you should not need to use this service.
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.
[!CAUTION] If the package is properly configured, you should not need to use this service.
All versions of dependency-injection with dependencies
aatis/tag Version ^1.0
aatis/parameter-bag Version ^1.0
symfony/yaml Version ^6.3
psr/container Version ^1.0