Download the PHP package anonymous-php/simple-di without Composer
On this page you can find all versions of the php package anonymous-php/simple-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anonymous-php/simple-di
More information about anonymous-php/simple-di
Files in anonymous-php/simple-di
Package simple-di
Short Description Simple Dependency Resolving and Injection Container
License MIT
Informations about the package simple-di
Simple DI
Actually this library is more about the dependency container than auto-wiring but don't worry auto-wiring is there. You have the possibility to create instances of classes with auto-wiring of their arguments and to inject dependencies to called methods.
History
I like PHP-DI very much but when I started new project I understood it needs another approach of determining it's definitions and dependencies. I wanted to have the simplest as possible syntax of definitions and powerful, but not complex at the same time, mechanism of injection. So I just wrote my own DI.
Peculiarities
I didn't want to wrap any definition to unnecessary functions which just marks this definition as an instantiable class. I already know what is it while write a code and I want to decide how to use this information in my application.
My colleague asked me about the primitives injection in my library. I don't think is a good idea. Just make the dependency for the Container and get the value you need. Keep this type of values under control by yourself.
Compatibility
Auto-wiring is DISABLED by default.
Installation
Methods
Сlass Container
implements \Psr\Container\ContainerInterface
interface so it has two methods has($id)
which works
as usual and get($id)
which can has the behavior different from your preferred library. There is the method
set($id, $value)
which does exactly the same it means.
get($id)
The method responds with a primitive with the only one exception - Closures. In case of Closure the library resolves it with the injection of it's dependencies. The Closure may to return any primitive or an instance of any class you wish. This method caches results of the resolving.
instantiate($id, array $arguments = [], $instanceOf = null)
This method creates an instance of the certain class. It tries to resolve the definition or instantiate provided class
in case of definition absence. The method creates the new one instance on each call. In case of closure as argument
instantiate
resolves it each time too.
make($id, array $arguments = [], $recreate = false)
Almost the same as previous but with the cache. It means you will get the same instance on each method call. If method
make
will be called after instantiate
using the same $id
you will get already cached instance of class you
provided.
injectOn($callable, array $arguments = [])
Injects arguments to the method of provided instance and calls it. If array of arguments doesn't contain all
variables which called method wait for the library tries to resolve them. Notice: Closure is an object with the method
__invoke
so you can use injectOn
on it.
call($callable, array $arguments = [])
Almost the same as injectOn
but provides the possibility to resolve and instantiate provided class.
Todo:
- Documentation
- Tests
- Performance measurement