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
$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);
});
$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');
class MyApp implements Iterator, ArrayAccess, Countable, Interop\Container\ContainerInterface
{
use Idealogica\InDI\ContainerTrait;
use Idealogica\InDI\ArrayAccessTrait;
use Idealogica\InDI\PropertyAccessTrait;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.