forked from Eurobertics/Nebucord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdaterestfile.php
126 lines (114 loc) · 4.6 KB
/
updaterestfile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
$docsrcdirectorylist = [
'..\\..\\Github\\discord-api-docs\\docs\\resources',
'..\\..\\Github\\discord-api-docs\\docs\\interactions'
];
$targetcsvfile = './src/REST/Base/restrequestlist.csv';
$targetclassfile = './src/REST/Base/RestStatusList.php';
function lb() {
if(php_sapi_name() == 'cli') {
return "\n";
}
return "<br />";
}
$csvarray = array();
$constarray = array();
for($dir_i = 0; $dir_i < count($docsrcdirectorylist); $dir_i++) {
$docsrcdirectory = $docsrcdirectorylist[$dir_i];
echo "Begin building REST request from Discord docs. Using directory '" . $docsrcdirectory . "'" . lb();
$filelist = scandir($docsrcdirectory);
for ($fi = 0; $fi < count($filelist); $fi++) {
if ($filelist[$fi] == '.' || $filelist[$fi] == '..') {
continue;
}
echo "Current file for processing: ".$filelist[$fi] . lb();
$curfilearray = file($docsrcdirectory . "\\" . $filelist[$fi]);
for ($i = 0; $i < count($curfilearray); $i++) {
if (strpos($curfilearray[$i], '%') === false) {
continue;
}
$expstr = explode('%', substr($curfilearray[$i], 3));
$csv_part1 = 'REST_' . strtoupper(str_replace(' ', '_', trim($expstr[0])));
$slashinrestname = strpos($csv_part1, "/");
if ($slashinrestname !== false) {
$csv_part1 = substr($csv_part1, 0, $slashinrestname);
}
if (in_array($csv_part1, $constarray)) {
continue;
}
$expstr2 = explode(' ', trim($expstr[1]));
$csv_part2 = $expstr2[0];
preg_match_all("/{([a-zA-Z.]*)/", $expstr2[1], $matches);
if (count($matches[1]) > 0) {
$curworkline = $expstr2[1];
for ($ii = 0; $ii < count($matches[1]); $ii++) {
$replacevar = '##' . strtoupper(str_replace('.', '_', $matches[1][$ii])) . '##';
$curworkline = str_replace("{" . $matches[1][$ii], $replacevar, $curworkline);
}
$curworkline = preg_replace("/#([a-zA-Z\/_-]*})/", '', $curworkline);
$csv_part3 = $curworkline;
unset($curworkline);
} else {
$csv_part3 = $expstr2[1];
}
$csvarray[] = str_replace('-', '_', $csv_part1) . ";" . $csv_part2 . ";" . $csv_part3 . "?";
$constarray[] = str_replace('-', '_', $csv_part1);
}
}
}
// TODO Interactions to be automated in the future?
$csvarray[] = "REST_INTERACTION_RESPONSE;POST;interactions/##INTERACTION_ID##/##INTERACTION_TOKEN/callback?";
echo "Building done... writing to '".$targetcsvfile."'".lb();
$fd = fopen($targetcsvfile, 'w');
fwrite($fd, implode("\n", $csvarray));
fclose($fd);
echo "Writing to '".$targetcsvfile."' finished, creating class with appropriate const vars.".lb().lb();
echo "Start writing to class: '".$targetclassfile."'".lb();
$fd = fopen($targetclassfile, 'w');
$classstring = <<<CLASS
<?php
/**
*
* Nebucord - A Discord Websocket and REST API
*
* Copyright (C) 2018 Bernd Robertz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Bernd Robertz <brobertz.net@gmail.com>
*
*/
namespace Nebucord\REST\Base;
/**
* Class RestStatusList
*
* ATTENTION: This class is autogenerated!
* This class stores the possible REST actions as consts used by the REST API to send requests.
*
* For detailed status information visit the Discord developer site: https://discordapp.com/developers/docs/intro
*
* @package Nebucord\REST\Base
*/
abstract class RestStatusList {
CLASS;
echo "Building consts from Discord docs...".lb();
for($i = 0; $i < count($constarray); $i++) {
$classstring .= "\tconst ".$constarray[$i]." = '".$constarray[$i]."';\n";
}
// TODO Interactions to be automated in the future?
$classstring .= "\tconst REST_INTERACTION_RESPONSE = 'REST_INTERACTION_RESPONSE';\n";
$classstring .= "}";
fwrite($fd, $classstring);
fclose($fd);
echo "Done building, generated class '".$targetclassfile."'".lb().lb();