|
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 $name Worker name. |
|
45
|
|
|
* @param string $id Service ID. |
|
46
|
|
|
* @param array $options Worker options. |
|
47
|
|
|
* @throws InvalidArgumentException Thrown, when there is no worker runner service registered in container. |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public function addRunner($name, $id, array $options = array()) |
|
50
|
|
|
{ |
|
51
|
5 |
|
if (!$this->container->has($id)) { |
|
52
|
1 |
|
throw new InvalidArgumentException("Service container does not have required service registered."); |
|
53
|
|
|
} |
|
54
|
4 |
|
$this->runners[$name] = array( |
|
55
|
4 |
|
'id' => $id, |
|
56
|
4 |
|
'options' => $options, |
|
57
|
|
|
); |
|
58
|
4 |
|
} |
|
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
|
|
|
* Get registered runners. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string[] |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function getRunners() |
|
81
|
|
|
{ |
|
82
|
2 |
|
return array_keys($this->runners); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.