New in Symfony 3.3: Added new shortcut methods

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

In addition to some large new features, Symfony 3.3 will also contain minor tweaks to make your work a bit easier.

Added a shortcut to create autowired definitions

Contributed by
Wouter De Jong
in #20648.

Creating service definitions in PHP via the ContainerBuilder work as follows:

1
2
3
4
$container->register('app.twig_extension', AppExtension::class)
    ->setAutowired(true)
    ->addTag('twig.extension')
;

Given that autowiring is all about working more quickly, in Symfony 3.3 you can use the new autowire() shortcut method to achieve the same result:

1
2
3
$container->autowire('app.twig_extension', AppExtension::class)
    ->addTag('twig.extension')
;

Added shorthand methods for Config prototypes

Contributed by
Ilyes kooli
in #20921.

The prototype() method of the ArrayNodeDefinition class allows you to create different types of prototypes for integers, floats, booleans, arrays, etc. The problem is that this method always returns a NodeDefinition object instead of the specific object created (IntegerNodeDefinition, ArrayNodeDefinition).

This makes IDEs unable to understand code like the following, where the max() method is undefined for the NodeDefinition object:

1
2
$node = new ArrayNodeDefinition('name');
$node->prototype('integer')->max(10);

In Symfony 3.3 we added a dedicated shortcut method for each of the possible prototypes. Now you can refactor the previous example as follows and your IDE will recognize the max() method:

1
2
$node = new ArrayNodeDefinition('name');
$node->integerPrototype()->max(10);

Added a Yaml syntax shortcut for name-only tags

Contributed by
Wouter De Jong
in #20651.

Tags used in service definitions can define configuration parameters, but they usually define just their names:

1
2
3
4
5
services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        tags:
            - { name: twig.extension }

In these cases, defining a Yaml hash (- { name: twig.extension }) is overkill. In Symfony 3.3, when only the tag name is needed, you can just add the tag as a string:

1
2
3
4
services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        tags: ['twig.extension']
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

> Added a Yaml syntax shortcut for name-only tags

Is this available only for service tags?
@Alex yes. You can see the details in the related pull request: https://github.com/symfony/symfony/pull/20651/files

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.