jeanmarcos/dependency-container

v1.0.0-alpha 2024-11-11 17:33 UTC

This package is auto-updated.

Last update: 2025-03-11 18:22:09 UTC


README

Latest Version Latest Version Build Status License

⚠️ 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:

  1. Fork the repository.
  2. Create a new branch (feature/my-feature).
  3. Commit your changes.
  4. 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! πŸ˜„