alanvdb / dependency-container
Basic PSR-11 dependency containers
Installs: 14
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/alanvdb/dependency-container
Requires
- psr/container: ^2.0
Requires (Dev)
- phpunit/phpunit: ^11.3
This package is auto-updated.
Last update: 2025-10-13 16:26:58 UTC
README
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 ofLazyContainerthat implements theIteratorinterface.ContainerFactory: A factory to create instances ofLazyContainerandIterableLazyContainer.
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:
composer require alanvdb/dependency-container
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
use AlanVdb\Dependency\LazyContainer; $container = new LazyContainer(); $container->add('service1', function() { return new Service1(); }); if ($container->has('service1')) { $service1 = $container->get('service1'); }
IterableLazyContainer
The IterableLazyContainer extends LazyContainer and implements the Iterator interface, allowing you to iterate over the registered services.
Example
use AlanVdb\Dependency\IterableLazyContainer; $container = new IterableLazyContainer(); $container->add('service1', function() { return new Service1(); }); $container->add('service2', function() { return new Service2(); }); foreach ($container as $serviceId => $service) { // Process each service }
ContainerFactory
The ContainerFactory class provides methods to create instances of LazyContainer and IterableLazyContainer.
Example
use AlanVdb\Dependency\Factory\ContainerFactory; $factory = new ContainerFactory(); $lazyContainer = $factory->createLazyContainer(); $iterableLazyContainer = $factory->createIterableLazyContainer();
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\NotFoundExceptionInterfaceis 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
IdNotFoundExceptionif 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
trueif the service exists,falseotherwise.
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:
vendor/bin/phpunit
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.