DX (developer experience) improvements are small changes that make your day-to-day work as developer easier or more pleasant. A few days ago we published the first DX improvements of Symfony 6.3 and this blog post shows other DX improvements added by Symfony 6.3.
Display Invisible Characters in Dumped Contents
Unicode defines tens of different invisible characters like white spaces. These characters are problematic because they can result in impossible to debug issues where e.g. two strings look identical but your code says that they are not the same.
In Symfony 6.3 we're improving the VarDumper component to always highlight the
invisible characters of the contents. For example, if the string Lorem Ipsum
contains a zero-width non-breaking space (Unicode character U+FEFF) after the
letter o
, you'll see this: Lo\u{FEFF}rem Ipsum
.
Allow Milliseconds and Microseconds in YAML Dates
The Yaml Component can process dates with milliseconds/microseconds but that
information is lost when dumping the dates again. In Symfony 6.3, we're improving
the Yaml dumper to detect if the dates have that information. This new behavior
is automatic, so you don't need to add any config flag to the ->dump()
call:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// BEFORE
date: 2023-01-08T16:18:25Z
# dumped as: 2023-01-08T16:18:25+00:00
date_with_milliseconds: 2023-01-08T16:18:25.213Z
# dumped as: 2023-01-08T16:18:25+00:00
date_with_microseconds: 2023-01-08T16:18:25.718800Z
# dumped as: 2023-01-08T16:18:25+00:00
// AFTER
date: 2023-01-08T16:18:25Z
# dumped as: 2023-01-08T16:18:25+00:00
date_with_milliseconds: 2023-01-08T16:18:25.213Z
# dumped as: 2023-01-08T16:18:25.213+00:00
date_with_microseconds: 2023-01-08T16:18:25.718800Z
# dumped as: 2023-01-08T16:18:25.718800+00:00
Build Parameters in Service Container
The Service Container compilation is one of the most critical parts of a Symfony application. It allows to build and optimize the services/parameters of your applications and it's the reason why Symfony is so fast.
During the compilation phase, it's common to create temporary parameters in the
container that you later delete when the compilation is finished. That's why in
Symfony 6.3 we've introduced build-only parameters. Prefix your parameter name
with a dot (e.g. .name
) and it will be automatically removed when the compilation
finishes:
1 2 3 4 5
// normal parameter (you can delete it manually if you want)
$containerBuilder->setParameter('foo');
// this won't be available in the built container (it's deleted after the compilation)
$containerBuilder->setParameter('.bar');
Streamed JSON Responses
When you stream lots of data via a JSON API, it's difficult to keep low the
consumption of resources such as memory. In Symfony 6.3 we're introducing a
new StreamedJsonResponse
that uses a structure and generics to create an
efficient resource-saving JSON response.
Imagine that you have a content generator like this:
1 2 3 4 5 6 7 8 9
public function loadArticles(): \Generator
{
// generate contents somehow...
yield ['title' => 'Article 1'];
yield ['title' => 'Article 2'];
yield ['title' => 'Article 3'];
// it's recommended to use flush() after every specific number of items
}
In Symfony 6.3, you can use the following in your controller to stream these JSON contents:
1 2 3 4 5
return new StreamedJsonResponse([
'_embedded' => [
'articles' => loadArticles(),
],
]);
That's all. Symfony will take care of all the needed logic to stream this efficiently.
Want to mention here that StreamedJsonResponse works well in combination with Doctrine toIterable method. So you stream a result row by row from the database and then directly to the client without the need having the whole result set in the memory.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/reference/batch-processing.html#iterating-large-results-for-data-processing