|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Puzzle\AMQP\Workers\WorkerProvider; |
|
6
|
|
|
use Puzzle\AMQP\Workers\WorkerContext; |
|
7
|
|
|
|
|
8
|
|
|
class Pimple implements WorkerProvider |
|
9
|
|
|
{ |
|
10
|
|
|
private |
|
11
|
|
|
$container; |
|
12
|
|
|
|
|
13
|
5 |
|
public function __construct(\Pimple\Container $container) |
|
14
|
|
|
{ |
|
15
|
5 |
|
$this->container = $container; |
|
16
|
5 |
|
} |
|
17
|
|
|
|
|
18
|
1 |
|
public function getWorker($workerName) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$workerContext = null; |
|
21
|
1 |
|
$key = $this->computeWorkerServiceKey($workerName); |
|
22
|
|
|
|
|
23
|
1 |
|
if(isset($this->container[$key])) |
|
24
|
1 |
|
{ |
|
25
|
1 |
|
$workerContext = $this->container[$key]; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
return $workerContext; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
private function computeWorkerServiceKey($workerName) |
|
32
|
|
|
{ |
|
33
|
1 |
|
return sprintf( |
|
34
|
1 |
|
'worker.%s', |
|
35
|
|
|
$workerName |
|
36
|
1 |
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function listAll() |
|
40
|
|
|
{ |
|
41
|
1 |
|
$workers = $this->extractWorkers(); |
|
42
|
|
|
|
|
43
|
1 |
|
return $this->listWorkers($workers); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function listWithRegexFilter($workerNamePattern) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$workers = $this->extractWorkers(); |
|
49
|
1 |
|
$workers = new \RegexIterator(new \ArrayIterator($workers), sprintf('~^worker\.%s~', $workerNamePattern)); |
|
50
|
1 |
|
$workers = iterator_to_array($workers); |
|
51
|
|
|
|
|
52
|
1 |
|
return $this->listWorkers($workers); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
private function listWorkers(array $extractedWorkers) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$workers = array(); |
|
58
|
|
|
|
|
59
|
2 |
|
foreach($extractedWorkers as $worker) |
|
60
|
|
|
{ |
|
61
|
2 |
|
$key = $this->formatWorkerName($worker); |
|
62
|
2 |
|
$worker = $this->container[$worker]; |
|
63
|
|
|
|
|
64
|
2 |
|
if($worker instanceof WorkerContext) |
|
65
|
2 |
|
{ |
|
66
|
2 |
|
$workers[$key] = [ |
|
67
|
2 |
|
'queue' => $worker->getQueueName(), |
|
68
|
2 |
|
'description' => $worker->getDescription(), |
|
69
|
|
|
]; |
|
70
|
2 |
|
} |
|
71
|
2 |
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
return $workers; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
private function extractWorkers() |
|
77
|
|
|
{ |
|
78
|
2 |
|
$services = new \ArrayIterator($this->container->keys()); |
|
79
|
2 |
|
$services = new \RegexIterator($services, '~^worker\..+~'); |
|
80
|
|
|
|
|
81
|
2 |
|
return iterator_to_array($services); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
private function formatWorkerName($workerServiceName) |
|
85
|
|
|
{ |
|
86
|
2 |
|
return str_replace('worker.', '', $workerServiceName); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
public function getMessageProcessors() |
|
90
|
|
|
{ |
|
91
|
2 |
|
if(isset($this->container[self::MESSAGE_PROCESSORS_SERVICE_KEY])) |
|
92
|
2 |
|
{ |
|
93
|
1 |
|
return $this->container[self::MESSAGE_PROCESSORS_SERVICE_KEY]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return []; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|