Download the PHP package mbretter/stk-di without Composer
On this page you can find all versions of the php package mbretter/stk-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mbretter/stk-di
More information about mbretter/stk-di
Files in mbretter/stk-di
Package stk-di
Short Description Dependency injection made easy
License BSD-3-Clause
Informations about the package stk-di
stk-di
A simple dependency injection system usable with any container implementing the Psr\Container\ContainerInterface.
The service factory supports constructor injection and argument injection for services implementing the Injectable interface.
As a special feature OnDemand services are supported, they support service creation at runtime not at creation time, this avoids a dependency loading bloat for each request, due to complex dependencies. As a side effect OnDemand services may be used to inject objects which are not implementing the Injectable interface, like 3rd party services.
The library comes up with a dumb container implementation, this container is mainly used for testing and demonstration purposes.
Injectable
The injectable interface is a dummy/empty interface for targeting injectable services. Services should implement this interface.
Registering services
As a first step you have to put all your services into the container and it is recommended to put the service factory into the container too.
Argument injection
The service factory scans each service for private methods having injectables as argument and injects the service by fetching it from the container, using the argument name as key.
Constructor injection
When instantiating services, the factory scans the constructor for argument names, if they are found in the container, the service is injected, default values are supported (if the container has no service with the given name). It is not needed, that the injected service implements the injectable interface, any container value may be injected.
Constructor injection with params
If it is needed to pass some kind of static parameters (e.g. config settings) at declaration time and some additional parameters at instantiation time, the service factory skips the passed parameters and only injects the remaining arguments.
OnDemand services
OnDemand services are wrappers around services to avoid the immediate instantiation of dependend services. In bigger projects with tons of services, it is very likely to run into a dependency bloat, ServiceA depends on ServiceB, ServiceB needs ServiceC ... at the end you have all your services instantiated, regardeless whether they are used or not.
Non injectables (3rd party services)
If you want to inject services not implementing the Injectable interface (http-clients, etc.), you can register them using the OnDemand service.
Reusability with traits
Traits are very handy, if you do not want to duplicate the code when injecting the same service again and again.
Write a Trait with the property, geter and seter