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