Skip to content

Commit 461cb53

Browse files
Update to new version of the class
1 parent 1f0b1c5 commit 461cb53

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

components/http_foundation.rst

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -636,39 +636,59 @@ Streaming a JSON Response
636636

637637
.. versionadded:: 6.2
638638

639-
The :class:`Symfony\\Component\\HttpFoundation\\StreamedJsonResponse` class allows
640-
an API to return a lot of data as JSON and keep the used resources low by make usage
641-
of Generators.
639+
The :class:`Symfony\\Component\\HttpFoundation\\StreamedJsonResponse` class was
640+
introduced in Symfony 6.2. It allows an API to return a lot of data as JSON and keep
641+
the used resources low by make usage of Generators.
642642

643-
It expects a JSON structure with one or multiple replacers identifiers, as example
644-
the `'__articles__'`. As a second argument it requires one or multiple Generators
645-
which items can be converted to a JSON via ``json_encode`` method. The key of the
646-
Generators requires to be the used replacer identifiers::
643+
It expects an array which represents the JSON structure and the list which should be
644+
streamed are represented in the array as ``\Generator``. The response will stream this
645+
JSON with generators in to most efficient way and keep resources as low as possible::
647646

648647
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
649648

649+
function loadArticles(): \Generator { // any method or function returning a Generator
650+
yield ['title' => 'Article 1'];
651+
yield ['title' => 'Article 2'];
652+
yield ['title' => 'Article 3'];
653+
});
654+
650655
$response = new StreamedJsonResponse(
651-
// json structure with replace identifiers
656+
// json structure with generators in which will be streamed as a list
652657
[
653658
'_embedded' => [
654-
'articles' => '__articles__',
659+
'articles' => loadArticles(), // any \Generator can be used which will be streamed as list of data
655660
],
656661
],
657-
// array of generators with replace identifier used as key
658-
[
659-
'__articles__' => (function (): \Generator { // any method or function returning a Generator
660-
yield ['title' => 'Article 1'];
661-
yield ['title' => 'Article 2'];
662-
yield ['title' => 'Article 3'];
663-
})(),
664-
]
665662
);
666663

667664
.. tip::
668665

669666
If loading data via doctrine the ``toIterable`` method of ``Doctrine`` can be
670667
used to keep also the resources low and fetch only one row one by one.
671-
See the `Doctrine Batch processing`_ documentation for more.
668+
See the `Doctrine Batch processing`_ documentation for more::
669+
670+
public function __invoke(): Response
671+
{
672+
return new StreamedJsonResponse(
673+
[
674+
'_embedded' => [
675+
'articles' => $this->loadArticles(),
676+
],
677+
],
678+
);
679+
}
680+
681+
public function loadArticles(): \Generator
682+
{
683+
$queryBuilder = $entityManager->createQueryBuilder();
684+
$queryBuilder->from(Article::class, 'article');
685+
$queryBuilder->select('article.id')
686+
->addSelect('article.title')
687+
->addSelect('article.description');
688+
689+
return $queryBuilder->getQuery()->toIterable();
690+
}
691+
672692

673693
Serving Files
674694
~~~~~~~~~~~~~

0 commit comments

Comments
 (0)