PHP code example of mbretter / stk-di

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

    

mbretter / stk-di example snippets


use Stk\Service\Injectable;

class MyService implements Injectable
{
...
} 

use Stk\Service\DumbContainer;
use Stk\Service\Factory;

class ServiceA implements Injectable
{

}

$container = new DumbContainer();
$container['config'] = [
    'param1' => 'foo',
    'param2' => 'bar'
];

$container['factory']  = new Factory($container); // put the service factory into the container
$container['serviceA'] = function (ContainerInterface $c) {
    return $c['factory']->get(ServiceA::class);
};


class ServiceA implements Injectable
{

}

class ServiceB implements Injectable
{
    protected $serviceA;

    // tell the factory to inject serviceA
    private function setServiceA(Injectable $serviceA)
    {
        $this->serviceA = $serviceA;
    }
}

$container['serviceA'] = function (ContainerInterface $c) {
    return $c['factory']->get(ServiceA::class);
};

$container['serviceB'] = function (ContainerInterface $c) {
    return $c['factory']->get(ServiceB::class);
};

$service = $this->container->get('serviceB');


class ServiceC implements Injectable
{
    protected $service;
    protected $whatever;

    public function __construct($serviceA, $whatever = [])
    {
        $this->service  = $serviceA;
        $this->whatever = $whatever;
    }
}


class ServiceK implements Injectable
{
    protected $config;
    public $param1;

    // $config should be passed at service declaration, $param1 at creation time
    public function __construct($config, $param1)
    {
        $this->config = $config;
        $this->param1 = $param1;
    }
}

// the container declaration for ServiceK, wrapped inside a Closure
$container['serviceK'] = function ($c) {
    return function ($param2) use ($c) {
        return $c['factory']->get(ServiceK::class, $c->get('config'), $param2);
    };
};

// accessing the service
/** @var Closure $serviceK */
$factory  = $container->get('serviceK');

/** @var ServiceK $serviceK */
$serviceK = $factory('val2');

$serviceK->param1 === 'val2';


class ServiceH implements Injectable
{
    public $arg1 = null;
    public $arg2 = null;

    public function __construct($arg1 = null, $arg2 = null)
    {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
    }
}

class ServiceE implements Injectable
{
    /** @var OnDemand */
    public $onDemand;

    // trigger DI of OnDemand service H
    private function setService(OnDemand $serviceH)
    {
        $this->onDemand = $serviceH;
    }

    public function getService()
    {
        return $this->onDemand->getInstance();
    }

    public function newService()
    {
        return $this->onDemand->newInstance();
    }
}

$container['serviceE'] = function ($c) {
    return $c['factory']->get(ServiceE::class);
};
$container['onDemandServiceH'] = function ($c) {
    // the protect method wraps the service inside into the OnDemand injectable
    return $c['factory']->protect(ServiceH::class);
};

/** @var OnDemand $serviceH */
$serviceH = $container->get('serviceH');
$inst     = $serviceH->newInstance('foo', 'bar');

// or if you want to treat them as singleton
$inst     = $serviceH->getInstance('foo', 'bar');

/** @var ServiceE $serviceE */
$serviceE = $container->get('serviceE');

$svc = $serviceE->getService();


class ServiceJ implements Injectable
{
    /** @var OnDemand */
    protected $foreignService = null;

    private function setForeignServices(OnDemand $foreignService)
    {
        $this->foreignService = $foreignService;
    }
    
    public function getForeignService()
    {
        return $this->foreignService->getInstance();
    }
}

$container['foreignService'] = function ($c) {
    return $c['factory']->protect(stdClass::class);
};
$container['serviceJ'] = function ($c) {
    return $c['factory']->get(ServiceJ::class);
};

/** @var ServiceJ $serviceJ */
$serviceJ = $container->get('serviceJ');

$std = $serviceJ->getForeignService();


use Stk\Service\OnDemand;

trait DependsOnServiceB 
{
    /** @var OnDemand */
    protected $_serviceB;

    private function setServiceB(OnDemand $serviceB)
    {
        $this->_serviceB = $serviceB;

        return $this;
    }

    /**
     * @return ServiceB
     */
    protected function serviceB()
    {
        return $this->_serviceB->getInstance();
    }
}

...

class ServiceJ implements Injectable
{
    use DependsOnServiceB;
}