Download the PHP package krak/cargo without Composer
On this page you can find all versions of the php package krak/cargo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cargo
Cargo
Cargo is yet another container library. Its feature set and semantics closely follow Pimple; however, it's design is more modular so that it can be extended. It was designed to be compatible with Pimple; so you can easily use any Pimple service providers with Cargo.
Pimple is a great service container; however it suffers from one problem... extendability. Pimple was never designed to properly extended or decorated which makes it very hard to add features without modifying the core. Cargo is a container that manages to keep the simplicity of Pimple while allowing powerful extensions.
Installation
Install with composer at krak/cargo
Usage
Creating a Container
There are several ways to create cargo containers. The easiest way is to just create the default container like so:
Which is just the same as doing:
Cargo is designed to be extendable and flexible, so each container decorator adds a feature that can be removed if not desired.
If you want just a bare bones container, you can use the following:
This just implements the Box and BoxFactory container which caches services as singletons by default.
Defining Services
Services can defined and configured several ways.
Due to the BoxFactoryContainer, all Closures are treated as lazy services. Meaning, they are not invoked until needed. The Singleton container also defaults all services to be singletons, so the result of the service definition closure is cached so that it's not invoked twice. These semantics mimic the behavior of the Pimple Container;
Accessing the Container
You can either use the ArrayAccess methods or get
to retrieve values and invoke services.
Factory or Singleton Services
You can specify if you want to define a service as a factory or singleton with these two helper methods.
Parameters/Values
Anything added to the container that isn't a service is defined as value.
Values are stored and retrieved as is. No processing is done to them.
If you want to use a closure as a parameter, you can use the protect
method:
Env Parameters
You can register parameters to be read from the environment with the env
method:
Wrapping Services
Similar to Pimple's extend
, Cargo allows you to wrap service definitions for decoration.
If you want to replace a definition, you would simply redefine it; however, if you want to decorate or modify a definition, you wrap it:
Service Freezing
Services by default will be frozen due to the FreezingContainer. You can redefine entries as much you'd like, but once a service is invoked, it is considered frozen and will throw an exception if you try to redefine it.
Aliasing Entries
It's often useful to use class names as the identifier, but then also provide aliases for a quick reference.
Auto Wiring
Auto wiring allows the container to try and automatically instantiate services if they aren't already defined in the container. To enable auto-wiring, you need to:
- Install the Auto Args Library (
composer install krak/auto-args
) - Use the AutoWireContainer
In addition, you can bind any class to be auto wired:
a
and b
will resolve to their respective classes. This only works on singleton/factory entries, else it'll just treat the service like a string value and won't try to auto-resolve it.
Service Providers
Cargo\ServiceProvider
provides a simple interface for defining multiple related services.
You can register service providers with a given container with the register
method:
Container Interop
Krak\Cargo\Container
is not compatible with the ContainerInterop
interface by default. However, you can easily export the container to an Interop container using the toInterop
function.
Pimple Interop
Achieving Pimple compatibility is simple with the toPimple
function.
Delegate Containers
In an effort to provide better integration with other containers, we provide delegate containers to allow you to default to a cargo definitions, but fallback to the delegate container.
ArrayAccessDelegateContainer
and PsrDelegateContainer
both act as delegate containers. The first will accept any array or ArrayAccess
object (like Pimple), and the other will accept any Psr Container.
Cargo Design
Container Interface for Decoration
To do...
Boxes
To do...
API
function alias(Container $c, $id, ...$aliases)
Aliases an entry $id
into $aliases
for the container $c
. Each alias will share the same box reference as the original entry.
function env(Container $c, $var_name, $id = null)
Adds an EnvBox entry into the container $c
with $var_name
being the name of the env var and $id
is the entry name.
If $id
is left null, then it will default to $var_name
.