Syringe - declarative IoC Container in PHP

Inversion of Control is an important principle of object-oriented programming used to reduce connectivity in computer programs (Wikipedia).

Simple as Pimple, powerful as Symfony DI


Syringe is a simple IoC Container written in PHP with a lot of features and declarative configuration.

It implements: implementation of parameters, factory methods, the main types of injections, including through the interface, scope, tag implementation and triggers.

Further features are described in more detail.

First example


There is a class Foo. Its constructor takes two parameters:

a = $a;
        $this->b = $b;
    }
}


The description of the service in the configuration looks like this:

services:
  foo: 
    class:     'Foo',
    arguments: ['value1', 'value2']


Now, when requesting the foo service, an instance of the Foo class will be created.

get('foo'); // Foo


Doctrine Deployment


A more complex example describes the use of Doctrine in a project.

The Foo class requires a database connection for its operation:

connection = $connection;
    }
    // ...
}


The application configuration will look like this:

# параметры доктрины
doctrine.configuration_paths: ['config/doctrine']
doctrine.db_parameters:
  driver: 'pdo_mysql'
  user: 'root'
  password: '1234'
  dbname: 'game'
  charset: 'UTF8'
services:
# сервис foo
  foo:
    class: 'Foo'
    arguments: ['@db_connection']
# сервисы доктрины
  doctrine.setup_configuration:
    factoryStaticMethod:
      - 'Doctrine\ORM\Tools\Setup'
      - 'createAnnotationMetadataConfiguration'
    arguments:
      - '%doctrine.configuration_paths%'
  doctrine.entity_manager:
    factoryStaticMethod:
      - 'Doctrine\ORM\EntityManager'
      - 'create'
    arguments:
      - '%doctrine.db_parameters%'
      - '@doctrine.setup_configuration'
    alias: doctrine
  doctrine.connection:
    factoryMethod:
      - '@doctrine.entity_manager'
      - 'getConnection'
    alias: db_connection


When requesting the foo service, the database connection will be passed to the constructor arguments:

get('foo');


Sources: SyringeExampleDoctrine

Tag Injection


The implementation of the tag is one of the unique functionality that allows you to use the list of services as a dependency.

The example uses a console application based on the Symfony Console Component:

services:
  app:
    class: 'Symfony\Component\Console\Application'
    calls:
      - ['addCommands', ['#console_commands']]
  command.foo:
    class: 'Command\FooCommand'
    tags: console_commands
  command.bar:
    class: 'Command\BarCommand'
    tags: console_commands


Services marked with the console_commands tag will be listed as an argument to the addCommands function.

Sources: SyringeExampleConsole

Principle of operation


The container configuration is specified using yaml, json or php format.
When compiling, the process of converting the configuration to a php array occurs. The converted configuration is exported to a file that is used to start the container.

The compilation algorithm is as follows:
  1. converting from an external format (e.g. yaml) to a php array,
  2. adding configuration to Builder,
  3. resolution of dependencies on parameters (% parameter.name%),
  4. separation of parameters from the service configuration,
  5. validation and distribution of configuration across collectors,
  6. merging configuration from collectors and parameters.


After that, the container is ready for use.

Future


Of course, Syringe will not supplant DI implementations from Symfony 2 or Zend2 - they provide the same functionality and are tightly integrated with the frameworks.

However, his goal is not this. Its niche is where there are no top-end frameworks. It is designed for projects without an integrated IoC Container and new lightweight applications where it has yet to be selected. And here he will give odds to other libraries, overtaking them in functionality and convenience.

Official site of the project: http://butterfly.pro/components/dependency-injection

I will be very glad to your comments and suggestions!

UPD: in connection with the development, the project was renamed Butterfly, and the Syringe IoC Container became one of its components.

Also popular now: