Symfony News

New in Symfony 3.2: DX improvements

Symfony 3.2 includes tens of minor tweaks and improvements to make your work easier and to improve your productivity. This article summarizes some of those DX improvements.

Added ability to regress the progress bar

Contributed by
James Halsall
in #19824.

This is useful for example when the workload of some task cannot be determined beforehand In those cases, you can regress the progress bar by passing a negative step to the advance() method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Symfony\Component\Console\Helper\ProgressBar;

$progress = new ProgressBar($output, 100);
$progress->start();

// ... do some work
$progress->advance(30);

// new tasks are created, regress the bar by 10 steps
$progress->advance(-10);

Automatically detect the service definition class

Contributed by
Guilhem N
in #19191.

In PHP 7.x, methods can define the types of their return values. For example:

1
2
3
4
public function myFactory(): MyServiceClass
{
    // ...
}

In Symfony 3.2, we've added a new FactoryReturnTypePass compiler pass that automatically updates the service definitions of the factories that define their return types.

Added support for prioritizing form type extensions

Contributed by
David Maicher
in #19790.

Form extensions are the best way to add custom features on top of existing Symfony Form types. These extensions are enabled as services with the special form.type_extension tag. In Symfony 3.2, this tag defines a new optional attribute called priority to better control the order in which the form type extensions are loaded:

1
2
3
4
5
6
services:
    app.form.image_type_extension:
        class: AppBundle\Form\Extension\ImageTypeExtension
        tags:
            - { name: form.type_extension, priority: -128,
                extended_type: Symfony\Component\Form\Extension\Core\Type\FileType }

Added a named constructor to JsonResponse

Contributed by
Timothée Barray
in #19552.

If you create a JsonResponse with a content that is already encoded as a JSON string, you must pass true as the fourth optional argument of JsonResponse:

1
2
// $contents = '{"foo":"bar"}';
return new JsonResponse($contents, Response::HTTP_OK, [], true);

In Symfony 3.2, you can use the new fromJsonString() named constructor:

1
2
// $contents = '{"foo":"bar"}';
return JsonResponse::fromJsonString($contents);

Added a short syntax for service configurators

Contributed by
Oleg Voronkovich in #19190.

Service configurators allow you to use a callable to configure a service after its instantiation. In Symfony 3.2, you can use a short syntax to define them in YAML config files:

1
2
3
4
5
6
7
services:
    app.some_service:
        class: ...
        # Traditional syntax
        configurator: [ '@app.configurator', 'configure' ]
        # New short syntax supported by Symfony 3.2
        configurator: 'app.configurator:configure'

Allowed to specify a domain when updating translations

Contributed by
Anthony Grassiot in #19325.

The translation:update command now supports a --domain option to only update the messages related to that domain. This is very useful for complex applications that define lots of different translation domains:

1
$ ./bin/console translation:update en --force --domain=admin

Simplified the transChoice() function

Contributed by
Victor Bocharsky
in #19795.

The transChoice() PHP function has been simplified to match Twig's transchoice filter. This means that you no longer have to provide the third optional argument with the value of the number parameter used to select the appropriate translation:

1
2
3
4
5
6
7
// Before
$this->get('translator')
    ->transChoice('1 apple|%count% apples', 7, ['%count%' => 7]);

// After
$this->get('translator')
    ->transChoice('1 apple|%count% apples', 7);

Be trained by Symfony experts - 2016-11-07 Paris - 2016-11-07 Paris - 2016-11-09 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook