1 | <?php |
||
46 | 1 | final class MQTTClientExtension extends DI\CompilerExtension |
|
47 | { |
||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $defaults = [ |
||
52 | 'broker' => [ |
||
53 | 'httpHost' => NULL, |
||
54 | 'port' => 1883, |
||
55 | 'address' => NULL, |
||
56 | 'dns' => [ |
||
57 | 'enable' => TRUE, |
||
58 | 'address' => '8.8.8.8', |
||
59 | ], |
||
60 | 'secured' => [ |
||
61 | 'enable' => FALSE, |
||
62 | 'sslSettings' => [], |
||
63 | ], |
||
64 | ], |
||
65 | 'connection' => [ |
||
66 | 'username' => '', |
||
67 | 'password' => '', |
||
68 | 'clientID' => '', |
||
69 | 'keepAlive' => 60, |
||
70 | 'protocol' => 4, |
||
71 | 'clean' => TRUE, |
||
72 | ], |
||
73 | 'loop' => NULL, |
||
74 | 'console' => FALSE, |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function loadConfiguration() |
||
81 | { |
||
82 | // Get container builder |
||
83 | 1 | $builder = $this->getContainerBuilder(); |
|
84 | |||
85 | // Merge extension default config |
||
86 | 1 | $this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters))); |
|
87 | |||
88 | // Get extension configuration |
||
89 | 1 | $configuration = $this->getConfig(); |
|
90 | |||
91 | 1 | if ($configuration['loop'] === NULL) { |
|
92 | 1 | if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) { |
|
93 | 1 | $loop = $builder->addDefinition($this->prefix('client.loop')) |
|
94 | 1 | ->setType(React\EventLoop\LoopInterface::class) |
|
95 | 1 | ->setFactory('React\EventLoop\Factory::create'); |
|
96 | |||
97 | } else { |
||
98 | 1 | $loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class); |
|
99 | } |
||
100 | |||
101 | } else { |
||
102 | $loop = $builder->getDefinition(ltrim($configuration['loop'], '@')); |
||
103 | } |
||
104 | |||
105 | 1 | $connection = new Mqtt\DefaultConnection( |
|
106 | 1 | $configuration['connection']['username'], |
|
107 | 1 | $configuration['connection']['password'], |
|
108 | 1 | NULL, |
|
109 | 1 | $configuration['connection']['clientID'], |
|
110 | 1 | $configuration['connection']['keepAlive'], |
|
111 | 1 | $configuration['connection']['protocol'], |
|
112 | 1 | $configuration['connection']['clean'] |
|
113 | ); |
||
114 | |||
115 | 1 | $clientConfiguration = new Client\Configuration( |
|
116 | 1 | $configuration['broker']['httpHost'], |
|
117 | 1 | $configuration['broker']['port'], |
|
118 | 1 | $configuration['broker']['address'], |
|
119 | 1 | $configuration['broker']['dns']['enable'], |
|
120 | 1 | $configuration['broker']['dns']['address'], |
|
121 | 1 | $configuration['broker']['secured']['enable'], |
|
122 | 1 | $configuration['broker']['secured']['sslSettings'], |
|
123 | 1 | $connection |
|
124 | ); |
||
125 | |||
126 | 1 | if ($builder->findByType(Log\LoggerInterface::class) === []) { |
|
127 | 1 | $builder->addDefinition($this->prefix('server.logger')) |
|
128 | 1 | ->setType(Logger\Console::class); |
|
129 | } |
||
130 | |||
131 | 1 | $builder->addDefinition($this->prefix('client.client')) |
|
132 | 1 | ->setType(Client\Client::class) |
|
133 | 1 | ->setArguments([ |
|
134 | 1 | 'eventLoop' => $loop, |
|
135 | 1 | 'configuration' => $clientConfiguration, |
|
136 | ]); |
||
137 | |||
138 | 1 | if ($configuration['console'] === NULL) { |
|
139 | // Define all console commands |
||
140 | $commands = [ |
||
141 | 'client' => Commands\ClientCommand::class, |
||
142 | ]; |
||
143 | |||
144 | foreach ($commands as $name => $cmd) { |
||
145 | $builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
||
146 | ->setType($cmd); |
||
147 | } |
||
148 | } |
||
149 | 1 | } |
|
150 | |||
151 | /** |
||
152 | * @param Nette\Configurator $config |
||
153 | * @param string $extensionName |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public static function register(Nette\Configurator $config, string $extensionName = 'mqttClient') |
||
163 | } |
||
164 |