|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Client; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
|
7
|
|
|
Client, |
|
|
|
|
|
|
8
|
|
|
Model\Exchange\Declaration as Exchange, |
|
9
|
|
|
Model\Queue\Declaration as Queue, |
|
10
|
|
|
Model\Queue\Binding, |
|
11
|
|
|
}; |
|
12
|
|
|
use Innmind\Immutable\{ |
|
13
|
|
|
SetInterface, |
|
14
|
|
|
Set, |
|
15
|
|
|
}; |
|
16
|
|
|
|
|
17
|
|
|
final class AutoDeclare implements Client |
|
18
|
|
|
{ |
|
19
|
|
|
private $client; |
|
20
|
|
|
private $exchanges; |
|
21
|
|
|
private $queues; |
|
22
|
|
|
private $bindings; |
|
23
|
|
|
private $declared = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param SetInterface<Exchange>|null $exchanges |
|
27
|
|
|
* @param SetInterface<Queue>|null $queues |
|
28
|
|
|
* @param SetInterface<Binding>|null $bindings |
|
29
|
|
|
*/ |
|
30
|
8 |
|
public function __construct( |
|
31
|
|
|
Client $client, |
|
32
|
|
|
SetInterface $exchanges = null, |
|
33
|
|
|
SetInterface $queues = null, |
|
34
|
|
|
SetInterface $bindings = null |
|
35
|
|
|
) { |
|
36
|
8 |
|
$this->client = $client; |
|
37
|
8 |
|
$this->exchanges = $exchanges ?? Set::of(Exchange::class); |
|
38
|
8 |
|
$this->queues = $queues ?? Set::of(Queue::class); |
|
39
|
8 |
|
$this->bindings = $bindings ?? Set::of(Binding::class); |
|
40
|
|
|
|
|
41
|
8 |
|
if ((string) $this->exchanges->type() !== Exchange::class) { |
|
42
|
1 |
|
throw new \TypeError(sprintf( |
|
43
|
1 |
|
'Argument 2 must be of type SetInterface<%s>', |
|
44
|
1 |
|
Exchange::class |
|
45
|
|
|
)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
7 |
|
if ((string) $this->queues->type() !== Queue::class) { |
|
49
|
1 |
|
throw new \TypeError(sprintf( |
|
50
|
1 |
|
'Argument 3 must be of type SetInterface<%s>', |
|
51
|
1 |
|
Queue::class |
|
52
|
|
|
)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
if ((string) $this->bindings->type() !== Binding::class) { |
|
56
|
1 |
|
throw new \TypeError(sprintf( |
|
57
|
1 |
|
'Argument 4 must be of type SetInterface<%s>', |
|
58
|
1 |
|
Binding::class |
|
59
|
|
|
)); |
|
60
|
|
|
} |
|
61
|
5 |
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function channel(): Channel |
|
64
|
|
|
{ |
|
65
|
2 |
|
$channel = $this->client->channel(); |
|
66
|
2 |
|
$this->declareThrough($channel); |
|
67
|
|
|
|
|
68
|
2 |
|
return $channel; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function closed(): bool |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->client->closed(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function close(): void |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->client->close(); |
|
79
|
1 |
|
$this->declared = true; |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
private function declareThrough(Channel $channel): void |
|
83
|
|
|
{ |
|
84
|
2 |
|
if ($this->declared) { |
|
85
|
2 |
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$exchange = $channel->exchange(); |
|
89
|
1 |
|
$queue = $channel->queue(); |
|
90
|
|
|
|
|
91
|
1 |
|
$this->exchanges->foreach(static function(Exchange $command) use ($exchange): void { |
|
92
|
1 |
|
$exchange->declare($command); |
|
93
|
1 |
|
}); |
|
94
|
1 |
|
$this->queues->foreach(static function(Queue $command) use ($queue): void { |
|
95
|
1 |
|
$queue->declare($command); |
|
96
|
1 |
|
}); |
|
97
|
1 |
|
$this->bindings->foreach(static function(Binding $command) use ($queue): void { |
|
98
|
1 |
|
$queue->bind($command); |
|
99
|
1 |
|
}); |
|
100
|
1 |
|
$this->declared = true; |
|
101
|
1 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: