rg / injektor
Dependency Injection container inspired by google-guice
Installs: 72 668
Dependents: 2
Suggesters: 0
Security: 0
Stars: 39
Watchers: 10
Forks: 13
Open Issues: 0
pkg:composer/rg/injektor
Requires
- php: >=8.1
- doctrine/annotations: ^1.12|^2.0.0
- laminas/laminas-code: ^4.13
- psr/log: ^1.0.0
Requires (Dev)
- friendsofphp/proxy-manager-lts: ^1.0
- phpunit/phpunit: ^9.0.0
- roave/better-reflection: ^5.0.0
Suggests
- ocramius/proxy-manager: For lazy loading
This package is auto-updated.
Last update: 2025-10-10 07:58:55 UTC
README
rg\injektor is a sophisticated dependency injection container for PHP that was inspired by Guice. Unlike other reflection based containers rg\injektor includes a factory class generator that you can use to prevent the use of reflection on production.
Prerequisites
This library needs PHP 8.1+.
It has been tested using PHP 8.1 and PHP 8.2.
Installation
You can install the library directly with composer. Just run this command in your project directory:
$ composer require rg/injektor
Usage
After you installed rg\injektor you can use it like this:
$configuration = new \rg\injektor\Configuration($pathToConfigFile, $pathToFactoryDirectory); $dic = new \rg\injektor\DependencyInjectionContainer($configuration); $instance = $dic->getInstanceOfClass('ClassName'); $result = $dic->callMethodOnObject($instance, 'methodName');
For more details on the specific features of rg\injektor see below.
If you use some kind of MVC framework it is recommended to include rg\injektor in your front controller to create your controller objects and call methods on them.
Generating Factories
By default rg\injektor relies heavily on Reflection which is fine for your development environment but would slow down your production environment unnecessarily. So you should use the built in possiblity to use generated factory classes instead. In order to do this you have to generate these factories before deploying your project.
First you have to use the \rg\injektor\FactoryDependencyInjectionContainer class in your code:
$configuration = new \rg\injektor\Configuration($pathToConfigFile, $pathToFactoryDirectory); $dic = new \rg\injektor\FactoryDependencyInjectionContainer($configuration);
If no factories are present \rg\injektor\FactoryDependencyInjectionContainer falls back to Reflection.
To generate factories you have to write a small script that iterates over your PHP files and create factories for each of them. Here is an example of such a script based on the Symfony Console Component:
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 . ']'); require_once $fullpath; $astLocator = (new \Roave\BetterReflection\BetterReflection())->astLocator(); $reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator($fileName, $astLocator)); $classes = $reflector->reflectAllClasses(); foreach ($classes as $class) { $generator->processClass($class->getName()); } } private function processClass(\Laminas\Code\Reflection\ClassReflection $class) { if (!$class->isInstantiable()) { return; } $this->factoryGenerator->processFileForClass($class->name); } }
Features
Constructor Injection
use rg\injektor\attributes\Inject; class Foo { #[Inject] public function __construct(Bar $bar) { } } class Bar { } $dic->getInstanceOfClass('Foo');
An instance of Bar will be injected as the constructor argument $bar. Of course Bar could use dependency injection as well. The container can inject any classes that are injectable because:
- they have a @inject annotation or
rg\injektor\attributes\Inject
attribute at the constructor - they have a constructor without arguments
- they have no constructor
- the arguments are optional
- the arguments are configured (see below)
A constructor can be either a __construct method or a static getInstance method if the class is configured as singleton and the __construct method is private or protected.
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');
Property Injection
use rg\injektor\attributes\Inject; class Foo { #[Inject] protected Bar $bar; } class Bar { } $dic->getInstanceOfClass('Foo');
Field $bar will have an instance of Bar. In order for this to work the field can not be private but has to be protected or public. This can also be combined with constructor injection.
Inject Concrete Implementation
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');
Instead of Bar, BarImpl is injected into $bar. You can also configure this in the dependecy injection configuration instead of using annotations
'Bar' => array( 'class' => 'BarImpl' )
Using Provider Classes
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');
Instead of Bar, the return value of BarProvider's get Method (BarImpl) is injected into $bar. You can also configure this in the dependecy injection configuration instead of using annotations
'Bar' => array( 'provider' => array( 'class' => 'BarImpl' ) )
Passing fixed data to providers
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');
Here the provider gets an additional instance of SomeClass injected. The variable $foo is set to 'bar'. You can also configure this in the config:
'Bar' => array( 'provider' => array( 'class' => 'BarImpl', 'params' => array( 'foo' => 'bar', ) ) )
Inject as Singleton
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');
Both $instanceOne and $instanceTwo will have the same instance of Bar injections.
You can also configure this in the dependecy injection configuration instead of using annotations
'Bar' => array( 'singleton' => true )
Note that for a singleton injektor analizes the given arguments of the injected class to determine if the wanted instance is already created or not.
That means in this example:
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));
$instanceOne and $instanceTwo will be different instances. This feature comes with a speed price though, so if you want to have the same instance regardless of the parameter are always pass in the same or inject all parameters, mark it as a service instead (see below).
Injecting as service
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');
Both $instanceOne and $instanceTwo will have the same instance of Bar injections.
You can also configure this in the dependecy injection configuration instead of using annotations
'Bar' => array( 'service' => true )
In contrast to singletons, In a service this example
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));
would lead to $instanceOne and $instanceTwo being the same object instance.
Configuring parameters
You can also configure the content of all or some parameters that the container should pass to the __construct or getInstance method in the configuration instead of letting the container guess them from typehints:
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');
Configuration:
'Foo' => array( 'params' => array( 'bar' => array( 'class' => 'Bar' ) ) ), 'Bar' = array( 'params' => array( 'foo' => array( 'value' => 'fooBar' ), 'buzz' => array( 'value' => true ) ) )
Alternatively you can also configure this with annotations
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');
Pass additional parameters on runtime
You also can pass some values to the new instance on runtime.
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() ));
This can also be combined with configured parameters.
Named injection
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, requires #[Inject] on the constructor level Bar $default, #[Inject] // works the same as above, but doesn't require #[Inject] on the constructor level Bar $default2, ) { } } interface Bar { } class BarImplDefault implements Bar { } class BarImplOne implements Bar { } class BarImplTwo implements Bar { } $dic->getInstanceOfClass('Foo');
Configuration:
'Bar' => array( 'class' => 'BarImplDefault' 'named' => array( 'barOne' => 'BarImplOne', 'barTwo' => 'BarImplTwo' ) )
You can also configure this directly with annotations
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 { }
It is also possible to name the default implementation, so that our configuration looks a bit cleaner. The result is the same:
use rg\injektor\attributes\ImplementedBy; #[ImplementedBy(BarImplDefault::class)] #[ImplementedBy(className: BarImplOne::class, named: 'barOne')] #[ImplementedBy(className: BarImplTwo::class, named: 'barTwo')] interface Bar { }
Named providers
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');
Configuration:
'Bar' => array( 'provider' => array( 'class' => 'BarProvider' ), 'namedProviders' => array( 'barOne' => array( 'class' => 'BarProvider', 'parameters' => array('name' => 'barOne') ), 'barTwo' => array( 'class' => 'BarProvider', 'parameters' => array('name' => 'barTwo') ) ) )
You can also configure this directly with annotations
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 { }
It is also possible to name the default provider, so that our configuration looks a bit cleaner. The result is the same:
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 { }
Call method on object instance
The container can also call methods on instances an inject all method arguments
use rg\injektor\attributes\Inject; class Foo { #[Inject] public function doSomething(Bar $bar) { } } class Bar { } $foo = new Foo(); $dic->callMethodOnObject($foo, 'doSomething');
Of course you can also use named injections.
It is also possible to add additional values to the method call, like with object creation:
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'));