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\Schema; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\EventDispatcher; |
24
|
|
|
|
25
|
|
|
use React; |
26
|
|
|
|
27
|
|
|
use Psr\Log; |
28
|
|
|
|
29
|
|
|
use IPub\MQTTClient; |
30
|
|
|
use IPub\MQTTClient\Client; |
31
|
|
|
use IPub\MQTTClient\Commands; |
32
|
|
|
use IPub\MQTTClient\Configuration; |
33
|
|
|
use IPub\MQTTClient\Events; |
34
|
|
|
use IPub\MQTTClient\Logger; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* MQTT client extension container |
38
|
|
|
* |
39
|
|
|
* @package iPublikuj:MQTTClient! |
40
|
|
|
* @subpackage DI |
41
|
|
|
* |
42
|
|
|
* @author Adam Kadlec <[email protected]> |
43
|
|
|
*/ |
44
|
1 |
|
final class MQTTClientExtension extends DI\CompilerExtension |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getConfigSchema() : Schema\Schema |
50
|
|
|
{ |
51
|
1 |
|
return Schema\Expect::structure([ |
52
|
1 |
|
'broker' => Schema\Expect::structure([ |
53
|
1 |
|
'httpHost' => Schema\Expect::string()->nullable(), |
54
|
1 |
|
'port' => Schema\Expect::int(1883), |
55
|
1 |
|
'address' => Schema\Expect::string('127.0.0.1'), |
56
|
1 |
|
'secured' => Schema\Expect::structure([ |
57
|
1 |
|
'enable' => Schema\Expect::bool(FALSE), |
58
|
1 |
|
'sslSettings' => Schema\Expect::array([]), |
59
|
|
|
]), |
60
|
|
|
]), |
61
|
1 |
|
'connection' => Schema\Expect::structure([ |
62
|
1 |
|
'username' => Schema\Expect::string(''), |
63
|
1 |
|
'password' => Schema\Expect::string(''), |
64
|
1 |
|
'clientID' => Schema\Expect::string(''), |
65
|
1 |
|
'keepAlive' => Schema\Expect::int(60), |
66
|
1 |
|
'protocol' => Schema\Expect::int(4), |
67
|
1 |
|
'clean' => Schema\Expect::bool(TRUE), |
68
|
|
|
]), |
69
|
1 |
|
'loop' => Schema\Expect::anyOf(Schema\Expect::string(), Schema\Expect::type(DI\Definitions\Statement::class))->nullable(), |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function loadConfiguration() |
77
|
|
|
{ |
78
|
1 |
|
$builder = $this->getContainerBuilder(); |
79
|
1 |
|
$configuration = $this->getConfig(); |
80
|
|
|
|
81
|
1 |
|
if ($configuration->loop === NULL) { |
82
|
1 |
|
if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) { |
83
|
1 |
|
$loop = $builder->addDefinition($this->prefix('client.loop')) |
84
|
1 |
|
->setType(React\EventLoop\LoopInterface::class) |
85
|
1 |
|
->setFactory('React\EventLoop\Factory::create'); |
86
|
|
|
|
87
|
|
|
} else { |
88
|
1 |
|
$loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
$loop = is_string($configuration->loop) ? new DI\Definitions\Statement($configuration->loop) : $configuration->loop; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$connection = $builder->addDefinition($this->prefix('client.configuration.connection')) |
96
|
1 |
|
->setType(Configuration\Connection::class) |
97
|
1 |
|
->setArguments([ |
98
|
1 |
|
'username' => $configuration->connection->username, |
99
|
1 |
|
'password' => $configuration->connection->password, |
100
|
|
|
'will' => NULL, |
101
|
1 |
|
'clientID' => $configuration->connection->clientID, |
102
|
1 |
|
'keepAlive' => $configuration->connection->keepAlive, |
103
|
1 |
|
'protocol' => $configuration->connection->protocol, |
104
|
1 |
|
'clean' => $configuration->connection->clean, |
105
|
|
|
]); |
106
|
|
|
|
107
|
1 |
|
$brokerConfiguration = $builder->addDefinition($this->prefix('client.configuration')) |
108
|
1 |
|
->setType(Configuration\Broker::class) |
109
|
1 |
|
->setArguments([ |
110
|
1 |
|
'httpHost' => $configuration->broker->httpHost, |
111
|
1 |
|
'port' => $configuration->broker->port, |
112
|
1 |
|
'address' => $configuration->broker->address, |
113
|
1 |
|
'enableSSL' => $configuration->broker->secured->enable, |
114
|
1 |
|
'sslSettings' => $configuration->broker->secured->sslSettings, |
115
|
1 |
|
$connection, |
116
|
|
|
]); |
117
|
|
|
|
118
|
1 |
|
if ($builder->findByType(Log\LoggerInterface::class) === []) { |
119
|
1 |
|
$builder->addDefinition($this->prefix('server.logger')) |
120
|
1 |
|
->setType(Logger\Console::class); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
$builder->addDefinition($this->prefix('client.client')) |
124
|
1 |
|
->setType(Client\Client::class) |
125
|
1 |
|
->setArguments([ |
126
|
1 |
|
'eventLoop' => $loop, |
127
|
1 |
|
'configuration' => $brokerConfiguration, |
128
|
|
|
]); |
129
|
|
|
|
130
|
1 |
|
if (class_exists('Symfony\Component\Console\Command\Command')) { |
131
|
|
|
// Define all console commands |
132
|
|
|
$commands = [ |
133
|
1 |
|
'client' => Commands\ClientCommand::class, |
134
|
|
|
]; |
135
|
|
|
|
136
|
1 |
|
foreach ($commands as $name => $cmd) { |
137
|
1 |
|
$builder->addDefinition($this->prefix('commands.' . lcfirst($name))) |
138
|
1 |
|
->setType($cmd); |
139
|
|
|
} |
140
|
|
|
} |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function beforeCompile() |
147
|
|
|
{ |
148
|
1 |
|
parent::beforeCompile(); |
149
|
|
|
|
150
|
1 |
|
$builder = $this->getContainerBuilder(); |
151
|
|
|
|
152
|
1 |
|
if (interface_exists('Symfony\Component\EventDispatcher\EventDispatcherInterface')) { |
153
|
1 |
|
$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcher\EventDispatcherInterface::class)); |
154
|
|
|
|
155
|
1 |
|
$client = $builder->getDefinition($builder->getByType(Client\Client::class)); |
156
|
1 |
|
assert($client instanceof DI\Definitions\ServiceDefinition); |
|
|
|
|
157
|
|
|
|
158
|
1 |
|
$client->addSetup('?->onStart[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
159
|
1 |
|
'@self', |
160
|
1 |
|
$dispatcher, |
161
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\StartEvent::class), |
162
|
|
|
]); |
163
|
1 |
|
$client->addSetup('?->onOpen[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
164
|
1 |
|
'@self', |
165
|
1 |
|
$dispatcher, |
166
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\OpenEvent::class), |
167
|
|
|
]); |
168
|
1 |
|
$client->addSetup('?->onConnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
169
|
1 |
|
'@self', |
170
|
1 |
|
$dispatcher, |
171
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\ConnectEvent::class), |
172
|
|
|
]); |
173
|
1 |
|
$client->addSetup('?->onDisconnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
174
|
1 |
|
'@self', |
175
|
1 |
|
$dispatcher, |
176
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\DisconnectEvent::class), |
177
|
|
|
]); |
178
|
1 |
|
$client->addSetup('?->onClose[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
179
|
1 |
|
'@self', |
180
|
1 |
|
$dispatcher, |
181
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\CloseEvent::class), |
182
|
|
|
]); |
183
|
1 |
|
$client->addSetup('?->onPing[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
184
|
1 |
|
'@self', |
185
|
1 |
|
$dispatcher, |
186
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\PingEvent::class), |
187
|
|
|
]); |
188
|
1 |
|
$client->addSetup('?->onPong[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
189
|
1 |
|
'@self', |
190
|
1 |
|
$dispatcher, |
191
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\PongEvent::class), |
192
|
|
|
]); |
193
|
1 |
|
$client->addSetup('?->onPublish[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
194
|
1 |
|
'@self', |
195
|
1 |
|
$dispatcher, |
196
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\PublishEvent::class), |
197
|
|
|
]); |
198
|
1 |
|
$client->addSetup('?->onSubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
199
|
1 |
|
'@self', |
200
|
1 |
|
$dispatcher, |
201
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\SubscribeEvent::class), |
202
|
|
|
]); |
203
|
1 |
|
$client->addSetup('?->onUnsubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
204
|
1 |
|
'@self', |
205
|
1 |
|
$dispatcher, |
206
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\UnsubscribeEvent::class), |
207
|
|
|
]); |
208
|
1 |
|
$client->addSetup('?->onMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
209
|
1 |
|
'@self', |
210
|
1 |
|
$dispatcher, |
211
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\MessageEvent::class), |
212
|
|
|
]); |
213
|
1 |
|
$client->addSetup('?->onWarning[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
214
|
1 |
|
'@self', |
215
|
1 |
|
$dispatcher, |
216
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\WarningEvent::class), |
217
|
|
|
]); |
218
|
1 |
|
$client->addSetup('?->onError[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
219
|
1 |
|
'@self', |
220
|
1 |
|
$dispatcher, |
221
|
1 |
|
new Nette\PhpGenerator\PhpLiteral(Events\ErrorEvent::class), |
222
|
|
|
]); |
223
|
|
|
} |
224
|
1 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param Nette\Configurator $config |
228
|
|
|
* @param string $extensionName |
229
|
|
|
* |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
public static function register( |
233
|
|
|
Nette\Configurator $config, |
234
|
|
|
string $extensionName = 'mqttClient' |
235
|
|
|
) :void { |
236
|
1 |
|
$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) { |
237
|
1 |
|
$compiler->addExtension($extensionName, new MQTTClientExtension); |
238
|
1 |
|
}; |
239
|
1 |
|
} |
240
|
|
|
} |
241
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.