1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Command; |
4
|
|
|
|
5
|
|
|
use Bernard\Consumer; |
6
|
|
|
use Bernard\Queue; |
7
|
|
|
use Bernard\Queue\RoundRobinQueue; |
8
|
|
|
use Bernard\QueueFactory; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class ConsumeCommand extends \Symfony\Component\Console\Command\Command |
15
|
|
|
{ |
16
|
|
|
protected $consumer; |
17
|
|
|
protected $queues; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Consumer $consumer |
21
|
|
|
* @param QueueFactory $queues |
22
|
|
|
*/ |
23
|
2 |
|
public function __construct(Consumer $consumer, QueueFactory $queues) |
24
|
|
|
{ |
25
|
2 |
|
$this->consumer = $consumer; |
26
|
2 |
|
$this->queues = $queues; |
27
|
|
|
|
28
|
2 |
|
parent::__construct('bernard:consume'); |
29
|
2 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
2 |
|
public function configure() |
35
|
|
|
{ |
36
|
2 |
|
$this |
37
|
2 |
|
->addOption('max-runtime', null, InputOption::VALUE_OPTIONAL, 'Maximum time in seconds the consumer will run.', null) |
38
|
2 |
|
->addOption('max-messages', null, InputOption::VALUE_OPTIONAL, 'Maximum number of messages that should be consumed.', null) |
39
|
2 |
|
->addOption('stop-when-empty', null, InputOption::VALUE_NONE, 'Stop consumer when queue is empty.', null) |
40
|
2 |
|
->addOption('stop-on-error', null, InputOption::VALUE_NONE, 'Stop consumer when an error occurs.', null) |
41
|
2 |
|
->addArgument('queue', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Names of one or more queues that will be consumed.') |
42
|
|
|
; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
2 |
|
$queue = $this->getQueue($input->getArgument('queue')); |
|
|
|
|
51
|
|
|
|
52
|
2 |
|
$this->consumer->consume($queue, $input->getOptions()); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array|string $queue |
57
|
|
|
* |
58
|
|
|
* @return Queue |
59
|
|
|
*/ |
60
|
2 |
|
protected function getQueue($queue) |
61
|
|
|
{ |
62
|
2 |
|
if (is_array($queue)) { |
63
|
1 |
|
if (count($queue) > 1) { |
64
|
1 |
|
$queues = array_map([$this->queues, 'create'], $queue); |
65
|
|
|
|
66
|
1 |
|
return new RoundRobinQueue($queues); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$queue = $queue[0]; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $this->queues->create($queue); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.