viragrajput/virag-container
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.
viragrajput/virag-container 适用场景与选型建议
viragrajput/virag-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 「inversion of control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 viragrajput/virag-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 viragrajput/virag-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 viragrajput/virag-container 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-25