Download the PHP package nimbly/carton without Composer
On this page you can find all versions of the php package nimbly/carton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nimbly/carton
More information about nimbly/carton
Files in nimbly/carton
Package carton
Short Description A simple PSR-11 container implementation offering reflection based autowiring.
License MIT
Informations about the package carton
Carton
A simple PSR-11 container implementation.
Requirements
- PHP 8.2+
Features
- PSR-11 compliant
- Singleton and factory builders
- Service providers
- Nested container support
- Reflection based autowiring
- Aliasing
Install
Getting the container
Instantiate container
You can create a new instance of a Container.
Singleton instance
Or use the container singleton getInstance
method.
Basic usage
Set an instance
The most basic usage is to just assign an instance to the container itself.
Of course, you don't need to assign objects - it can be anything you like.
Retrieving a value
Grab a value from the container by its key.
NOTE: Retrieving a value that does not exist will throw a Nimbly\Carton\NotFoundException
.
Checking for instance
You can check for the existance of items registered in the container.
Advanced usage
Singleton builder
The singleton builder will ensure only a single instance is ever returned when it is retrieved from the container. The singleton builder requires a callable
that will be invoked when it needs to build your dependency and will pass along the Container
instance as a single parameter.
It also has the added benefit over the set
method by lazily calling your callback. I.e. it will only be created when it is actually needed.
Factory builder
The factory builder will create new instances each time it is retrieved from the container. The factory builder requires a callable
that will be invoked when it needs to build your dependency and will pass along the Container
instance as a single parameter.
Just like the singleton builder, it has the added benefit over the set method by lazily calling your callback. I.e. it will only be created when it is actually needed.
Aliases
You can create aliases of your container items. These aliases simply point to an existing container item and fetch that item for you.
Alternatively, you can provide an alias or an array of aliases when calling set
, singleton
, or factory
.
Autowiring
You can have instances made for you automatically using the make
method - which will attempt to pull dependencies in from the container itself or recursively attempt to make
them if not found.
Dependecy injection on instance methods
Calling an instance method couldn't be easier - Carton will attempt to autoresolve dependencies (autowire) for you when making a call to an instance method.
Adding additional containers
You can extend Carton with additional PSR-11 compliant container instances by calling the addContainer
method. When Carton attempts to resolve an item, it will always attempt to resolve locally first, and if not found, will loop through any additional containers you have provided.
For example, if you had a configuration manager that implemented ContainerInterface
(PSR-11),
you could add it to Carton.
Now you can retrieve your configuration data through the container instance.
Service providers
Service providers allow you to organize your application dependencies in a set of classes.
Create service classes that implement ServiceProviderInterface
.
Then register your service providers with the container.
You can also register multiple services at once and register services by their class name.