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
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
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
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']
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