Skip to content

Commit c0b8473

Browse files
committed
Add Subscribe event
1 parent bfafe0e commit c0b8473

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/Command/SubscribeCommand.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function configure()
4848
new InputOption('config-path', null, InputOption::VALUE_OPTIONAL, 'Setting the Swoole config file path'),
4949
new InputOption('properties-path', null, InputOption::VALUE_OPTIONAL, 'Setting the Properties config file path'),
5050
new InputOption('unsubscribe', 'U', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Topics that need to be unsubscribed'),
51+
new InputOption('event', 'e', InputOption::VALUE_OPTIONAL, 'Subscribed EventDispatcher', ''),
5152
])
5253
);
5354
}

src/Event/AbstractEvent.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* This file is part of Simps
4+
*
5+
* @link https://github.com/simps/mqtt
6+
* @contact Lu Fei <[email protected]>
7+
*
8+
* For the full copyright and license information,
9+
* please view the LICENSE file that was distributed with this source code
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Simps\MQTTCLI\Event;
15+
16+
use Simps\MQTT\Client;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Contracts\EventDispatcher\Event;
20+
21+
abstract class AbstractEvent extends Event
22+
{
23+
/** @var Client */
24+
protected $client;
25+
26+
/** @var InputInterface */
27+
protected $input;
28+
29+
/** @var OutputInterface */
30+
protected $output;
31+
32+
public function __construct(Client $client, InputInterface $input, OutputInterface $output)
33+
{
34+
$this->client = $client;
35+
$this->input = $input;
36+
$this->output = $output;
37+
$this->handle();
38+
}
39+
40+
abstract public function handle();
41+
}

src/Handler/AbstractHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public function getUnsubscribe(): array
230230
return $this->input->getOption('unsubscribe');
231231
}
232232

233+
public function getEvent(): string
234+
{
235+
return $this->input->getOption('event');
236+
}
237+
233238
protected function log($msg): void
234239
{
235240
$date = date('Y-m-d H:i:s');

src/Handler/SubscribeHandler.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Command\Command;
2020
use Symfony\Component\Console\Input\InputInterface;
2121
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\EventDispatcher\EventDispatcher;
2223

2324
class SubscribeHandler extends AbstractHandler
2425
{
@@ -102,8 +103,11 @@ public function handle(InputInterface $input, OutputInterface $output): int
102103
$buffer = $client->recv();
103104
if ($buffer && $buffer !== true) {
104105
$this->log(json_encode($buffer));
105-
// TODO: need event
106-
// $client $this->input $this->output
106+
$event = $this->getEvent();
107+
if (!empty($event) && class_exists($event)) {
108+
echo 'sas';
109+
(new EventDispatcher())->dispatch(new $event($client, $input, $output));
110+
}
107111
}
108112
if ($timeSincePing <= (time() - $client->getConfig()->getKeepAlive())) {
109113
$buffer = $client->ping();

0 commit comments

Comments
 (0)