Skip to content
/ utils Public

Batch allows you to separate code that is fetching data and processing them in batches. It has many uses.

License

Notifications You must be signed in to change notification settings

rixxi/utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Batch allows you to separate code that is fetching data and processing them in batches. It has many uses.

Example

Write ten lines of 80 characters per line.

use Rixxi\Utils\Batch;

$batch = new Batch(function ($values) {
	echo implode($values), PHP_EOL;
}, $limit = 80);

$lines = 10;
for ($x = $limit * $lines; $x-- > 0;) {
	$batch[] = chr(ord('A') + rand(0, 26 /* ord('Z') - ord('A') */));
}

Flushing & Callback

Batch is automatically flushed and reset when limit is reached and when is destroyed.

If no items were added to batch since start or last flush callback is not called.

You can flush batch manually at any point calling flush().

$batch->flush();

Or flush and throw away calling unset on batch.

unset($batch);

Usually you don't have to unset since garbage collector will do it for you at the end of function.

Install

composer require rixxi/utils

Troubleshooting

If you are getting random errors after processing data via Batch make sure you flush it before doing anything else. You are probably assuming all data are flushed when there might be few left after last round.

Ie.: in Doctrine when processing entities don't forget to flush manually before calling $repository->clear();.

About

Batch allows you to separate code that is fetching data and processing them in batches. It has many uses.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages