PHP code example of sysvyz / brunt

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

    

sysvyz / brunt example snippets



$injector = new Brunt\Injector(null);

/** @var Engine $engine */
$engine = $injector->get(Engine::class);



$engine = $injector->get(Engine::class);
//is equivalent to
$engine = $injector->{Engine::class};


$injector = new Injector(null);

//                          TOKEN           PROVIDER            CLASS
$injector->addProviders([Engine::class => ClassProvider::init(HeavyEngine::class)]);

/** @var Engine $engine */
$engine = $injector->get(Engine::class);

$injector->bind([

    bind('%SomeValue%')
    ->toValue(3.1415),
    
    bind(Car::class)
    ->toClass(Car::class),
    
    bind(Request::class)
        ->toFactory(function (Injector $injector) {
            return Request::createFromGlobals();
        })
])


$injector = new Injector(null);

//                          TOKEN           PROVIDER            CLASS              SINGLETON
$injector->addProviders([Engine::class => ClassProvider::init(HeavyEngine::class)->singleton()]);

/** @var Engine $engine */
$engine = $injector->get(Engine::class);

$injector = new Injector(null);
$injector->bind([
    
    bind(Car::class)
    ->toClass(Car::class)->singleton(),
    
]);
$car = $injector->get(Car::class)

$injector = new Injector(null);

//                          TOKEN           PROVIDER            CLASS              LAZY
$injector->addProviders([Engine::class => ClassProvider::init(HeavyEngine::class)->lazy()]);

/** @var Engine $engine */
$engine = $injector->get(Engine::class); //returns a proxy object

$injector = new Injector(null);
$injector->bind([
    
    bind(Car::class)
    ->toClass(Car::class)->lazy(),
    
]);
$car = $injector->get(Car::class) //returns a proxy object 

$car->honk() //creates the actual car and honks


bind(Car::class)->lazy()->singleton()

ClassProvider::init(Car::class)->lazy()->singleton();


$injector->addProviders([
    HeavyEngine::class => ClassProvider::init(HeavyEngine::class)->lazy()
    Engine::class => AliasProvider::init(HeavyEngine::class)
]);
$heavyEngine = $injector->get(Engine::class); //returns a proxy object for HeavyEngine