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