Download the PHP package ob-ivan/sd-dependency-injection without Composer
On this page you can find all versions of the php package ob-ivan/sd-dependency-injection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sd-dependency-injection
Registers services and injects them into consumers.
Installation
Usage
Terminology
In large applications a situation when some class instances are to be shared among many other classes is common. Think of a database connection instance shared among various controller classes.
Those shared classes are called services (they provide some useful functionality to others), and those sharing are called consumers (they consume the services provided).
The process of letting a consumer know all services it requires access to is called injection.
A consumer may declare formally a list of services it requires. This is called listing its dependencies. The process of taking a consumer's dependency list and injecting corresponging services into it is called, unsurprisingly, dependency injection.
A dependency injection container (DIC) is a way to implement dependency injection. It requires that each service is registered within the container under a certain common name, and that consumers list common names of their required services as formal dependency lists.
Dependecies may be injected into consumers in several different ways. In this library, these three types of dependency injection are supported:
- Setter injection aka Interface injection - given a consumer Instance implementing DeclarerInterface call a setter for each declared dependency.
- Constructor injection - given a consumer's ClassName resolve its constructor arguments as dependency names and call constructor with appropriate services. Setter injection is then invoked on the created instance.
- Argument injection - given a consumer Function resolve its arguments as dependency names and call the Function with appropriate services.
You can use a container to register services, or to produce consumers, or to inject dependencies into arbitrary code.
- Consumer = Function | Instance
- ConsumerInitializer = ClassName | Consumer
- ServiceInitializer = ConsumerInitializer | Value
Construction
A container is initialized with raw values, no Initalizers allowed:
If you have several containers, you can create a new one by merging them:
Registering services
Services are registered with ServiceInitializers:
You do not have to register container within itself, as it is self-aware by default:
You can extend registered services:
Consumer production
Consumers are produced with ConsumerInitializers (Value is not supported):
You can inject services into any Consumer:
Though discouraged, you can use container as service locator, if execution context does not let you use the power of dependency injection:
Defining a consumer
Class consumers (as opposed to callable consumers) declare their dependencies in any of following ways --- or a combination of both:
- By implementing
DeclarerInterface
and returning a list of common names fromdeclareDependencies
method. - By implementing
AutoDeclarerInterface
and importingAutoDeclarerTrait
andAwareTrait
s of their respective dependencies.
As listing common names in declareDependencies
method causes heavy reduplication of common names,
we recommend to define an AwareTrait
for each service you define.
Here's a sample code:
This way defining a consumer in a following way will tell a container to inject ExampleService into it --- or to throw an exception if it's not available.
Service providers
You can encapsulate a service's common name further by putting it into a provider.
Then the service is registered with its provider's instance:
This way neither registration code nor consumers need to know the common name. They just refer to Provider instance and the AwareTrait, and it just works. Hey, it's magic!
Development
To run tests: