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
{
/**
* @var \rg\injektor\DependencyInjectionContainer
*/
private $dic;
/**
* @var \rg\injektor\WritingFactoryGenerator
*/
private $factoryGenerator;
/**
* @var string
*/
private $root;
protected function configure()
{
$this->setDescription('generates factories for dependency injection container');
$this->setHelp('generates factories for dependency injection container');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
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);
}
/**
* @param OutputInterface $output
*/
private function processAllDirectories(OutputInterface $output)
{
$this->processDirectory($this->root . DIRECTORY_SEPARATOR . 'folderWithPhpClasses', $output);
}
/**
* @param $directory
* @param OutputInterface $output
*/
private function processDirectory($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);
}
}
/**
* @param $fullpath
* @param OutputInterface $output
*/
private function processFile($fullpath, OutputInterface $output)
{
$output->writeln('Process file [' . $fullpath . ']');
class Foo
{
/**
* @inject
* @param Bar $bar
*/
public function __construct(Bar $bar)
{
}
}
class Bar
{
}
$dic->getInstanceOfClass('Foo');
class Foo
{
/**
* @inject
* @param Bar $bar
*/
public function __construct(Bar $bar)
{
}
}
/**
* @singleton
*/
class Bar
{
private function __construct()
{
}
public static function getInstance()
{
}
}
$dic->getInstanceOfClass('Foo');
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
class Bar
{
}
$dic->getInstanceOfClass('Foo');
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
/**
* @implementedBy BarImpl
*/
interface Bar
{
}
class BarImpl implements Bar
{
}
$dic->getInstanceOfClass('Foo');
'Bar' => array(
'class' => 'BarImpl'
)
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
/**
* @providedBy BarProvider
*/
interface Bar
{
}
class BarImpl implements Bar
{
}
class BarProvider implements rg\injektor\Provider
{
public function get()
{
return new BarImpl();
}
}
$dic->getInstanceOfClass('Foo');
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
/**
* @singleton
*/
class Bar
{
}
$instanceOne = $dic->getInstanceOfClass('Foo');
$instanceTwo = $dic->getInstanceOfClass('Foo');
'Bar' => array(
'singleton' => true
)
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
/**
* @singleton
*/
class Bar
{
public function __construct($arg)
{
}
}
$instanceOne = $dic->getInstanceOfClass('Foo', array('arg' => 1));
$instanceTwo = $dic->getInstanceOfClass('Foo', array('arg' => 2));
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
/**
* @service
*/
class Bar
{
}
$instanceOne = $dic->getInstanceOfClass('Foo');
$instanceTwo = $dic->getInstanceOfClass('Foo');
'Bar' => array(
'service' => true
)
class Foo
{
/**
* @inject
* @var Bar
*/
protected $bar;
}
/**
* @service
*/
class Bar
{
public function __construct($arg)
{
}
}
$instanceOne = $dic->getInstanceOfClass('Foo', array('arg' => 1));
$instanceTwo = $dic->getInstanceOfClass('Foo', array('arg' => 2));
class Foo
{
/**
* @inject
*/
public function __construct($bar)
{
}
}
/**
* @singleton
*/
class Bar
{
private function __construct()
{
}
/**
* @inject
*/
public static function getInstance($foo, $buzz)
{
}
}
$dic->getInstanceOfClass('Foo');
class Foo
{
/**
* @inject
* @var Bar {"foo":456,"buzz":"content"}
*/
protected $propertyInjection;
/**
* @inject
* @param Bar $bar {"foo":123,"buzz":"content"}
*/
public function __construct(Bar $bar)
{
}
}
/**
* @singleton
*/
class Bar
{
private function __construct()
{
}
/**
* @inject
*/
public static function getInstance($foo, $buzz)
{
}
}
$dic->getInstanceOfClass('Foo');
class Foo
{
/**
* @inject
*/
public function __construct($val, Bar $bar, Buzz $buzz)
{
}
}
class Bar
{
}
class Buzz
{
}
$dic->getInstanceOfClass('Foo', array(
'val' => 123,
'buzz' => new Buzz()
));
class Foo
{
/**
* @var Bar
* @named barOne
*/
protected $bar;
/**
* @inject
* @param Bar $one
* @param Bar $two
* @param Bar $default
* @named barOne $one
* @named barTwo $two
*/
public function __construct(Bar $one, Bar $two, Bar $default)
{
}
}
interface Bar
{
}
class BarImplDefault implements Bar
{
}
class BarImplOne implements Bar
{
}
class BarImplTwo implements Bar
{
}
$dic->getInstanceOfClass('Foo');
/**
* @implementedBy BarImplDefault
* @implementedBy barOne BarImplOne
* @implementedBy barTwo BarImplTwo
*/
interface Bar
{
}
class BarImplDefault implements Bar
{
}
class BarImplOne implements Bar
{
}
class BarImplTwo implements Bar
{
}
/**
* @providedBy BarProvider
* @providedBy barOne BarProvider {"name" : "barOne"}
* @providedBy barTwo BarProvider {"name" : "barOne"}
*/
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
{
}
class Foo
{
/**
* @inject
*/
public function doSomething(Bar $bar)
{
}
}
class Bar
{
}
$foo = new Foo();
$dic->callMethodOnObject($foo, 'doSomething');
class Foo
{
/**
* @inject
*/
public function doSomething(Bar $bar, $foo)
{
}
}
class Bar
{
}
$foo = new Foo();
$dic->callMethodOnObject($foo, 'doSomething', array('foo' => 'value'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.