1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueBundle\Worker; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Description of WorkerRunnerManager |
11
|
|
|
* |
12
|
|
|
* @author Tomasz Struczyński <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class WorkerRunnerManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Worker runner services configuration. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $runners = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Container. |
25
|
|
|
* |
26
|
|
|
* @var ContainerInterface |
27
|
|
|
*/ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
7 |
|
* Register runner service. |
37
|
|
|
* |
38
|
7 |
|
* @param string $name Worker name. |
39
|
7 |
|
* @param string $id Service ID. |
40
|
|
|
* @param array $options Worker options. |
41
|
|
|
* @throws InvalidArgumentException Thrown, when there is no worker runner service registered in container. |
42
|
|
|
*/ |
43
|
|
|
public function addRunnerService($name, $id, array $options = array()) |
44
|
|
|
{ |
45
|
|
|
if (!$this->container->has($id)) { |
46
|
|
|
throw new InvalidArgumentException("Service container does not have required service registered."); |
47
|
|
|
} |
48
|
|
|
$this->runners[$name] = array( |
49
|
6 |
|
'id' => $id, |
50
|
|
|
'options' => $options, |
51
|
6 |
|
); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
5 |
|
public function has($name) |
55
|
5 |
|
{ |
56
|
5 |
|
return array_key_exists($name, $this->runners); |
57
|
|
|
} |
58
|
5 |
|
|
59
|
|
|
public function run($name, OutputInterface $output = null) |
60
|
5 |
|
{ |
61
|
|
|
if (!$this->has($name)) { |
62
|
5 |
|
throw new \InvalidArgumentException("No runner service registered for provided name."); |
63
|
|
|
} |
64
|
|
|
/* @var $runner WorkerRunnerInterface */ |
65
|
3 |
|
$runner = $this->container->get($this->runners[$name]['id']); |
66
|
|
|
$runner->run($this->runners[$name]['options'], $this->container, $output); |
67
|
3 |
|
} |
68
|
1 |
|
|
69
|
|
|
/** |
70
|
|
|
* Get registered runners. |
71
|
2 |
|
* |
72
|
2 |
|
* @return string[] |
73
|
2 |
|
*/ |
74
|
|
|
public function getRunners() |
75
|
|
|
{ |
76
|
|
|
return array_keys($this->runners); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|