PHP code example of code-distortion / di-caller

1. Go to this page and download the library: Download code-distortion/di-caller 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/ */

    

code-distortion / di-caller example snippets


namespace MyApp;

class User
{
    public function __construct(
        private string $firstName,
        private string $lastName,
    ) {}
}

use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$result = DICaller::new(User::class)
    ->registerByName('firstName', 'Bob')
    ->registerByName('lastName', 'Smith')
    ->instantiate(); // new User instance

use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$callable = fn(User $user) => "hello {$user->firstName}";

$result = DICaller::new($callable)
    ->registerType(new User('Bob', 'Smith'))
    ->call(); // 'hello Bob'

use CodeDistortion\DICaller\DICaller;
use MyApp\Request;
use MyApp\ShoppingCart;
use MyApp\User;

$callable = fn(Request $request, User $user, float $duration)
    => "$user->name ({$request->getIp()}) - $duration seconds";

$user = new User('Bob', 'Smith');
$request = new Request(…);
$shoppingCart = new ShoppingCart(…);
$someId = 10;
$duration = 4.55;

$result = DICaller::new($callable)
    ->registerByType($user)         // <<<
    ->registerByType($request)      // <<<
    ->registerByType($shoppingCart) // <<<
    ->registerByType($someId)       // <<<
    ->registerByType($duration)     // <<<
    ->call(); // 'Bob (192.168.1.1) - 4.55 seconds'

use CodeDistortion\DICaller\DICaller;
use MyApp\Request;
use MyApp\ShoppingCart;
use MyApp\User;

$callable = fn(Request $request, User $user, float $duration)
    => "$user->name ({$request->getIp()}) - $duration seconds";

$user = new User('Bob', 'Smith');
$request = new Request(…);
$shoppingCart = new ShoppingCart(…);
$someId = 10;
$duration = 4.55;

$result = DICaller::new($callable)
    ->registerByType($user, User::class)                 // <<<
    ->registerByType($request, Request::class)           // <<<
    ->registerByType($shoppingCart, ShoppingCart::class) // <<<
    ->registerByType($someId)
    ->registerByType($duration)
    ->call(); // 'Bob (192.168.1.1) - 4.55 seconds'

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)
    ->registerByName('param1', 'hello') // <<<
    ->registerByName('param2', 'world') // <<<
    ->call(); // 'hello world'

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)
    ->registerByPosition(0, 'hello') // <<<
    ->registerByPosition(1, 'world') // <<<
    ->call(); // 'hello world'

use CodeDistortion\DICaller\DICaller;

$callable = fn(int|float $param1) => $param1; // <<<

$result = DICaller::new($callable)
    ->registerByType(1.1) // <<<
    ->call(); // 1.1

use CodeDistortion\DICaller\DICaller;

$callable = fn(ParentClass&ChildClass $param1) => $param1; // <<<

$result = DICaller::new($callable)
    ->registerByType(new ChildClass()) // <<<
    ->call(); // ChildClass

use CodeDistortion\DICaller\DICaller;

$callable = fn(int $param1, int ...$param2) => func_get_args(); // <<<

$result = DICaller::new($callable)
    ->registerByName('param1', 1)
    ->registerByName('param2', 2)
    ->call(); // [1, 2]

use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$user = DICaller::new(User::class)->instantiate();
// throws DICallerInstantiationException - when there are unresolved parameters

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)->call(); // throws DICallerCallableException

use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$instantiator = DICaller::new(User::class)
    ->registerByName('middleName', 'John');
$user = $instantiator->canInstantiate() // false - because $firstName and $lastName are unresolved
    ? $instantiator->instantiate()
    : null;

use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$user = DICaller::new(User::class)
    ->registerByName('middleName', 'John')
    ->instantiateIfPossible();
// null - because $firstName and $lastName are unresolved

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$caller = DICaller::new($callable)
    ->registerByName('param3', 'something');
if ($caller->canCall()) { // false - because $param1 and $param2 are unresolved
    $result = $caller->call();
}

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)
    ->registerByName('param3', 'something')
    ->callIfPossible(); // null - because $param1 and $param2 are unresolved