jeanmarcos / dependency-container
Dependency container
Requires
- php: >=8.3
- psr/container: ^1.1|^2.0
Requires (Dev)
- phpunit/phpunit: ^11.4
- squizlabs/php_codesniffer: ^3.10
This package is auto-updated.
Last update: 2025-03-11 18:22:09 UTC
README
β οΈ Note: This is an alpha version and is not ready for production use. Upcoming releases will include new features and improvements.
A lightweight, PSR-4 compatible Dependency Injection (DI) container for managing dependencies in PHP applications.
π Overview
jeanmarcos/dependency-container
is a flexible and simple-to-use dependency injection container designed to help you
manage your services and dependencies in PHP projects. The project is inspired by the DI container of Magento, aiming to
offer similar extensibility and flexibility..
β¨ Features
- PSR-4 autoloading compatibility.
- Auto-wiring capabilities.
- Support for shared (singleton) and non-shared instances.
- Circular dependency detection.
- Flexible configuration for optional and nullable dependencies.
- Singleton container implementation.
- Upcoming: Interceptors to extend and modify class behaviors dynamically.
π οΈ Installation
Install the package via Composer:
composer require jeanmarcos/dependency-container
β‘ Quick Start
Instantiating with Services in the Constructor
You can directly instantiate the container with services provided as configurations:
<?php use Jeanmarcos\DependencyContainer\Container; use Jeanmarcos\DependencyContainer\Configs\ServiceConfig; class MyService {} $container = new Container(null, [ new ServiceConfig(className: MyService::class) ]); $myService = $container->get(MyService::class);
Configuring Singleton Services
You can configure a specific service as a singleton (shared instance) when registering it:
$container = new Container( servicesConfig: [ new ServiceConfig(className: MyService::class, shared: true) ] ); $firstInstance = $container->get(MyService::class); $secondInstance = $container->get(MyService::class); // Both instances are the same assert($firstInstance === $secondInstance);
Configuring the Entire Container for Singleton Behavior
If you want all services to be shared by default, configure the container itself for singleton behavior:
use Jeanmarcos\DependencyContainer\Configs\ContainerConfig; $config = new ContainerConfig(allInstanceSharedByDefault: true); $servicesConfig = [ new ServiceConfig(className: MyService::class) ]; $container = new Container($config, $servicesConfig); $firstInstance = $container->get(MyService::class); $secondInstance = $container->get(MyService::class); // Both instances are the same due to container configuration assert($firstInstance === $secondInstance);
Singleton Container Pattern
For applications that require a single shared container instance, you can use SingletonContainer
:
use Jeanmarcos\DependencyContainer\SingletonContainer; // Retrieve the singleton instance of the container $singletonContainer = SingletonContainer::getInstance(null, [ new ServiceConfig(className: MyService::class) ]); $myService = $singletonContainer->get(MyService::class);
The SingletonContainer
class ensures only one instance of the container is created, providing a global access point to
your services.
π§ͺ Testing
The package includes unit and integration tests. To run the tests, use the following command:
composer test
Testing Dependencies
- PHPUnit: PHPUnit is used for both unit and integration testing. The configuration for PHPUnit is included in the
phpunit.xml
file. - PHP CodeSniffer: Ensure code quality by running PHP CodeSniffer.
Run PHP CodeSniffer with:
composer phpcs
π Directory Structure
Here's an overview of the directory structure:
src/
βββ Configs/ # Configuration classes
βββ Exceptions/ # Custom exceptions
βββ Container.php # Main container class
βββ SingletonContainer.php # Singleton container class
tests/
βββ Unit/ # Unit tests
βββ Integration/ # Integration tests
π Documentation
The documentation for jeanmarcos/dependency-container
is currently in progress and will be expanded in future
releases.
π₯ Contributing
Contributions are welcome! Follow these steps to contribute:
- Fork the repository.
- Create a new branch (
feature/my-feature
). - Commit your changes.
- Push the branch and create a pull request.
Please ensure your code follows the existing style and includes tests where appropriate.
π Changelog
v1.0.0-alpha
- Initial alpha release.
- Basic dependency injection container with singleton and non-shared instances.
- Auto-wiring capabilities.
- Support for optional dependencies and nullable parameters.
- Basic circular dependency detection.
- Upcoming Features: Interceptors for extending class methods dynamically.
π License
This package is licensed under the MIT License. See LICENSE for details.
Happy coding! π