renakdup/simple-dic
Composer 安装命令:
composer require renakdup/simple-dic
包简介
Simple DI Container with autowiring for your WordPress application with NO dependencies.
关键字:
README 文档
README
WordPress DI Container with autowiring in a single file with NO dependencies allows you to easily use it in your PHP applications and especially convenient for WordPress plugins and themes.
Important
100% code coverage
Why choose Simple DI Container
- Easy to integrate in your WordPress project - just copy one file or install via Composer.
- No dependencies on other scripts or libraries.
- Supports auto-wiring
__constructorparameters for classes as well as for scalar types that have default values. - Supports PHP ^7.4|^8.
- Allow you following the best practices for developing your code.
- No PHPCS conflicts.
- Supports Lazy Load class instantiating.
- Lightweight and fast.
How to install in a project
There are several ways, you can choose what you more prefer:
Install via Composer
composer require renakdup/simple-php-dic
Copy and paste file
- Just copy the file
./src/Container.phpto your plugin directory or theme. - Rename
namespacein the file
fromRenakdup\SimpleDICto<Your_Plugin_Name>\SimpleDIC - Include the file.
How to use it
Get started:
- Create container instance.
- Set a service
- Get a service
- Use object
use Renakdup\SimpleDIC\Container; // create container $container = new Container(); // set service $container->set( Paypal::class, function () { return new Paypal(); } ); // get service $paypal = $container->get( Paypal::class ); // use this object $paypal->pay();
Method get() works as a singleton. Service will be resolved once and cached inside the $container, new invocations of the get() method return the same object.
$obj1 = $constructor->get( Paypal::class ); $obj2 = $constructor->get( Paypal::class ); var_dump( $obj1 === $obj2 ) // true
If you want to instantiate a service several times, then use the make() method.
$obj1 = $constructor->make( Paypal::class ); $obj2 = $constructor->make( Paypal::class ); var_dump( $obj1 === $obj2 ) // false
Factory
Factory is an anonymous function that wrap creating an instance.
Allows to configure how an object will be created and allows to use Conainer instance inside the factory.
$container->set( Paypal::class, function () { return new Paypal(); } );
As well factories create objects in the Lazy Load mode. It means that object will be created just when you resolve it by using get() method:
$container->set( Paypal::class, function () { return new Paypal(); } ); $paypal = $constructor->get( Paypal::class ); // PayPal instance created
Primitives
SimpleDIC allows setting values for the container for primitive types:
$container->set( 'requests_limit', 100 ); $container->set( 'post_type', 'products' ); $container->set( 'users_ids', [ 1, 2, 3, 4] ); $user_ids = $container->get( 'users_ids', [ 1, 2, 3, 4] );
Container inside factory
SimpleDIC allows to get a Container instance inside a factory if you add parameter in a callback ( Container $c ). This allows to get or resolve another services inside for building an object:
$container->set( 'config', [ 'currency' => '$', 'environment' => 'production', ] ); $container->set( Paypal::class, function ( Container $c ) { return new Paypal( $c->get('config') ); } );
Autowiring
SimpleDIС autowiring feature allows to Container automatically create and inject dependencies.
I'll show an example:
class PayPalSDK {} class Logger {} class Paypal { public function __constructor( PayPalSDK $pal_sdk, Logger $logger ) { //... } }
And then when you create Paypal::class, you run $container->get(Paypal::class), and Container identifies all classes in the constructor and resolves them. As if it's:
new Paypal( new PayPalSDK(), new Logger() );
Container autowiring can resolve default values for primitive parameters in a constructor:
class Logger { public function __constructor( $type = 'filestorage', $error_lvl = 1 ) { //... } }
You can use auto-wiring feature that allows to Container create an instances that requires in the __constructor of class as well as it resolves constructor dependencies for
Note
But if object creating is more complex and requires configuring and you don't have parameters with default values in the constructor then you need to use factory for preparing service.
Create an instance every time
Method make() resolves services by its name. It returns a new instance of service every time and supports auto-wiring.
$conatainer->make( Paypal::class );
Note
Constructor's dependencies will not instantiate every time.
If dependencies were resolved before then they will be passed as resolved dependencies.
Consider example:
class PayPalSDK {} class Paypal { public PayPalSDK $pal_sdk; public function __constructor( PayPalSDK $pal_sdk ) { $this->pal_sdk = $pal_sdk; } } // if we create PayPal instances twice $paypal1 = $container->make( Paypal::class ); $paypal2 = $container->make( Paypal::class ); var_dump( $paypal1 !== $paypal2 ); // true var_dump( $paypal1->pal_sdk === $paypal2->pal_sdk ); // true
Dependencies of PayPal service will not be recreated and will be taken from already resolved objects.
PSR11 Compatibility
in progress
Roadmap
- Add binding services with configuration
- Add auto-wiring for registered classes in DIC
- Add auto-wiring for defaults primitives for auto-fillings
- Add supporting invocable classes
- Add PSR11 interfaces in the Container.php.
- Add auto-wiring support for not bounded classes.
- Add resolved service storage (getting singleton).
- Add ability to create new instances of service every time.
- Improve performance.
- Fix deprecated
Use ReflectionParameter::getType() and the ReflectionType APIs should be used instead.for PHP8 - Bind $container instance by default.
- Add
removemethod. - Circular dependency protector.
Nice to have
- Integrate CI with running autotests
- Add badges with tests passed
- Add PHPStan analyzer
- Add code coverage badge
- Add descriptions in the code for functions.
- Choose codestyle
- Add on packagist
- Add if class exists checks in the Container file?
- Rename Container.php to SimpleContainer.php
- Show stack trace when I have a debug only?
- PHP 8 named arguments and autowiring.
- Allow to set definitions via
__constructor. - Add supporting Code Driven IoC.
- Add configurations of Container.
- Add Performance Benchmarks
License
The MIT License (MIT). Please see the License File for more information.
renakdup/simple-dic 适用场景与选型建议
renakdup/simple-dic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.02k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「di container」 「dic」 「wp」 「wordpress plugin」 「mu-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 renakdup/simple-dic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 renakdup/simple-dic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 renakdup/simple-dic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
PHP 8.1-8.5 compatible fork of rdlowrey/auryn — a dependency injector for bootstrapping object-oriented PHP applications.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
A simple Dependency Injection Container, PSR-11 compliant.
统计信息
- 总下载量: 2.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 34
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-24