Download the PHP package atanvarno/dependency without Composer
On this page you can find all versions of the php package atanvarno/dependency. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download atanvarno/dependency
More information about atanvarno/dependency
Files in atanvarno/dependency
Package dependency
Short Description PSR-11 dependency injection container
License MIT
Homepage https://github.com/atanvarno69/dependency
Informations about the package dependency
Atanvarno\Dependency
A bare bones PSR-11 dependency injection
container, implementing ArrayAccess
.
Features
- Interoperability using PSR-11
- Implements
container-interop
's delegate lookup feature - Lazy loading
- Optionally use PSR-16 caching
- Optionally use array syntax to access the container
Atanvarno\Dependency
does not provide auto-wiring via reflection. It is a
bare bones container only.
Requirements
PHP >= 8.0 is required, but the latest stable version of PHP is recommended.
Installation
Atanvarno\Dependency is available on Packagist and can be installed using Composer:
Basic Usage
Usage
Retrieving Entries
There are two ways to retrieve an entry:
Entries retrieved from an ID will, by default, be the same instance:
You can specify that each time you get a particular ID it will be a new instance when you define your entries (see lazy loading entries).
Checking Entries
There are two ways to check if an entry is available, each returns bool
:
Removing Entries
There are two ways to remove an entry:
Adding entries
There are several ways to add an entry:
$entry
can be any PHP value. Atanvarno\Dependency\Container
when used like
this is a simple key-value store, not particularly different from an array.
Instead entries can be defined so that they are lazy loaded.
Entries can also be added via the constructor (see instantiation).
Lazy Loading Entries
Any entry can be defined as lazy loaded, so it is built only when it is accessed.
Two helper functions are included to allow you define lazy loaded entries:
object()
.
(The examples use the set()
method, but array syntax works as well.)
You can return a value from any callable
using factory()
:
You can return an object from its class name using object()
:
Both factory()
and object()
take an array of parameters to pass to the
callable
or constructor. These parameters can be any PHP value or can be
other container entries. Other container entries are referenced by using the
entry()
function:
(The container itself can referenced using the default entry ID container
. If
you need the container to have a different entry ID, use setSelfId()
.)
Both factory()
and object()
take an optional third bool
parameter which
determines whether the first value they return should be registered and
subsequently always returned when the entry is retrieved (default behaviour),
or whether a new value should be returned each time (pass false
).
Setting Properties and Calling Methods
You may want to set public
properties or call methods of newly instantiated
objects in order to configure them for use.
Both factory()
and object()
return a Definition
which provides methods with a fluent interface to allow this:
The example uses object()
, but will work as well with factory()
. Note if
factory()
does not define a object instance, these methods will do nothing.
Delegate Lookup Feature
Atanvarno\Dependency\Container
implements container-interop
's
delegate lookup feature
and can act as both a parent/composite/delegate and child container.
To add child containers (and make $container
a composite container) use
addChild()
:
Subsequent calls will add additional children.
To add a parent container (and make $container
delegate its dependency
lookups) use setDelegate()
:
Subsequent calls will replace the parent.
Fluent Interface
Atanvarno\Dependency\Container
provides a
fluent interface, allowing
multiple calls to be chained. These methods each return the Container
instance:
addChild()
clearCache()
delete()
get()
set()
setDelegate()
setSelfId()
Instantiation
When the container is instantiated it optionally accepts an array of
Definition
instances (returned from factory()
, object()
and value()
)
which will be added to the container. This array is indexed by entry ID.
You can make a configuration file that returns this array:
Then include it in the constructor call:
Caching
Atanvarno\Dependency\Container
can use a
PSR-16 cache to persist registered items.
Note it does not persist definitions; only registered values that have been
returned at least once.
The cache is invisible to the user; calls to delete()
,get()
and set()
will use and update the cache as they require.
If you want to clear the container's cache use clearCache()
:
The constructor accepts a cache as its second parameter and an optional key for
use with the cache as its third parameter (defaults to container
).
The constructor also accept an entry()
that points to a cache defined in the
first parameter, so the cache instance can be loaded by the container itself.
Exceptions
All exceptions thrown implement PSR-11's
ContainerExceptionInterface
.
Full API
See API.