📜 ⬆️ ⬇️

Syringe - declarative IoC Container for PHP

Inversion of Control (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 a declarative configuration.

It implements: the introduction of parameters, factory methods, the main types of injections, including through the interface, scope, the introduction of the tag and triggers.
')
Further features are described in more detail.

First example


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

<?php class Foo { protected $a; protected $b; public function __construct($a, $b) { $this->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 the service foo is requested, an instance of the class Foo will be created.

 <?php // container init... $foo = $container->get('foo'); // Foo 


Doctrine implementation


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

The Foo class requires a database connection for its work:

 <?php class Foo { protected $connection; public function __construct($connection) { $this->connection = $connection; } // ... } 


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 the foo service is requested, the connection to the database will be passed to the constructor arguments:

 <?php // init container ... $foo = $container->get('foo'); 


Sources: SyringeExampleDoctrine

Tag implementation


Tag injection is one of the unique functionalities that allow using a 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 the yaml, json or php format.
When compiling, the process of converting the configuration into an array of php. The converted configuration is exported to the file that is used to launch the container.

The compilation algorithm is as follows:
  1. converting from external format (for example yaml) to php array,
  2. add configuration in Builder,
  3. resolution of dependencies on parameters (% parameter.name%),
  4. separation of parameters from service configuration,
  5. validation and distribution of configurations across collectors,
  6. confluence of configurations from collectors and parameters.


After that the container is ready to work.

Future


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

However, its purpose is not that. Its niche is where there are no top 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 would be very happy for your comments and suggestions!

UPD: due to development, the project was renamed Butterfly, and the Syringe IoC Container became one of its components.

Source: https://habr.com/ru/post/229879/


All Articles