1.0.4 2021-04-25 09:36 UTC

This package is auto-updated.

Last update: 2024-12-11 14:58:07 UTC


README

Di:

Latest Version on Packagist Scrutinizer Code Quality Build Status Total Downloads

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 ']
            ]
        ]
    ]
];