1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MQTTClientExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:MQTTClient! |
9
|
|
|
* @subpackage DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 12.03.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\MQTTClient\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\DI; |
21
|
|
|
use Nette\PhpGenerator as Code; |
22
|
|
|
|
23
|
|
|
use BinSoul\Net\Mqtt; |
24
|
|
|
|
25
|
|
|
use Symfony\Component\EventDispatcher; |
26
|
|
|
|
27
|
|
|
use React; |
28
|
|
|
|
29
|
|
|
use Psr\Log; |
30
|
|
|
|
31
|
|
|
use IPub\MQTTClient; |
32
|
|
|
use IPub\MQTTClient\Client; |
33
|
|
|
use IPub\MQTTClient\Commands; |
34
|
|
|
use IPub\MQTTClient\Events; |
35
|
|
|
use IPub\MQTTClient\Logger; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* MQTT client extension container |
39
|
|
|
* |
40
|
|
|
* @package iPublikuj:MQTTClient! |
41
|
|
|
* @subpackage DI |
42
|
|
|
* |
43
|
|
|
* @author Adam Kadlec <[email protected]> |
44
|
|
|
* |
45
|
|
|
* @method DI\ContainerBuilder getContainerBuilder() |
46
|
|
|
* @method array getConfig(array $default) |
47
|
|
|
* @method string prefix($id) |
48
|
|
|
*/ |
49
|
1 |
|
final class MQTTClientExtension extends DI\CompilerExtension |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $defaults = [ |
55
|
|
|
'broker' => [ |
56
|
|
|
'httpHost' => NULL, |
57
|
|
|
'port' => 1883, |
58
|
|
|
'address' => NULL, |
59
|
|
|
'dns' => [ |
60
|
|
|
'enable' => TRUE, |
61
|
|
|
'address' => '8.8.8.8', |
62
|
|
|
], |
63
|
|
|
'secured' => [ |
64
|
|
|
'enable' => FALSE, |
65
|
|
|
'sslSettings' => [], |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
'connection' => [ |
69
|
|
|
'username' => '', |
70
|
|
|
'password' => '', |
71
|
|
|
'clientID' => '', |
72
|
|
|
'keepAlive' => 60, |
73
|
|
|
'protocol' => 4, |
74
|
|
|
'clean' => TRUE, |
75
|
|
|
], |
76
|
|
|
'loop' => NULL, |
77
|
|
|
'console' => FALSE, |
78
|
|
|
'symfonyEvets' => FALSE, |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function loadConfiguration() |
85
|
|
|
{ |
86
|
|
|
// Get container builder |
87
|
1 |
|
$builder = $this->getContainerBuilder(); |
88
|
|
|
|
89
|
|
|
// Merge extension default config |
90
|
1 |
|
$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters))); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// Get extension configuration |
93
|
1 |
|
$configuration = $this->getConfig(); |
|
|
|
|
94
|
|
|
|
95
|
1 |
|
if ($configuration['loop'] === NULL) { |
96
|
1 |
|
if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) { |
97
|
1 |
|
$loop = $builder->addDefinition($this->prefix('client.loop')) |
98
|
1 |
|
->setType(React\EventLoop\LoopInterface::class) |
99
|
1 |
|
->setFactory('React\EventLoop\Factory::create'); |
100
|
|
|
|
101
|
|
|
} else { |
102
|
1 |
|
$loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} else { |
106
|
|
|
$loop = $builder->getDefinition(ltrim($configuration['loop'], '@')); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$connection = new Mqtt\DefaultConnection( |
110
|
1 |
|
$configuration['connection']['username'], |
111
|
1 |
|
$configuration['connection']['password'], |
112
|
1 |
|
NULL, |
113
|
1 |
|
$configuration['connection']['clientID'], |
114
|
1 |
|
$configuration['connection']['keepAlive'], |
115
|
1 |
|
$configuration['connection']['protocol'], |
116
|
1 |
|
$configuration['connection']['clean'] |
117
|
|
|
); |
118
|
|
|
|
119
|
1 |
|
$clientConfiguration = new Client\Configuration( |
120
|
1 |
|
$configuration['broker']['httpHost'], |
121
|
1 |
|
$configuration['broker']['port'], |
122
|
1 |
|
$configuration['broker']['address'], |
123
|
1 |
|
$configuration['broker']['dns']['enable'], |
124
|
1 |
|
$configuration['broker']['dns']['address'], |
125
|
1 |
|
$configuration['broker']['secured']['enable'], |
126
|
1 |
|
$configuration['broker']['secured']['sslSettings'], |
127
|
1 |
|
$connection |
128
|
|
|
); |
129
|
|
|
|
130
|
1 |
|
if ($builder->findByType(Log\LoggerInterface::class) === []) { |
131
|
1 |
|
$builder->addDefinition($this->prefix('server.logger')) |
132
|
1 |
|
->setType(Logger\Console::class); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$builder->addDefinition($this->prefix('client.client')) |
136
|
1 |
|
->setType(Client\Client::class) |
137
|
1 |
|
->setArguments([ |
138
|
1 |
|
'eventLoop' => $loop, |
139
|
1 |
|
'configuration' => $clientConfiguration, |
140
|
|
|
]); |
141
|
|
|
|
142
|
1 |
|
if ($configuration['console'] === TRUE) { |
143
|
|
|
// Define all console commands |
144
|
|
|
$commands = [ |
145
|
1 |
|
'client' => Commands\ClientCommand::class, |
146
|
|
|
]; |
147
|
|
|
|
148
|
1 |
|
foreach ($commands as $name => $cmd) { |
149
|
1 |
|
$builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
150
|
1 |
|
->setType($cmd); |
151
|
|
|
} |
152
|
|
|
} |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function beforeCompile() |
159
|
|
|
{ |
160
|
|
|
parent::beforeCompile(); |
161
|
|
|
|
162
|
|
|
// Get container builder |
163
|
|
|
$builder = $this->getContainerBuilder(); |
164
|
|
|
|
165
|
|
|
// Merge extension default config |
166
|
|
|
$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters))); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// Get extension configuration |
169
|
|
|
$configuration = $this->getConfig(); |
|
|
|
|
170
|
|
|
|
171
|
|
|
// Get container builder |
172
|
|
|
$builder = $this->getContainerBuilder(); |
173
|
|
|
|
174
|
|
|
if ($configuration['symfonyEvets'] === TRUE) { |
175
|
|
|
$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcher\EventDispatcherInterface::class)); |
176
|
|
|
|
177
|
|
|
$client = $builder->getDefinition($builder->getByType(Client\Client::class)); |
178
|
|
|
assert($client instanceof DI\ServiceDefinition); |
|
|
|
|
179
|
|
|
|
180
|
|
|
$client->addSetup('?->onStart[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
181
|
|
|
'@self', |
182
|
|
|
$dispatcher, |
183
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\StartEvent::class), |
184
|
|
|
]); |
185
|
|
|
$client->addSetup('?->onOpen[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
186
|
|
|
'@self', |
187
|
|
|
$dispatcher, |
188
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\OpenEvent::class), |
189
|
|
|
]); |
190
|
|
|
$client->addSetup('?->onConnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
191
|
|
|
'@self', |
192
|
|
|
$dispatcher, |
193
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\ConnectEvent::class), |
194
|
|
|
]); |
195
|
|
|
$client->addSetup('?->onDisconnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
196
|
|
|
'@self', |
197
|
|
|
$dispatcher, |
198
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\DisconnectEvent::class), |
199
|
|
|
]); |
200
|
|
|
$client->addSetup('?->onClose[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
201
|
|
|
'@self', |
202
|
|
|
$dispatcher, |
203
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\CloseEvent::class), |
204
|
|
|
]); |
205
|
|
|
$client->addSetup('?->onPing[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
206
|
|
|
'@self', |
207
|
|
|
$dispatcher, |
208
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\PingEvent::class), |
209
|
|
|
]); |
210
|
|
|
$client->addSetup('?->onPong[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
211
|
|
|
'@self', |
212
|
|
|
$dispatcher, |
213
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\PongEvent::class), |
214
|
|
|
]); |
215
|
|
|
$client->addSetup('?->onPublish[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
216
|
|
|
'@self', |
217
|
|
|
$dispatcher, |
218
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\PublishEvent::class), |
219
|
|
|
]); |
220
|
|
|
$client->addSetup('?->onSubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
221
|
|
|
'@self', |
222
|
|
|
$dispatcher, |
223
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\SubscribeEvent::class), |
224
|
|
|
]); |
225
|
|
|
$client->addSetup('?->onUnsubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
226
|
|
|
'@self', |
227
|
|
|
$dispatcher, |
228
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\UnsubscribeEvent::class), |
229
|
|
|
]); |
230
|
|
|
$client->addSetup('?->onMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
231
|
|
|
'@self', |
232
|
|
|
$dispatcher, |
233
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\MessageEvent::class), |
234
|
|
|
]); |
235
|
|
|
$client->addSetup('?->onWarning[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
236
|
|
|
'@self', |
237
|
|
|
$dispatcher, |
238
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\WarningEvent::class), |
239
|
|
|
]); |
240
|
|
|
$client->addSetup('?->onError[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
241
|
|
|
'@self', |
242
|
|
|
$dispatcher, |
243
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\ErrorEvent::class), |
244
|
|
|
]); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param Nette\Configurator $config |
250
|
|
|
* @param string $extensionName |
251
|
|
|
* |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'mqttClient') |
255
|
|
|
{ |
256
|
|
|
$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) { |
257
|
|
|
$compiler->addExtension($extensionName, new MQTTClientExtension); |
258
|
|
|
}; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
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.