PHP code example of wlib / dibox

1. Go to this page and download the library: Download wlib/dibox 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/ */

    

wlib / dibox example snippets


use wlib\Di\DiBox;

$box = new DiBox();

// Créer une instance de Foo\Bar
$bar = $box->make(Foo\Bar::class);

// Passer des arguments au constructeur
$bar = $box->make(Foo\Bar::class, ['a', 'b']); // indexés
$bar = $box->make(Foo\Bar::class, ['sGreetings' => 'Hello']); // nommés, ici, le constructeur attend l'entrée `$sGreetings`

class Bar { public function __construct(public Baz $baz); }
class Baz {}

$bar = $box->make(Bar::class);

// $bar->baz sera une instance de Baz

$box->bind(Foo\Bar::class);
$bar = $box->get(Foo\Bar::class); // instance de Foo\Bar

$box->bind('MyBar', Foo\Bar::class);
$bar = $box->get('MyBar'); // toujours une instance de Foo\Bar mais depuis son p'tit nom

$bar = new Foo\Bar();
$box->bind('MyBar', $bar);
$samebar = $box->get('MyBar'); // toujours pareil !

$box->bind('MyBaz', function($box, $args)
{
	return new Foo\Baz($box->get('MyBar'));
});

$baz = $box->get('MyBaz'); // instance de Foo\Baz prête à bosser, classe non ?

$box->bind('one.string', 'DiBox has all of a great one !');
$box->bind('one.integer', 2023);
$box->bind('one.array', [1, 2, 3]);

echo $box->get('one.string'));  // Affiche "DiBox has ...')
echo $box->get('one.integer')); // Affiche "2023"
echo $box->get('one.array'));   // Attention piège, ça affiche quoi ?

$box['make.b']   = function ($box, $args) { return new B(new A()); };
$box['integer']  = 4046;
$box['list']     = ['a', 'b', 'c'];
$box['class.a']  = A::class;

$b        = $box['make.b'];  // instance de B
$iInteger = $box['integer']; // 4096
$aList    = $box['list'];    // cf. 5 lignes au dessus
$a        = $box['class.a']; // bon, vous maîtrisez normalement

unset($box['class.a']);
isset($box['class.a']); // >> false

class Counter
{
    protected $count = 0;
    
    public function increment(): int
    {
        return $this->count++;
    }
}

$box->singleton(Counter::class);

echo $box->get(Counter::class)->increment(); // 0
echo $box->get(Counter::class)->increment(); // 1
echo $box->get(Counter::class)->increment(); //	2

$box->has('MyBar');    // pour vérifier la présense d'une dépendance
$box->remove('MyBar'); // pour retirer cette dépendance que vous ne voulez plus voir
$box->empty();         // pour vider le conteneur

// Exemple (classique ?) d'un fournisseur des services HTTP d'une application
class HttpProvider implements DiBoxProvider
{
	public function provide(DiBox $box)
	{
		$box->bind('http.request', function($box, $args)
		{
			return MyApp\Http\Request();
		});

		$box->bind('http.response', function($box, $args)
		{
			return MyApp\Http\Response($box['http.request']);
		});
	}
}

// Et pour le fournir au conteneur, rien de plus simple :
$box->register(HttpProvider::class);

$response = $box->get('http.response'); // Et vous avez une réponse HTTP prête à servir vos applications / API