Download the PHP package rg/injektor without Composer

On this page you can find all versions of the php package rg/injektor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package injektor

rg\injektor

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.

Test status

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:

Usage

After you installed rg\injektor you can use it like this:

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:

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:

Features

Constructor Injection

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:

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.

Property Injection

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

Instead of Bar, BarImpl is injected into $bar. You can also configure this in the dependecy injection configuration instead of using annotations

Using Provider Classes

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

Passing fixed data to providers

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:

Inject as Singleton

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

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:

$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

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

In contrast to singletons, In a service this example

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:

Configuration:

Alternatively you can also configure this with annotations

Pass additional parameters on runtime

You also can pass some values to the new instance on runtime.

This can also be combined with configured parameters.

Named injection

Configuration:

You can also configure this directly with annotations

It is also possible to name the default implementation, so that our configuration looks a bit cleaner. The result is the same:

Named providers

Configuration:

You can also configure this directly with annotations

It is also possible to name the default provider, so that our configuration looks a bit cleaner. The result is the same:

Call method on object instance

The container can also call methods on instances an inject all method arguments

Of course you can also use named injections.

It is also possible to add additional values to the method call, like with object creation:


All versions of injektor with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
doctrine/annotations Version ^1.12|^2.0.0
laminas/laminas-code Version ^4.13
psr/log Version ^1.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rg/injektor contains the following files

Loading the files please wait ....