tleckie / di
Simple PSR-11 dependency injection container
Fund package maintenance!
teodoroleckie
www.paypal.com/donate?business=ZHYA2MTGA4884¤cy_code=USD
Requires
- php: ^8
- php-di/php-di: ^6.3
- psr/container: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: v3.0.0-beta.2
- infection/infection: ^0.21.5
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.5
README
Di:
You can install the package via composer:
composer require tleckie/di
<?php require "../vendor/autoload.php"; use Tleckie\Di\Di; use Tleckie\Di\Definition\Adapter\FileAdapter; $container = new Di(); $container->setAdapter(new FileAdapter('conf/definition.php', $container)); $container->get('stringValue'); $container->get('numericValue'); $container->get('chainValue'); $container->get('closureValue'); $container->get('closureValue'); // same instance $container->get('closureValue'); // same instance $container->get('closureValue'); // same instance $container->get('arrayValue'); $container->get('lazyFactoryWithConstructArgumentsAnReturnSameInstance'); $container->get('lazyFactoryWithConstructArgumentsAndCallMethodWithArgumentsAndCreateANewInstance');
Definition:
conf/definition.php
<?php return [ 'stringValue' => 'Lorem ipsum dolor sit amet', 'numericValue' => 55, 'chainValue' => 'stringValue', 'closureValue' => static function () { // each call returns the same instance return new Project\B('String Argument'); }, 'arrayValue' => [ [ [ 'host' => 'localhost', 'port' => 443, 'user' => 'userValue', 'pass' => 'password', ] ] ], // lazy loading and singleton instance 'lazyFactoryWithConstructArgumentsAnReturnSameInstance' => [ 'className' => Project\A::class, 'arguments' => ['stringValue', 'closureValue'], 'newInstance' => false // each call returns the same instance ], 'lazyFactoryWithConstructArgumentsAndCallMethodWithArgumentsAndCreateANewInstance' => [ 'className' => Project\A::class, 'arguments' => ['stringValue', 'closureValue'], 'newInstance' => true, // each call returns a new instance 'methods' => [ [ 'methodName' => 'setValue', 'arguments' => ['changed value '] ], [ 'methodName' => 'setOtherValue', 'arguments' => ['changed other value '] ] ] ] ];