Download the PHP package idealogica/indi without Composer
On this page you can find all versions of the php package idealogica/indi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download idealogica/indi
More information about idealogica/indi
Files in idealogica/indi
Package indi
Short Description The simplest [in]dependency injection container for PHP7, compatible with PSR-11.
License MIT
Homepage https://github.com/idealogica/indi
Informations about the package indi
InDI - [In]Dependency Injector
1. What is InDI?
2. Installation
3. Container values
4. Lazy initialization of container values
4.1. Defining shared values
4.2. Defining factory values
4.3. Accessing shared and factory values
5. Dependency injection
6. Value providers
7. Delegates
8. Integration
9. License
1. What is InDI?
InDI is the simplest [in]dependency injection container for PHP7, compatible with PSR-11. It offers intuitive way to manage PHP application dependencies mostly using native language constructs. The main idea is to provide painless way for programmers of any level of experience to use dependency injection in their projects. InDI is fast, easy to use, powerful and standards compliant. It doesn't provide sort of magic with automatic constructor arguments resolution, but I'm sure that it's a main advantage of InDI and such containers. Your code will always be readable and clear.
What is dependency injection?
It's a pattern that allows you to manage relations between your services and settings transparently. Using it you can build your application from a bunch of reusable decoupled components, distribute your initial settings over your application, write clear code that can be easily refactored, tested and maintained. There is a good explanatory article from Fabien Potencier.
InDI - more simple than Pimple
InDI is inspired by Pimple - another great dependency injection container for PHP. Their main principles are the same, so it won't take much of your time to start using InDI if you are an experienced Pimple user.
2. Installation
InDI requires PHP7 and psr/container
package.
3. Container values
InDI is a simple key-value storage and you can add any kind of data to it. Any PHP variable can be stored inside InDI:
Values can be accessed in the similar way:
You can iterate over InDI values:
You can check that value exists:
Values that you set previously can be removed:
4. Lazy initialization of container values
Let's assume that you have database connection service. Of course, you can add it to container directly:
In this case database connection initializes instantly when you call new
operator.
If you want on-demand connection to your database you should use value lazy definition.
Value lazy definition is a PHP callable that simply returns initialized value:
4.1. Defining shared values
You can pass value lazy definition to addShared
method of container to get
value shared across your application:
4.2. Defining factory values
Use addFactory
method along with value lazy definition to obtain a new value instance
every time you access it:
Notice that additional arguments can be defined in factory value lazy definition and then passed to it at runtime.
4.3. Accessing shared and factory values
Your previously defined shared or factory values can be accessed in two different ways:
► Directly form container:
► Using raw value lazy definition closure. Obtain it just like an any other regular value:
Later you can get shared or factory data by calling obtained closure:
It can be helpful when:
- You need for "laziest" initialization. For example you can pass this closure to your
middleware and get instance of
DBAL\Connection
right just before using it - You want to get variable amount of instances of factory values in one place. For example you want to have multiple view instances in the same middleware
5. Dependency injection
Let's define all initial settings for our database connection and view classes:
Of course, you can inject these values to service that was added directly:
In case of lazy initialization you can inject any value from container in your value lazy definition.
When value lazy definition is closure $this
can be used to access container:
Anyway, for all PHP callables InDI can detect instance of Interop\Container\ContainerInterface in arguments and pass itself on its place:
Make sure that container argument is typehinted.
6. Value providers
If you want to create redistributable component and use it in different projects you should define value provider. It's also just a PHP callable like a value lazy definition:
As you can see, you can add additional arguments to callable along with container instance.
Let's register our new value provider using container register
method:
Value providers are executed right after they are registered.
7. Delegates
InDI can interact with any PSR-11 compliant dependency injection container. You can pass foreign container instance as a constructor argument to share values form it in two modes:
► Master mode. Allows to have all values from foreign container available in InDI:
► Lookup mode. Allows to have all values from foreign container available as a dependency lookups:
More details about delegate lookup feature you can find in PSR-11 documentation.
8. Integration
It's possible to integrate InDI into your project in few different ways:
► Most common and simple - just create InDI container instance and then use it:
► Inherit you main application class from Idealogica\InDI\Container
:
► In case when your main application class is already inherited you can use traits to introduce InDI functionality:
9. License
InDI is licensed under a MIT License.