Download the PHP package alanvdb/dependency-container without Composer
On this page you can find all versions of the php package alanvdb/dependency-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alanvdb/dependency-container
More information about alanvdb/dependency-container
Files in alanvdb/dependency-container
Package dependency-container
Short Description Basic PSR-11 dependency containers
License MIT
Informations about the package dependency-container
Dependency Container
A PHP PSR-11 Dependency Injection System
Overview
This project provides a lightweight dependency injection system for PHP. It includes the following main components:
LazyContainer
: A container that lazily instantiates services.IterableLazyContainer
: An extension ofLazyContainer
that implements theIterator
interface.ContainerFactory
: A factory to create instances ofLazyContainer
andIterableLazyContainer
.
This library is fully compatible with the PSR-11 standard, which defines a common interface for dependency injection containers. This ensures that your dependency injection containers are interoperable with other PSR-11 compliant libraries.
Installation
To install the dependency-container, you can use Composer:
Usage
LazyContainer
The LazyContainer
class allows you to register services with a callable that will be invoked only when the service is requested. This provides lazy instantiation of services, which can improve performance by deferring the creation of services until they are actually needed.
Example
IterableLazyContainer
The IterableLazyContainer
extends LazyContainer
and implements the Iterator
interface, allowing you to iterate over the registered services.
Example
ContainerFactory
The ContainerFactory
class provides methods to create instances of LazyContainer
and IterableLazyContainer
.
Example
PSR-11 Compatibility
This project adheres to the PSR-11 Container Interface, ensuring that the LazyContainer
and IterableLazyContainer
classes conform to the standard. This allows for seamless integration with other libraries and frameworks that support PSR-11.
Key PSR-11 Methods
LazyContainer
and IterableLazyContainer
These classes implement the following PSR-11 methods:
get(string $id)
: Retrieves an entry from the container by its identifier.- If the identifier is not found, an exception implementing
Psr\Container\NotFoundExceptionInterface
is thrown.
- If the identifier is not found, an exception implementing
has(string $id): bool
: Returns true if the container can return an entry for the given identifier.
By adhering to PSR-11, these containers can be used wherever a PSR-11 compliant container is expected.
API Documentation
LazyContainer
Methods
-
add(string $id, callable $generator)
: Adds a new service to the container.string $id
: The service identifier.callable $generator
: A callable that returns the service instance.
-
get(string $id)
: Retrieves a service from the container.string $id
: The service identifier.- Returns the service instance.
- Throws
IdNotFoundException
if the service identifier is not found.
has(string $id): bool
: Checks if a service identifier exists in the container.string $id
: The service identifier.- Returns
true
if the service exists,false
otherwise.
IterableLazyContainer
The IterableLazyContainer
inherits all methods from LazyContainer
and implements additional methods from the Iterator
interface:
current()
: Returns the current element.key()
: Returns the key of the current element.next()
: Moves forward to the next element.rewind()
: Rewinds back to the first element.valid()
: Checks if the current position is valid.
ContainerFactory
Methods
createLazyContainer(): LazyContainerInterface
: Creates a new instance ofLazyContainer
.createIterableLazyContainer(): LazyContainerInterface & Iterator
: Creates a new instance ofIterableLazyContainer
.
Testing
To run the tests, use the following command:
The tests are located in the tests
directory and cover the functionality of LazyContainer
, IterableLazyContainer
, and ContainerFactory
.
License
This project is licensed under the MIT License. See the LICENSE file for details.