|
1 |
| -# Installation |
| 1 | +# Getting started |
| 2 | + |
| 3 | +## Configuring the bundle |
| 4 | + |
| 5 | +```php |
| 6 | +// config/bundles.php |
| 7 | +return [ |
| 8 | + // ... |
| 9 | + Yokai\Batch\Bridge\Symfony\Framework\YokaiBatchBundle::class => ['all' => true], |
| 10 | +]; |
| 11 | +``` |
| 12 | + |
| 13 | +```yaml |
| 14 | +# config/packages/yokai_batch.yaml |
| 15 | +yokai_batch: |
| 16 | + storage: |
| 17 | + filesystem: ~ |
| 18 | + # Or with yokai/batch-doctrine-dbal (& doctrine/dbal) |
| 19 | + # dbal: ~ |
| 20 | +``` |
| 21 | + |
| 22 | +## Job Example |
| 23 | + |
| 24 | +Let say you have a Doctrine ORM entity `App\Entity\User`, |
| 25 | +and a [JSON Lines](https://jsonlines.org/) file with information about these entities. |
| 26 | + |
| 27 | +Your goal is to import this file in the database. |
| 28 | + |
| 29 | +### Job in YAML |
| 30 | + |
| 31 | +```yaml |
| 32 | +# config/packages/yokai_batch.yaml (or anywhere else) |
| 33 | +services: |
| 34 | + job.import_users: |
| 35 | + class: Yokai\Batch\Job\Item\ItemJob |
| 36 | + tags: ['yokai_batch.job'] |
| 37 | + arguments: |
| 38 | + $batchSize: 500 |
| 39 | + $reader: !service |
| 40 | + class: Yokai\Batch\Job\Item\Reader\Filesystem\JsonLinesReader |
| 41 | + arguments: |
| 42 | + $filePath: !service |
| 43 | + class: Yokai\Batch\Job\Parameters\DefaultParameterAccessor |
| 44 | + arguments: |
| 45 | + $accessor: !service |
| 46 | + class: Yokai\Batch\Job\Parameters\JobExecutionParameterAccessor |
| 47 | + arguments: ['importFile'] |
| 48 | + $default: '%kernel.project_dir%/var/import/users.jsonl' |
| 49 | + $processor: !service |
| 50 | + class: Yokai\Batch\Bridge\Symfony\Serializer\DenormalizeItemProcessor |
| 51 | + arguments: |
| 52 | + $denormalizer: '@serializer' |
| 53 | + $type: App\Entity\User |
| 54 | + $writer: '@yokai_batch.item_writer.doctrine_orm_object_writer' |
| 55 | +``` |
| 56 | +
|
| 57 | +Then the job will be triggered with its service id: |
| 58 | +
|
| 59 | +```php |
| 60 | +/** @var \Yokai\Batch\Launcher\JobLauncherInterface $launcher */ |
| 61 | +$launcher->launch('job.import_users'); |
| 62 | +``` |
| 63 | + |
| 64 | +### Job in sources |
| 65 | + |
| 66 | +Although it is 100% possible to register jobs via a YAML file it can become very tedious. |
| 67 | + |
| 68 | +As Symfony supports registering all classes in `src/` as a service, |
| 69 | +we can leverage this behaviour to register all jobs in `src/`. |
| 70 | + |
| 71 | +```yaml |
| 72 | +# config/services.yaml |
| 73 | +services: |
| 74 | + _defaults: |
| 75 | + _instanceof: |
| 76 | + Yokai\Batch\Job\JobInterface: |
| 77 | + tags: ['yokai_batch.job'] |
| 78 | +``` |
| 79 | +
|
| 80 | +```php |
| 81 | +<?php |
| 82 | + |
| 83 | +namespace App\Job; |
| 84 | + |
| 85 | +use App\Entity\User; |
| 86 | +use Doctrine\Persistence\ManagerRegistry; |
| 87 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 88 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 89 | +use Yokai\Batch\Bridge\Doctrine\Persistence\ObjectWriter; |
| 90 | +use Yokai\Batch\Bridge\Symfony\Serializer\DenormalizeItemProcessor; |
| 91 | +use Yokai\Batch\Job\Item\ItemJob; |
| 92 | +use Yokai\Batch\Job\Item\Reader\Filesystem\JsonLinesReader; |
| 93 | +use Yokai\Batch\Job\Parameters\DefaultParameterAccessor; |
| 94 | +use Yokai\Batch\Job\Parameters\JobExecutionParameterAccessor; |
| 95 | +use Yokai\Batch\Storage\JobExecutionStorageInterface; |
| 96 | + |
| 97 | +final class ImportUsersJob extends ItemJob |
| 98 | +{ |
| 99 | + public function __construct( |
| 100 | + JobExecutionStorageInterface $executionStorage, |
| 101 | + ManagerRegistry $doctrine, |
| 102 | + DenormalizerInterface $denormalizer, |
| 103 | + KernelInterface $kernel |
| 104 | + ) { |
| 105 | + parent::__construct( |
| 106 | + 500, |
| 107 | + new JsonLinesReader( |
| 108 | + new DefaultParameterAccessor( |
| 109 | + new JobExecutionParameterAccessor('importFile'), |
| 110 | + $kernel->getProjectDir() . '/var/import/users.jsonl' |
| 111 | + ) |
| 112 | + ), |
| 113 | + new DenormalizeItemProcessor($denormalizer, User::class), |
| 114 | + new ObjectWriter($doctrine), |
| 115 | + $executionStorage |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Then the job will be triggered with its service id: |
| 122 | + |
| 123 | +```php |
| 124 | +/** @var \Yokai\Batch\Launcher\JobLauncherInterface $launcher */ |
| 125 | +$launcher->launch(\App\Job\ImportUsersJob::class); |
| 126 | +``` |
0 commit comments