DI: Built-in Container Extensions

Package nette/di contains a few built-in extension to DI container.

ExtensionsExtension

Using this extension we can add a more extensions defined in NEON configuration files.

extensions:
    dibi: Dibi\Bridges\Nette\DibiExtension22

That's the way how we add Dibi\Bridges\Nette\DibiExtension22 extension named dibi, so settings will be in dibi section.

dibi:
    host: localhost

We can pass arguments into constructor for extension class.

extensions:
    application: Nette\Bridges\ApplicationDI\ApplicationExtension(%debugMode%, %appDir%, %tempDir%/cache)

InjectExtension

This extension allows us to use option inject: true, which enables passing dependencies via variables with @inject annotation and inject* methods.

services:
    blog.articles:
        factory: App\Model\Articles
        inject: true
namespace App\Model;

use Nette\Database\Connection;

class Articles
{
    /** @var Connection @inject */
    public $connection;
}

By default is inject attribute enabled only for presenters.

DecoratorExtension

Using this extensions we can add tags or add setup (eg call methods) to all services of given class or implementing given interface.

decorator:
    # key is class or interface name
    App\Repository\AbstractRepository:
        setup:
            - setConnection
        inject: true

    App\Component\BaseControl:
        setup:
            - setTranslator(@mytranslator)
        tags: [view]

SearchExtension

Search extension walks through given directories and registers found classes as services automatically.

search:
    # section name can be ommited if you want to search in only one directory
    section:
        in: %appDir%/Forms
        # file masks, *.php is default
        files: ['*.php', '*.php5']
        # array of masks, class name must match at least one
        classes:
            - App\*Form
        # only classes extending one of specified clasess will be registered
        extends: Nette\Application\UI\Form
        # only classes implementing at least one of specified interfaces will be registered
        implements: []
        # rules for excluding classes from auto registration
        exclude:
            classes: []
            extends: []
            implements: []
        # classes will be registered with specified tags
        tags: []

PhpExtension

You can set PHP extensions using this extension. For an overview of all directives, look at php.net.

php:
    date.timezone: Europe/Prague

ConstantsExtension

We can define PHP contants by this extension.

constants:
    FOOBAR: 'baz'

The FOOBAR constant will available in our application after startup.