PHP code example of sterzik / di

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

    

sterzik / di example snippets


use Sterzik\DI\DI;

// get global DI container
$di = DI::instance();

// instantiate a service:
$service = $di->get(MyServiceClass::class);

// test if a service is available
$myServiceAvailable = $di->has(MyServiceClass::class);

use Sterzik\DI\DI;

// instantiate a separate custom container
$container = new DI();

// instantiate a service
$service = $di->get(MyServiceClass::class);

    // take configuration from $projectRoot/service-config.php
    define("DI_SERVICE_DEFINITIONS", "service-config.php");
    $di = DI::instance();

    // take configuration from /app/root/service-config.php
    DI::setServiceDefinitions("/app/root/service-config.php");
    $di = DI::instance();

    // set explicit service definitions (see later how service definitions look like)
    $config = [
        ...
    ];
    DI::setServiceDefinitions($config);
    $di = DI::instance();

    $di = new DI("config/services.php");

    $config = [
        ...
    ];
    $di = new DI($config);

use Sterzik\DI\DI;

$config = [
    // callable
    "service1" => fn($builder) =>
        $builder->setClass(MyService1Class::class),

    // array - reserved for future development
    "service2" => [
        "some" => "configuration"
    ],

    // static service resolving
    "service3" => new StaticServiceClass(),
];

$di = new DI($config);

// lead to: new MyService1Class()
$service1 = $di->get("service1");

// not yet implemented
// leads to Sterzik\DI\Exception\NotImplementedException exception
$service2 = $di->get("service2");

// leads to the static instance of StaticServiceInstance class
// passed to the configuration
$service3 = $di->get("service3");

use Sterzik\DI\DI;

$config = [
    // function returns builder:
    "service1" => function ($builder) {
        return $builder->setClass(MyService1Class::class);
    },

    // function returns null (none)
    "service2" => function ($builder) {
        $builder->setClass(MyService2Class::class);
    },

    // function returns anything else
    "service3" => function ($builder) {
        return new StaticServiceClass();
    },
];

$di = new DI($config);

// lead to: new MyService1Class()
$service1 = $di->get("service1");

// lead to: new MyService2Class() (same as in the case of service1)
$service2 = $di->get("service2");

// leads to the static instance of StaticServiceI stance class
// created in the callable service definition of service3
$service3 = $di->get("service3");

use Sterzik\DI\DI;

$config = ...;
$parameters = ...;

// $di has already preconfigured all parameters from the $parameters array
$di = new DI($config, $parameters);

// and these parameters may be changed even after instantiation of the container:
$di->setParameter("remoteUrl", "http://example.com");
$di->setParameters(["remoteUrl" => "https://example.com", "cacheFile" => "cache-file.dat"]);

$di->get(SomeService::class);

// now calling $di->setParameter() and $di->setParameters() throws an exception


return [
    "service" => function ($builder) {
        return $builder->setClass(MyServiceClass::class);
    },
];

return [
    MyServiceClass::class => fn($builder) => $builder
        ->setArgument(0, "FirstArgument")
        ->setArgument("argument2", "SecondArgument"),
];

return [
    MyServiceClass::class => fn($builder) => $builder
        ->setArguments("FirstArgument",argument2: "SecondArgument"),
];

return [
    MyServiceClass::class => fn($builder) => $builder
        ->putArguments([0 => "FirstArgument", "argument2" => "SecondArgument"], true),
];

return [
    '\\' => fn($builder) => $builder
        ->setArgument("url" => $url),

    MyServiceClass::class => fn($builder) => $builder
        ->resetArguments(),
];

$factory = function (string $argument) {
    return new Service($argument);
}

return [
    MyServiceClass::class => fn($builder) => $builder
        ->setFactory($factory)
        ->setArgument("argument", "someValue"),
];

// globally disable autowiring
return [
    '\\' => fn($builder) => $builder
        ->setAutowire(false),
    ...
];


// globally   '\\' => fn($builder) => $builder
        ->setRequireExplicitClass(true),

    ...
];


// set all services as public except service of id SomePublicServiceClass::class
return [
    '\\' => fn($builder) => $builder
        ->setPublic(false),

    SomePublicServiceClass::class => fn($builder) => $builder
        ->setPublic(true),
];


// after creation of MyServiceClass instance, the method setupUrl($url) will be called.
return [
    MyServiceClass::class => fn($builder) => $builder
        ->call("setupUrl", $url),
];

// after creation of MyServiceClass instance, the method setupUrl($url) will be called.
return [
    MyServiceClass::class => fn($builder) => $builder
        ->callArguments("setupUrl", [$url], false),
];

$service = new Service();

// both services resolve to the same object instance
return [
    "service1" => fn($builder) => $service,
    "service2" => fn($builder) => $builder->setService($service),
];

// service "subservice" will be wired to the constructor argument $subService of class SomeClass
return [
    "service" => fn($builder) => $builder
        ->setClass(SomeClass::class)
        ->setArgument("subService", $builder->get("subservice")),

    "subservice" => fn($builder) => $builder
        ->setClass(SubserviceClass::class),
];

// set the constructor first argument of service Service
// to the DI container parameter "url"
return [
    Service::class => fn($builder) => $builder
        ->setArguments($builder->parameter("url")),
];

// set the constructor argument $applicationRoot
// to the application root directory path
return [
    Service::class => fn($builder) => $builder
        ->setArgument("applicationRoot", $builder->getAppRoot()),
];

// set the constructor first argument of service Service
// to the absolute path of the file "config/config-file.conf" 
// interpreted relatively to the application root
return [
    Service::class => fn($builder) => $builder
        ->setArgument("configFile", $builder->resolvePath("config/config-file.conf")),
];