Download the PHP package pmjones/caplet without Composer
On this page you can find all versions of the php package pmjones/caplet. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pmjones/caplet
More information about pmjones/caplet
Files in pmjones/caplet
Package caplet
Short Description A minimal PSR-11 compliant autowiring dependency injection container.
License MIT
Informations about the package caplet
Caplet
Caplet is a minimal autowiring dependency injection container to handle basic constructor injection and object factories.
Getting Started
Instantiate Caplet like so:
You can then call one of these PSR-11 methods:
-
get(string $class) : object
to get a shared object instance of$class
. has(string $class) : bool
to see if an instance is available. (This means either a class definition exists; or, in the case of an interface, the interface definition exists and has afactory()
entry -- see below for thefactory()
method.)
Caplet offers this non-PSR-11 method:
new(string $class) : object
to get a new object instance of$class
. (This method is not part of PSR-11.)
Configuration
Configure non-object constructor arguments by passing an array with the
structure $config['ClassName']['parameterName']
at Caplet construction
time. For example, given the following class ...
... you would configure the arguments for its parameters like so:
Alternatively, extend Caplet and override __construct()
to accept your own
environment or configuration values, then call the parent::__construct()
with
the $config['ClassName']['parameterName']
structure.
Factories
Extending Caplet also allows you to call the protected factory()
method
inside the constructor to define the object-creation logic for a given type.
This allows you to specify concrete classes for instantiation in place of
abstracts or interfaces. For example:
As seen above, the callable factory logic must have the signature
function (Caplet $caplet)
, and may specify a return type.
Constructor Parameter Resolution
Caplet will attempt to resolve constructor parameters in this order:
- First, use an argument from $config, if one is available.
- Next, try to
get()
an object of the parameter type. - Last, use the default parameter value, if one is defined.
If none of these work, Caplet will throw Exception\NotInstantiated, with a previous exception of Exception\NotResolved.