PHP code example of idealogica / indi

1. Go to this page and download the library: Download idealogica/indi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

idealogica / indi example snippets


$container = new Idealogica\InDI\Container();

$container->value1 = 'string'; // using container property
$container['value2'] = ['array']; // using array notation
$container->add('value3', new stdClass()); // using container method

var_export($container->value1); // 'string'
var_export($container['value2']); // array (0 => 'array',)
var_export($container->get('value3')); // stdClass::__set_state(array())

foreach($container as $id => $value) {}

var_export(isset($container->value1)); // true
var_export(isset($container['value2'])); // true
var_export($container->has('value3')); // true
var_export(isset($container->value4)); // false

unset($container->value1);
unset($container['value2']);
$container->remove('value3');


$dbDriver = new DBAL\MySql('localhost', 'database');
$container->db = new DBAL\Connection($dbDriver, 'user', 'pass');

// closure
function (): DBAL\Connection
{
    $dbDriver = new DBAL\MySql('localhost', 'database');
    return new DBAL\Connection($dbDriver, 'user', 'pass');
}

// function
function defineDB(): DBAL\Connection
{
    $dbDriver = new DBAL\MySql('localhost', 'database');
    return new DBAL\Connection($dbDriver, 'user', 'pass');
}

// static method
class DB
{
    public static function define(): DBAL\Connection
    {
        $dbDriver = new DBAL\MySql('localhost', 'database');
        return new DBAL\Connection($dbDriver, 'user', 'pass');
    }
}

// invokable object
new class
{
    public function __invoke(): DBAL\Connection
    {
        $dbDriver = new DBAL\MySql('localhost', 'database');
        return new DBAL\Connection($dbDriver, 'user', 'pass');
    }
}

// object method
[new class
{
    public function define(): DBAL\Connection
    {
        $dbDriver = new DBAL\MySql('localhost', 'database');
        return new DBAL\Connection($dbDriver, 'user', 'pass');
    }
}, 'define']

// sharing service
$container->addShared('db', function (): DBAL\Connection
{
    $dbDriver = new DBAL\MySql('localhost', 'database');
    return new DBAL\Connection($dbDriver, 'user', 'pass');
});

// you can share any kind of data
$container->addShared('number', function (): int
{
    return rand(0, 5);
});

// factory service
$container->addFactory('view', function (string $template, array $parms = []): View
{
    return (new View('path/to/templates'))->setTemplate(template)->setParms(parms);
});

// you can produce any kind of data
$container->addFactory('number', function (int $min, int $max): int
{
    return rand($min, $max);
});

// shared value
$db1 = $container->db(); // returns DBAL\Connection instance
$db2 = $container->db(); // returns the same DBAL\Connection instance
var_export($db1 === $db2); // true

// factory values
$view1 = $container->view('template', ['parm' => 'value']); // returns View instance
$view2 = $container->view('template', ['parm' => 'value']); // returns another View instance
var_export($view1 === view2); // false

$getDb = $container->db;
$getView = $container->view;

// shared value
$db1 = $getDb(); // returns DBAL\Connection instance
$db2 = $getDb(); // returns the same DBAL\Connection instance
var_export($db1 === $db2); // true

// factory values
$view1 = $getView('template', ['parm' => 'value']); // returns View instance
$view2 = $getView('template', ['parm' => 'value']); // returns another View instance
var_export($view1 === $view2); // false

$container->dbHost = 'localhost';
$container->dbDatabase = 'database';
$container->dbUser = 'user';
$container->dbPassword = 'pass';
$container->templatesPath = 'path/to/templates';

$container->dbDriver = new DBAL\MySql($container->dbHost, $container->dbDatabase);
$container->db = new DBAL\Connection($container->dbDriver, $container->dbUser, $container->dbPassword);

$container->addShared('dbDriver', function (): DBAL\MySql
{
    return new DBAL\MySql($this->dbHost, $this->dbDatabase);
});
$container->addShared('db', function (): DBAL\Connection
{
    return new DBAL\Connection($this->dbDriver(), $this->dbUser, $this->dbPassword);
});

// shared value
$container->addShared('dbDriver', function (Interop\Container\ContainerInterface $container): DBAL\MySql
{
    new DBAL\MySql($container->dbHost, $container->dbDatabase);
});
$container->addShared('db', function (Idealogica\InDI\Container $container): DBAL\Connection
{
    return new DBAL\Connection($container->dbDriver(), $container->dbUser, $container->dbPassword);
});
$db = $container->db();

// factory values
$container->addFactory('view', function (
    string $template,
    Idealogica\InDI\Container $container,
    array $parms = []): View
{
    return (new View($container->templatesPath))->setTemplate(template)->setParms(parms);
});
$view = $container->view('template', ['parm' => 'value']);

class DbProvider
{
    public function __invoke(
        Interop\Container\ContainerInterface $container,
        string $host,
        string $database,
        string $user,
        string $password)
    {
        $container->addShared('dbDriver', function () use ($host, $database)
        {
            return new DBAL\MySql($host, $database);
        });
        $container->addShared('db', function () use ($user, $password)
        {
            return new DBAL\Connection($this->dbDriver(), $user, $password);
        });
    }
}


// 'localhost', 'database', 'user', 'pass' are additional arguments of value provider
$container->register(new DbProvider(), 'localhost', 'database', 'user', 'pass');

if($foreignContainer instanceof Interop\Container\ContainerInterface)
{
    var_export($foreignContainer->has('dbDriver')); // true
    $container = new InDI\Container($foreignContainer);
    var_export($container->has('dbDriver')); // true
}

if($foreignContainer instanceof Interop\Container\ContainerInterface)
{
    var_export($foreignContainer->has('dbDriver')); // true
    $container = new Idealogica\InDI\Container($foreignContainer, Idealogica\InDI\DELEGATE_LOOKUP);
    var_export($container->has('dbDriver')); // false
    $container->addShared('db', function ()
    {
        var_export($this->has('dbDriver')); // true
    });
}

$container = new Idealogica\InDI\Container();

class MyApp extends Idealogica\InDI\Container {}

class MyApp implements Iterator, ArrayAccess, Countable, Interop\Container\ContainerInterface
{
    use Idealogica\InDI\ContainerTrait;
    use Idealogica\InDI\ArrayAccessTrait;
    use Idealogica\InDI\PropertyAccessTrait;
}