Skip to content

Commit ccadce0

Browse files
Fill before/after example documentation (#148)
* Fill before/after example documentation * Fixed minor errors in before script * Apply suggestions from code review Co-authored-by: Maximilien Delangle <[email protected]> --------- Co-authored-by: Maximilien Delangle <[email protected]>
1 parent 3c4bf64 commit ccadce0

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

docs/docs/tutorials/before-after/before.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3434
$families = [];
3535
while ($line = \fgets($file)) {
3636
try {
37-
$data = \json_decode($line, true, 512, JSON_THROW_ON_ERROR);
37+
$data = \json_decode($line, true, 512, \JSON_THROW_ON_ERROR);
3838
\assert(\is_array($data));
3939
} catch (\JsonException) {
4040
continue;
@@ -52,7 +52,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5252
foreach ($families as $family) {
5353
$manager->persist($family);
5454
}
55-
$manager->flush($family);
55+
56+
$manager->flush();
57+
$families = [];
5658
}
5759
}
5860

@@ -62,6 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6264
foreach ($families as $family) {
6365
$manager->persist($family);
6466
}
67+
6568
$manager->flush();
6669
}
6770

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
Example: Before/After showcase
22
============================================================
33

4-
.. note::
5-
| The code involved in this example is part of the test suite of **Yokai Batch**.
6-
| You can find the original code in the source repository:
7-
`Data <https://github.com/yokai-php/batch-src/tree/0.x/tests/symfony/data/star-wars>`__,
8-
`Entities <https://github.com/yokai-php/batch-src/tree/0.x/tests/symfony/src/Entity/StarWars>`__,
9-
`Jobs <https://github.com/yokai-php/batch-src/tree/0.x/tests/symfony/src/Job/StarWars>`__,
10-
`Tests <https://github.com/yokai-php/batch-src/blob/0.x/tests/symfony/tests/StarWarsJobSet.php>`__
4+
| This documentation will help you understand the difference between a classic code you could write.
5+
| Versus a code that had been written using ``yokai/batch`` library.
6+
117

128
What are we trying to do?
139
------------------------------------------------------------
1410

15-
We have a ``jsonl`` file, containing data that we want to import in our database.
11+
We have a ``jsonl`` file, containing data that we want, and we must import it in our database, via ``doctrine/orm``.
1612

1713
.. code-block:: jsonl
1814
@@ -21,11 +17,14 @@ We have a ``jsonl`` file, containing data that we want to import in our database
2117
{"code":"headphones","attributes":["description","headphone_connectivity","name","picture","power_requirements","price","release_date","sku","snr","thd","weight"],"attribute_as_label":"name","attribute_as_image":"picture","labels":{"en_US":"Headphones","fr_FR":"Casques audio","de_DE":"Kopfh\u00f6rer"}}
2218
2319
.. note::
24-
| todo
20+
| This file is obviously much larger than these 3 lines, you might have thousands lines to process.
21+
2522

2623
Before: Without Yokai Batch
2724
------------------------------------------------------------
2825

26+
The easiest way to do this is to create the one script you have already written thousands of times:
27+
2928
.. literalinclude:: before-after/before.php
3029
:language: php
3130

@@ -35,14 +34,19 @@ Before: Without Yokai Batch
3534
| As your team grow, it will become more important to avoid duplicating things like this.
3635
| Because it is likely that someone will forget one of those little things, code will start acting funny.
3736
37+
3838
After: With Yokai Batch
3939
------------------------------------------------------------
4040

41+
| Now, using ``yokai/batch``, we will be able to factorize most of this code to show only the business part :
42+
4143
.. literalinclude:: before-after/after.php
4244
:language: php
4345

4446
.. note::
45-
| todo
47+
| Most of the classes of that snippet are from ``Yokai\Batch`` namespace, you will reuse a lot of those along the way.
48+
| After all, batch processing is almost always the same, the only things that changes are:
4649
47-
.. hint::
48-
| todo parler du fait que c'est pas obligé d'avoir un storage, etc...
50+
* the data source you are reading from
51+
* some transformations you are performing on that source
52+
* the data source you are writing to

0 commit comments

Comments
 (0)