PHP code example of rg / injektor

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

    

rg / injektor example snippets


$configuration = new \rg\injektor\Configuration($pathToConfigFile, $pathToFactoryDirectory);
$dic = new \rg\injektor\DependencyInjectionContainer($configuration);

$instance = $dic->getInstanceOfClass('ClassName');
$result = $dic->callMethodOnObject($instance, 'methodName');

$configuration = new \rg\injektor\Configuration($pathToConfigFile, $pathToFactoryDirectory);
$dic = new \rg\injektor\FactoryDependencyInjectionContainer($configuration);

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use rg\injektor\WritingFactoryGenerator;

class GenerateDependencyInjectionFactories extends \Symfony\Component\Console\Command\Command
{
    private \rg\injektor\DependencyInjectionContainer $dic;

    private \rg\injektor\WritingFactoryGenerator $factoryGenerator;

    private string $root;

    protected function configure()
    {
        $this->setDescription('generates factories for dependency injection container');
        $this->setHelp('generates factories for dependency injection container');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Generating Factories');

        $this->root = '/path/to/your/project';

        $factoryPath = $this->root . '/folder/for/generated/factories';

        if (!file_exists($factoryPath)) {
            mkdir($factoryPath, 0777, true);
        }

        $pathToConfigFile = '/config/dic.php';

        $configuration = new \rg\injektor\Configuration($pathToConfigFile, $factoryPath);
        $this->dic = new \rg\injektor\FactoryDependencyInjectionContainer($configuration);

        $this->factoryGenerator = new WritingFactoryGenerator($this->dic->getConfig(), $factoryPath);

        $this->factoryGenerator->cleanUpGenerationDirectory($factoryPath);

        $this->processAllDirectories($output);
    }

    private function processAllDirectories(OutputInterface $output)
    {
        $this->processDirectory($this->root . DIRECTORY_SEPARATOR . 'folderWithPhpClasses', $output);
    }

    private function processDirectory(string $directory, OutputInterface $output)
    {
        $output->writeln('Directory: ' . $directory);
        $directoryIterator = new \RecursiveDirectoryIterator($directory);
        $iterator = new \RecursiveIteratorIterator($directoryIterator);
        $regexIterator = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
        foreach ($regexIterator as $file) {
            $this->processFile($file[0], $output);
        }
    }

    private function processFile(string $fullpath, OutputInterface $output)
    {
        $output->writeln('Process file [' . $fullpath . ']');

        

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject]
    public function __construct(Bar $bar)
    {

    }
}

class Bar
{

}

$dic->getInstanceOfClass('Foo');

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Singleton;

class Foo
{
    #[Inject]
    public function __construct(Bar $bar)
    {

    }
}

 #[Singleton]
class Bar
{
    private function __construct()
    {

    }

    public static function getInstance()
    {

    }
}

$dic->getInstanceOfClass('Foo');

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

class Bar
{

}

$dic->getInstanceOfClass('Foo');

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\ImplementedBy;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

 #[ImplementedBy(className: BarImpl::class)]
interface Bar
{

}

class BarImpl implements Bar
{

}

$dic->getInstanceOfClass('Foo');

'Bar' => array(
    'class' => 'BarImpl'
)

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\ProvidedBy;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

 #[ProvidedBy(className: BarProvider::class)]
interface Bar
{

}

class BarImpl implements Bar
{

}

class BarProvider implements rg\injektor\Provider
{
    public function get()
    {
        return new BarImpl();
    }
}

$dic->getInstanceOfClass('Foo');

'Bar' => array(
    'provider' => array(
        'class' => 'BarImpl'
    )
)

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\ProvidedBy;
use rg\injektor\attributes\Param;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

#[ProvidedBy(className: BarProvider::class, overwriteParams: [new Param('foo', 'bar')])]
interface Bar
{

}

class BarImpl implements Bar
{

}

class BarProvider implements rg\injektor\Provider
{

    #[Inject]
    public function __construct(SomeClass $someClass, $foo)
    {
    }

    public function get()
    {
        return new BarImpl();
    }
}

$dic->getInstanceOfClass('Foo');

'Bar' => array(
    'provider' => array(
        'class' => 'BarImpl',
        'params' => array(
            'foo' => 'bar',
        )
    )
)

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Singleton;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

#[Singleton]
class Bar
{

}

$instanceOne = $dic->getInstanceOfClass('Foo');
$instanceTwo = $dic->getInstanceOfClass('Foo');

'Bar' => array(
    'singleton' => true
)

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Singleton;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

#[Singleton]
class Bar
{
    public function __construct($arg)
    {
    }
}

$instanceOne = $dic->getInstanceOfClass('Foo', array('arg' => 1));
$instanceTwo = $dic->getInstanceOfClass('Foo', array('arg' => 2));

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Singleton;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

#[Service]
class Bar
{

}

$instanceOne = $dic->getInstanceOfClass('Foo');
$instanceTwo = $dic->getInstanceOfClass('Foo');

'Bar' => array(
    'service' => true
)

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Service;

class Foo
{
    #[Inject]
    protected Bar $bar;
}

#[Service]
class Bar
{
    public function __construct($arg)
    {
    }
}

$instanceOne = $dic->getInstanceOfClass('Foo', array('arg' => 1));
$instanceTwo = $dic->getInstanceOfClass('Foo', array('arg' => 2));

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Singleton;

class Foo
{
    #[Inject]
    public function __construct($bar)
    {

    }
}

#[Singleton]
class Bar
{
    private function __construct()
    {

    }

    #[Inject]
    public static function getInstance($foo, $buzz)
    {

    }
}

$dic->getInstanceOfClass('Foo');

'Foo' => array(
    'params' => array(
        'bar' => array(
            'class' => 'Bar'
        )
    )
),
'Bar' = array(
    'params' => array(
        'foo' => array(
            'value' => 'fooBar'
        ),
        'buzz' => array(
            'value' => true
        )
    )
)

use rg\injektor\attributes\Inject;
use rg\injektor\attributes\Singleton;
use rg\injektor\attributes\Param;

class Foo
{
     #[Inject(overwriteParams: [
        new Param('foo', 456),
        new Param('buzz', 'content')
     ])]
    protected Bar $propertyInjection;


    public function __construct(
        #[Inject(overwriteParams: [)]
            new Param('foo', 123),
            new Param('buzz', 'content')
        ])]
        Bar $bar
    ) {

    }
}

#[Singleton]
class Bar
{
    private function __construct()
    {

    }

    #[Inject]
    public static function getInstance($foo, $buzz)
    {

    }
}

$dic->getInstanceOfClass('Foo');

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject]
    public function __construct($val, Bar $bar, Buzz $buzz)
    {

    }
}

class Bar
{
}

class Buzz
{
}

$dic->getInstanceOfClass('Foo', array(
    'val' => 123,
    'buzz' => new Buzz()
));

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject(named: 'barOne')]
    protected Bar $bar;

    #[Inject]
    public function __construct(
        #[Inject(named: 'barOne')]
        Bar $one,
        #[Inject(named: 'barTwo')]
        Bar $two,
        // default implementation,  Bar
{

}

$dic->getInstanceOfClass('Foo');

'Bar' => array(
    'class' => 'BarImplDefault'
    'named' => array(
        'barOne' => 'BarImplOne',
        'barTwo' => 'BarImplTwo'
    )
)

use rg\injektor\attributes\ImplementedBy;

#[ImplementedBy(className: BarImplDefault::class)]
#[ImplementedBy(className: BarImplOne::class, named: 'barOne')]
#[ImplementedBy(className: BarImplTwo::class, named: 'barTwo')]
interface Bar
{

}

class BarImplDefault implements Bar
{

}

class BarImplOne implements Bar
{

}

class BarImplTwo implements Bar
{

}

use rg\injektor\attributes\ImplementedBy;

#[ImplementedBy(BarImplDefault::class)]
#[ImplementedBy(className: BarImplOne::class, named: 'barOne')]
#[ImplementedBy(className: BarImplTwo::class, named: 'barTwo')]
interface Bar
{

}

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject(named: 'barOne')]
    protected Bar $bar;

    #[Inject]
    public function __construct(
        #[Inject(named: 'barOne')]
        Bar $one,
        #[Inject(named: 'barTwo')]
        Bar $two,
        Bar $default,
    ) {
    }
}

interface Bar
{

}

$dic->getInstanceOfClass('Foo');

'Bar' => array(
    'provider' => array(
        'class' => 'BarProvider'
    ),
    'namedProviders' => array(
        'barOne' => array(
            'class' => 'BarProvider',
            'parameters' => array('name' => 'barOne')
        ),
        'barTwo' => array(
            'class' => 'BarProvider',
            'parameters' => array('name' => 'barTwo')
        )
    )
)

use rg\injektor\attributes\ProvidedBy;
use rg\injektor\attributes\Param;

#[ProvidedBy(BarProvider::class)]
#[ProvidedBy(BarProvider::class, named: 'barOne', overwriteParams: [new Param('name', 'barOne')])]
#[ProvidedBy(BarProvider::class, named: 'barTwo', overwriteParams: [new Param('name', 'barTwo')])]
interface Bar
{

}

class BarProvider implements rg\injektor\Provider
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function get()
    {
        switch ($this->name) {
            case 'barOne':
                return new BarImplOne();
            case 'barTwo':
                return new BarImplTwo();
        }

        return new BarImplDefault();
    }
}

class BarImplDefault implements Bar
{

}

class BarImplOne implements Bar
{

}

class BarImplTwo implements Bar
{

}

use rg\injektor\attributes\ProvidedBy;
use rg\injektor\attributes\Param;

#[ProvidedBy(BarProvider::class, named: 'default')]
#[ProvidedBy(BarProvider::class, named: 'barOne', overwriteParams: [new Param('name', 'barOne')])]
#[ProvidedBy(BarProvider::class, named: 'barTwo', overwriteParams: [new Param('name', 'barTwo')])]
interface Bar
{

}

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject]
    public function doSomething(Bar $bar)
    {
    }
}

class Bar
{

}

$foo = new Foo();
$dic->callMethodOnObject($foo, 'doSomething');

use rg\injektor\attributes\Inject;

class Foo
{
    #[Inject]
    public function doSomething(Bar $bar, $foo)
    {
    }
}

class Bar
{

}

$foo = new Foo();
$dic->callMethodOnObject($foo, 'doSomething', array('foo' => 'value'));