Pipeline
NghiaNV 8/16/2021
# Description
Pipeline giúp xử lí object theo từng step (pipe). Có thể dừng hoặc tiếp tục xử lí task kế tiếp theo logic xử lí của task.
Tương tự như Laravel Pipeline (opens new window) nhưng mình sẽ dùng Chain of responsibility pattern (opens new window) để tạo Pipeline cho Nin.
# Usage:
Cấu trúc class Pipeline có thể xem tại Nin\Libs\Pipeline
- send($object): Set object cần xử lí
- addPipe($pipe): Thêm pipe task handle
- thenReturn(): Run pipeline và return kết quả, mặc định sẽ trả về object.
- then($callback): Run pipeline với 1 callback function cuối cùng.
# Example
Tạo 2 pipe task xử lí tring string và upper case:
class TrimString extends Pipeline
{
public function handle($ob)
{
$ob->name = trim($ob->name);
return parent::handle($ob);
}
}
///
class UpperCase extends Pipeline
{
public function handle($ob)
{
$ob->name = strtoupper($ob->name);
return parent::handle($ob);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$stdClass = new \stdClass();
$stdClass->name = " foo";
$pipeline = new Pipeline();
$pipeline->send($stdClass)
->addPipe(new TrimString())
->addPipe(new UpperCase());
$pipeline->then(function ($passable) {
vd($passable->name);
});
/* Ouput */
/*
"FOO"
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Packagist: https://packagist.org/packages/nin/nin (opens new window)