Skip to content

Dependency injection

Bob Gobeille edited this page Apr 23, 2015 · 1 revision

Dependency injection

Dependency injection is used to create singleton objects and their dependencies automatically. This is very useful when global objects should be available in a variety of objects.

Fossology currently uses the symfony dependency injection version 2.5.*.

The objects and their dependencies are configured in the XML file

src/lib/php/services.xml

A simple example of the configuration would be:

<container xmlns="http://symfony.com/schema/dic/services">

    <services>
        <service id="logger" class="\Monolog\Logger">
            <argument type="string">default</argument>
        </service>
        <service id="db.manager" class="\Fossology\Lib\Db\DbManager">
            <argument type="service" id="logger"/>
        </service>
    </services>
</container>

The dependency injection container is created in the file src/lib/php/common-container.php and stored in the global variable $container.

It can be used anywhere in the application like in the following example:

  global $container;
  $dbManager = $container->get('db.manager');
Clone this wiki locally