1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Protocol\v091; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Model\Connection\StartOk, |
8
|
|
|
Model\Connection\SecureOk, |
9
|
|
|
Model\Connection\TuneOk, |
10
|
|
|
Model\Connection\Open, |
11
|
|
|
Model\Connection\Close, |
12
|
|
|
Transport\Frame, |
13
|
|
|
Transport\Protocol\Connection as ConnectionInterface, |
14
|
|
|
Transport\Frame\Type, |
15
|
|
|
Transport\Frame\Channel, |
|
|
|
|
16
|
|
|
Transport\Frame\Method, |
17
|
|
|
Transport\Frame\Value, |
18
|
|
|
Transport\Frame\Value\Table, |
19
|
|
|
Transport\Frame\Value\LongString, |
20
|
|
|
Transport\Frame\Value\Bits, |
21
|
|
|
Transport\Frame\Value\ShortString, |
22
|
|
|
Transport\Frame\Value\UnsignedShortInteger, |
23
|
|
|
Transport\Frame\Value\UnsignedLongInteger |
24
|
|
|
}; |
25
|
|
|
use Innmind\Url\Authority\UserInformation\{ |
26
|
|
|
UserInterface, |
27
|
|
|
PasswordInterface |
28
|
|
|
}; |
29
|
|
|
use Innmind\Math\Algebra\Integer; |
30
|
|
|
use Innmind\Immutable\{ |
31
|
|
|
Str, |
32
|
|
|
Map |
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
final class Connection implements ConnectionInterface |
36
|
|
|
{ |
37
|
|
|
public function startOk(StartOk $command): Frame |
38
|
|
|
{ |
39
|
|
|
$clientProperties = new Table( |
40
|
|
|
(new Map('string', Value::class)) |
41
|
|
|
->put('product', new LongString(new Str('InnmindAMQP'))) |
42
|
|
|
->put('platform', new LongString(new Str('PHP'))) |
43
|
|
|
->put('version', new LongString(new Str('1.0'))) |
44
|
|
|
->put('information', new LongString(new Str(''))) |
45
|
|
|
->put('copyright', new LongString(new Str(''))) |
46
|
|
|
->put( |
47
|
|
|
'capabilities', |
48
|
|
|
new Table( |
49
|
|
|
(new Map('string', Value::class)) |
50
|
|
|
->put('authentication_failure_close', new Bits(true)) |
51
|
|
|
->put('publisher_confirms', new Bits(true)) |
52
|
|
|
->put('consumer_cancel_notify', new Bits(true)) |
53
|
|
|
->put('exchange_exchange_bindings', new Bits(true)) |
54
|
|
|
->put('connection.blocked', new Bits(true)) |
55
|
|
|
) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return new Frame( |
60
|
|
|
Type::method(), |
61
|
|
|
new Channel(0), |
62
|
|
|
Methods::get('connection.start-ok'), |
63
|
|
|
$clientProperties, |
64
|
|
|
new ShortString(new Str('AMQPLAIN')), //mechanism |
65
|
|
|
$this->response($command->user(), $command->password()), |
66
|
|
|
new ShortString(new Str('en_US')) //locale |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function secureOk(SecureOk $command): Frame |
71
|
|
|
{ |
72
|
|
|
return new Frame( |
73
|
|
|
Type::method(), |
74
|
|
|
new Channel(0), |
75
|
|
|
Methods::get('connection.secure-ok'), |
76
|
|
|
$this->response($command->user(), $command->password()) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function tuneOk(TuneOk $command): Frame |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return new Frame( |
83
|
|
|
Type::method(), |
84
|
|
|
new Channel(0), |
85
|
|
|
Methods::get('connection.tune-ok'), |
86
|
|
|
new UnsignedShortInteger(new Integer($command->maxChannels())), |
87
|
|
|
new UnsignedLongInteger(new Integer($command->maxFrameSize())), |
88
|
|
|
new UnsignedShortInteger(new Integer( |
89
|
|
|
(int) ($command->heartbeat()->milliseconds() / 1000) |
90
|
|
|
)) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function open(Open $command): Frame |
95
|
|
|
{ |
96
|
|
|
return new Frame( |
97
|
|
|
Type::method(), |
98
|
|
|
new Channel(0), |
99
|
|
|
Methods::get('connection.open'), |
100
|
|
|
new ShortString(new Str((string) $command->virtualHost())), |
101
|
|
|
new ShortString(new Str('')), //capabilities (reserved) |
102
|
|
|
new Bits(false) //insist (reserved) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function close(Close $command): Frame |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$replyCode = 0; |
109
|
|
|
$replyText = ''; |
110
|
|
|
$method = new Method(0, 0); |
111
|
|
|
|
112
|
|
|
if ($command->hasReply()) { |
113
|
|
|
$replyCode = $command->replyCode(); |
114
|
|
|
$replyText = $command->replyText(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($command->causedKnown()) { |
118
|
|
|
$method = Methods::get($command->cause()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return new Frame( |
122
|
|
|
Type::method(), |
123
|
|
|
new Channel(0), |
124
|
|
|
Methods::get('connection.close'), |
125
|
|
|
new UnsignedShortInteger(new Integer($replyCode)), |
126
|
|
|
new ShortString(new Str($replyText)), |
127
|
|
|
new UnsignedShortInteger(new Integer($method->class())), |
128
|
|
|
new UnsignedShortInteger(new Integer($method->method())) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function closeOk(): Frame |
133
|
|
|
{ |
134
|
|
|
return new Frame( |
135
|
|
|
Type::method(), |
136
|
|
|
new Channel(0), |
137
|
|
|
Methods::get('connection.close-ok') |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function response(UserInterface $user, PasswordInterface $password): LongString |
142
|
|
|
{ |
143
|
|
|
$response = new Table( |
144
|
|
|
(new Map('string', Value::class)) |
145
|
|
|
->put( |
146
|
|
|
'LOGIN', |
147
|
|
|
new LongString(new Str((string) $user)) |
148
|
|
|
) |
149
|
|
|
->put( |
150
|
|
|
'PASSWORD', |
151
|
|
|
new LongString(new Str((string) $password)) |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
$response = (new Str((string) $response)) |
155
|
|
|
->toEncoding('ASCII') |
156
|
|
|
->substring(4); //skip the encoded table length integer |
157
|
|
|
|
158
|
|
|
return new LongString($response); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: