-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobDataMutators.php
More file actions
72 lines (65 loc) · 1.82 KB
/
Copy pathJobDataMutators.php
File metadata and controls
72 lines (65 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace Crustum\Queue\Event;
/**
* Mutate job data after tags/_uniqueId and before pending emit + push.
*
* Host packages (e.g. Speculum) can inject fields such as `speculum_uuid`
* into the payload that will be stored on the queue message.
*
* ```php
* JobDataMutators::register(function (string $jobClass, array $data, array $config): array {
* $data['speculum_uuid'] = $uuid;
*
* return $data;
* });
* ```
*/
final class JobDataMutators
{
/**
* @var list<callable(class-string, array<string, mixed>, array<string, mixed>): array<string, mixed>>
*/
protected static array $mutators = [];
/**
* Register a data mutator (FIFO).
*
* @param callable(class-string, array<string, mixed>, array<string, mixed>): array<string, mixed> $mutator Mutator
* @return void
*/
public static function register(callable $mutator): void
{
self::$mutators[] = $mutator;
}
/**
* Apply all mutators and return the final job data.
*
* @param class-string $jobClass Job class
* @param array<string, mixed> $data Job data
* @param array<string, mixed> $config Queue configuration
* @return array<string, mixed>
*/
public static function prepare(string $jobClass, array $data, array $config): array
{
foreach (self::$mutators as $mutator) {
$data = $mutator($jobClass, $data, $config);
}
return $data;
}
/**
* Clear mutators (tests).
*
* @return void
*/
public static function clear(): void
{
self::$mutators = [];
}
/**
* @return list<callable(class-string, array<string, mixed>, array<string, mixed>): array<string, mixed>>
*/
public static function all(): array
{
return self::$mutators;
}
}