Download the PHP package crysalead/box without Composer
On this page you can find all versions of the php package crysalead/box. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download crysalead/box
More information about crysalead/box
Files in crysalead/box
Package box
Short Description Minimalist Dependency Injection Container.
License MIT
Informations about the package box
Box - Minimalist Dependency Injection Container
Box is dependency injection container which manage dependencies based on closure definitions only. This approch has the particularity to be simple, easy and flexible. It also support lazy dependencies resolution using wrappers.
API
Creating a Dependency container
Setting up a service
To to share a unique service over your application use Box::service()
. A service can be a class, an instance, a string or any kind of value.
Example:
Each shared service defined with Box::service()
can be retreived using Box::get()
.
Note: If a share is defined using a closure, the closure will be executed once and the result will be returned for all next Box::get()
calls.
Setting up a factory
Use Box::factory()
to setup a factory. It can be a closure:
Or a fully-namespaced class name.
Box::factory()
will create an new instance of the definition when resolved using Box::get()
.
Resolving a dependency
To resolve a dependency, use Box::get()
:
All $paramX
are optional parameters passed to the closure or directly to the constructor if the definition is a fully-namespaced class name string.
Returing a wrapped dependency
Wrapping a dependency has the advantage to allow to inject a dependency without resolving it directly. To be able to lazily resolve a dependency you need to use Box::wrap()
:
All $paramX
are optional parameters passed to the closure or directly to the constructor if the definition is a fully-namespaced class name string.
Then dependency is resolved by doing:
All $paramX
are optional and will overrides the ones setted at the wrapping step.
Cleanup
Use Box::remove('foo')
to remove a specific dependency or Box::clear()
to remove all dependencies.
Global API
You can use the box()
function to get/set a DI anywhere in your code.