Skip to content

A simple to use buffering queue as PHP package (no dependencies), which flexibly buffers objects, strings, integers etc. until a maximum size is reached

License

Notifications You must be signed in to change notification settings

mahmudkuet11/Buffered-Queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Buffered Queue

Latest Stable Version Build Status License

A simple to use buffering queue as PHP package (no dependencies), which flexibly buffers objects, strings, integers etc. until a maximum size is reached

Installation

Via Composer

composer require mahmud/buffered-queue

Usage

Basic usage

use Mahmud\BufferedQueue\BufferedQueue;

// Maximum size is 3
$queue = BufferedQueue::make('key', function($items){
    var_dump($items); // Output is mentioned below
}, 3);

$queue->push('item 1');
$queue->push('item 2');
$queue->push('item 3');
$queue->push('item 4');
$queue->push('item 5');
$queue->push('item 6');
$queue->push('item 7');

$queue->finish();

// Output

// First
[
    'item 1', 'item 2', 'item 3'
]

// Second
[
    'item 4', 'item 5', 'item 6'
]

// Third
[
    'item 7'
]

Also instance of Mahmud\BufferedQueue\HandlerContract can be passed.

use Mahmud\BufferedQueue\HandlerContract;

class QueueHandler implements HandlerContract {

    public function handle($items) {
        // Do something
    }
}

$queue = BufferedQueue::make('key', new QueueHandler, 3);

API

make($key, $callback, $max_size)

BufferedQueue::make($key, $callback, $max_size);
  • $key is a unique string. For duplicated key this will return same instance.
$queue1 = BufferedQueue::make('key1', $callback, $max_size);
$queue2 = BufferedQueue::make('key1', $callback, $max_size);

$queue1 === $queue2 // true
  • $callback will receive array of items.
  • $max_size determines the maximum size of the queue. When queue is full, callback is triggered with the buffered items.

push($item)

$item can be anything. object, string, integer, boolean, array etc...

$queue->push('item 1');

finish()

Trigger callback with buffered items at any time. Generally used after pushing all items.

run()

Force to run the callback with buffered items at any time. After running the callback buffer will be empty.

getItems()

Get all items in buffer.

Testing

vendor/bin/phpunit

License

MIT license. Please see the license file for more information.

About

A simple to use buffering queue as PHP package (no dependencies), which flexibly buffers objects, strings, integers etc. until a maximum size is reached

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages