承接 viragrajput/virag-container 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

viragrajput/virag-container

最新稳定版本:v1.1.2

Composer 安装命令:

composer require viragrajput/virag-container

包简介

A container package for dependency injection

README 文档

README

Virag Container is a versatile and lightweight dependency injection container designed to streamline the management of class dependencies and service resolution within PHP applications.

With Virag Container, you gain access to a comprehensive set of features essential for effective dependency management. This includes binding classes and interfaces to concrete implementations, resolving dependencies, defining singleton bindings, and managing contextual bindings. Additionally, advanced functionalities such as service providers, custom resolvers, inflectors, auto-wiring, and factories are seamlessly integrated, providing flexibility and extensibility.

Moreover, Virag Container is built with performance in mind, ensuring minimal overhead and optimal efficiency in your application. Whether you're building a small project or a large-scale application, Virag Container offers the tools you need to organize and maintain your codebase effectively.

Installation

You can install Virag Container via Composer. Run the following command in your terminal:

composer require viragrajput/virag-container

Usage

Basic Usage

First, create an instance of the container:

use Virag\Container\Container;

$container = new Container();

Binding Classes or Interfaces

You can bind classes or interfaces to concrete implementations using the bind method:

$container->bind('LoggerInterface', 'FileLogger');

Resolving Dependencies

To resolve dependencies, use the make method:

$logger = $container->make('LoggerInterface');

This will resolve the FileLogger instance.

Singleton Bindings

Singleton bindings can be defined using the singleton method:

$container->singleton('DatabaseConnection', function () {
    return new DatabaseConnection();
});

Constructor Injection

The container supports constructor injection. For example:

class UserRepository
{
    protected $db;

    public function __construct(DatabaseConnection $db)
    {
        $this->db = $db;
    }
}

$container->bind('UserRepository', 'UserRepository');

$userRepository = $container->make('UserRepository');

Contextual Bindings

You can define contextual bindings using the when and needs methods:

$container->when('PaymentGateway')
          ->needs('LoggerInterface', 'PaymentLogger');

Advanced Usage

Service Providers

You can register service providers to bootstrap your application:

$container->addServiceProvider('App\Providers\DatabaseServiceProvider');

Custom Resolvers

Define custom resolvers for specific bindings:

$container->registerCustomResolver('MailService', function ($container) {
    return new ExternalMailService($container->make('Config')->get('mail.api_key'));
});

Inflectors

Inflectors allow you to modify instances after instantiation:

$container->addInflector('LoggerAwareInterface', function ($loggerAware) use ($container) {
    $loggerAware->setLogger($container->make('Logger'));
});

Auto-Wiring

Enable auto-wiring to automatically resolve class dependencies:

$container->enableAutoWiring();

Factories

Use factories for dynamic instance creation:

$container->bindFactory('PaymentGateway', function ($container) {
    return new PaymentGateway($container->make('Config')->get('payment.gateway'));
});

Examples:

Explore various usage examples of the Virag Container Package in the "Docs" folder within this repository. These examples cover a wide range of scenarios to help you understand how to utilize the features of the package effectively.

Contributing

We welcome contributions from the community! If you have any suggestions, find bugs, or want to add new features, please don't hesitate to submit issues or pull requests. Your contributions help improve the package for everyone. Let's build something great together!

License

Virag Container is open-source software licensed under the MIT license.

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固