Skip to content

Commit

Permalink
Add mqtt constants (#58)
Browse files Browse the repository at this point in the history
* Add mqtt constants

* Add COMPOSER_ROOT_VERSION
  • Loading branch information
sy-records authored Oct 8, 2021
1 parent 2a8d2c7 commit d7e27ae
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.php-cs-fixer.php export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/bin export-ignore
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
sudo make install
- name: Setup Deps
env:
COMPOSER_ROOT_VERSION: 1.4-dev
run:
composer install -o

Expand Down
55 changes: 55 additions & 0 deletions bin/gen-constants
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env php
<?php

include 'vendor/autoload.php';

use Simps\MQTT\Protocol\Types;
use Simps\MQTT\Protocol\ProtocolInterface;

function getConstants(string $objectOrClass)
{
$objClass = new \ReflectionClass($objectOrClass);
return $objClass->getConstants();
}

function genData(array $constants, string $prefix = '')
{
$data = '';
foreach ($constants as $name => $value) {
$value = is_int($value) ? $value : "'{$value}'";
$data .= "define('{$prefix}{$name}', $value);\n";
}
return $data;
}

$constants = "<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*/
declare(strict_types=1);
";

$protocolConstants = getConstants(ProtocolInterface::class);
$constants .= genData($protocolConstants);
$typeConstants = getConstants(Types::class);
$constants .= genData($typeConstants, 'MQTT_TYPE_');

$verbose = [
'NONE' => 0,
'HEXDUMP' => 1,
'HEXDUMP_ASCII' => 2,
'ASCII' => 3,
'TEXT' => 4,
'HEX_STREAM' => 5,
];
$constants .= genData($verbose, 'MQTT_VERBOSE_');

file_put_contents('src/constants.php', $constants);
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"phpunit/phpunit": "^8.5",
"swoole/ide-helper": ">=4.4.20"
"swoole/ide-helper": ">=4.4.20",
"simps/mqtt-cli": "*"
},
"suggest": {
"ext-swoole": "The ext-swoole >= v4.4.20 or v4.5.3 needs to be loaded when using the MQTT Client."
},
"autoload": {
"psr-4": {
"Simps\\MQTT\\": "src/"
}
},
"files": [
"src/constants.php"
]
},
"autoload-dev": {
"psr-4": {
Expand All @@ -46,5 +50,10 @@
"cs-check": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix --dry-run",
"cs-fix": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix",
"test": "./tests/co-phpunit --colors=always"
},
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
}
}
49 changes: 49 additions & 0 deletions src/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*/

declare(strict_types=1);

define('MQTT_PROTOCOL_LEVEL_3_1', 3);
define('MQTT_PROTOCOL_LEVEL_3_1_1', 4);
define('MQTT_PROTOCOL_LEVEL_5_0', 5);
define('MQISDP_PROTOCOL_NAME', 'MQIsdp');
define('MQTT_PROTOCOL_NAME', 'MQTT');
define('MQTT_QOS_0', 0);
define('MQTT_QOS_1', 1);
define('MQTT_QOS_2', 2);
define('MQTT_RETAIN_0', 0);
define('MQTT_RETAIN_1', 1);
define('MQTT_RETAIN_2', 2);
define('MQTT_DUP_0', 0);
define('MQTT_DUP_1', 1);
define('MQTT_SESSION_PRESENT_0', 0);
define('MQTT_SESSION_PRESENT_1', 1);
define('MQTT_TYPE_CONNECT', 1);
define('MQTT_TYPE_CONNACK', 2);
define('MQTT_TYPE_PUBLISH', 3);
define('MQTT_TYPE_PUBACK', 4);
define('MQTT_TYPE_PUBREC', 5);
define('MQTT_TYPE_PUBREL', 6);
define('MQTT_TYPE_PUBCOMP', 7);
define('MQTT_TYPE_SUBSCRIBE', 8);
define('MQTT_TYPE_SUBACK', 9);
define('MQTT_TYPE_UNSUBSCRIBE', 10);
define('MQTT_TYPE_UNSUBACK', 11);
define('MQTT_TYPE_PINGREQ', 12);
define('MQTT_TYPE_PINGRESP', 13);
define('MQTT_TYPE_DISCONNECT', 14);
define('MQTT_TYPE_AUTH', 15);
define('MQTT_VERBOSE_NONE', 0);
define('MQTT_VERBOSE_HEXDUMP', 1);
define('MQTT_VERBOSE_HEXDUMP_ASCII', 2);
define('MQTT_VERBOSE_ASCII', 3);
define('MQTT_VERBOSE_TEXT', 4);
define('MQTT_VERBOSE_HEX_STREAM', 5);

0 comments on commit d7e27ae

Please sign in to comment.