Skip to content

Commit dada31a

Browse files
committed
Some structure improvements
1 parent fa4d381 commit dada31a

File tree

4 files changed

+84
-61
lines changed

4 files changed

+84
-61
lines changed

structure/functions.php

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,96 @@ function convert_key_type($key, $type, $required, $indentation) {
9090
/**
9191
* Print structure ready to use.
9292
*/
93-
function print_ws_structure($name, $structure, $useparams) {
93+
function print_ws_structures($structures) {
94+
foreach ($structures as $wsname => $structure) {
95+
$type = getTSTypeFromName($wsname);
96+
97+
print_ws_structure($wsname, $type, $structure, true);
98+
print_ws_structure($wsname, $type, $structure, false);
99+
}
100+
}
101+
102+
/**
103+
* Print params or return structures.
104+
*/
105+
function print_ws_structure($wsname, $type, $structure, $useparams) {
94106
if ($useparams) {
95-
$type = implode('', array_map('ucfirst', explode('_', $name))) . 'WSParams';
96-
$comment = "Params of $name WS.";
107+
$comment = "Params of $wsname WS.";
108+
$type .= 'WSParams';
109+
$typestructure = $structure->parameters_desc;
110+
$export = '';
97111
} else {
98-
$type = implode('', array_map('ucfirst', explode('_', $name))) . 'WSResponse';
99-
$comment = "Data returned by $name WS.";
112+
$comment = "Data returned by $wsname WS.";
113+
$type .= 'WSResponse';
114+
$typestructure = $structure->returns_desc;
115+
$export = 'export ';
100116
}
101117

102118
echo "
103119
/**
104-
* $comment
120+
* $comment";
121+
if (!empty($structure->description)) {
122+
echo "
123+
*
124+
* WS Description: $structure->description";
125+
}
126+
if (isset($structure->deprecated) && $structure->deprecated) {
127+
echo "
128+
*
129+
* @deprecatedonmoodle since ADDVERSIONHERE. This WS method is deprecated";
130+
}
131+
/* echo "
132+
* WS Type: $structure->type
133+
* Allowed from AJAX: " . ($structure->allowed_from_ajax ? 'yes' : 'no') . "
134+
* Read only session: " . ($structure->readonlysession ? 'yes' : 'no') . "
135+
* Login required: " . ($structure->loginrequired ? 'yes' : 'no');*/
136+
echo "
137+
*/
138+
${export}type $type = ".convert_to_ts(null, $typestructure).";\n";
139+
}
140+
141+
/**
142+
* Returns TS Type From WS Name.
105143
*/
106-
export type $type = ".convert_to_ts(null, $structure).";\n";
144+
function getTSTypeFromName($wsname) {
145+
$type = implode('', array_map('ucfirst', explode('_', $wsname) ) );
146+
$search = [
147+
'/^Block/',
148+
'/^Mod/',
149+
'/^Enrol/',
150+
'/^Gradereport/',
151+
'/^CoreCalendar/',
152+
'/^CoreBadges/',
153+
'/^CoreBlog/',
154+
'/^CoreCompetency/',
155+
'/^CoreFiles/',
156+
'/^CoreMessage/',
157+
'/^CoreNotes/',
158+
'/^MessageAirnotifier/',
159+
'/^ReportInsights/',
160+
'/^ToolLp/',
161+
'/^ToolMobile/',
162+
];
163+
164+
$replaces = [
165+
'AddonBlock',
166+
'AddonMod',
167+
'AddonEnrol',
168+
'CoreGradesGradereport',
169+
'AddonCalendar',
170+
'AddonBadges',
171+
'AddonBlog',
172+
'AddonCompetency',
173+
'AddonPrivateFiles',
174+
'AddonMessages',
175+
'AddonNotes',
176+
'AddonMessageOutputAirnotifier',
177+
'AddonReportInsights',
178+
'AddonCompetency',
179+
'CoreSite',
180+
];
181+
182+
return preg_replace($search, $replaces, $type);
107183
}
108184

109185
/**

structure/get_all_ws_structures.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,4 @@
3838

3939
$structures = get_all_ws_structures();
4040

41-
foreach ($structures as $wsname => $structure) {
42-
43-
remove_default_closures($structure->parameters_desc);
44-
print_ws_structure($wsname, $structure->parameters_desc, true);
45-
46-
remove_default_closures($structure->returns_desc);
47-
print_ws_structure($wsname, $structure->returns_desc, false);
48-
}
41+
print_ws_structures($structures);

structure/ws_functions.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,3 @@ function convert_to_ts($key, $value, $boolisnumber = false, $indentation = '', $
104104
return "{}; // WARNING: Unknown structure: $key " . get_class($value);
105105
}
106106
}
107-
108-
/**
109-
* Remove all closures (anonymous functions) in the default values so the object can be serialized.
110-
*/
111-
function remove_default_closures($value) {
112-
if ($value instanceof external_warnings || $value instanceof external_files) {
113-
// Ignore these types.
114-
115-
} else if ($value instanceof external_value) {
116-
if ($value->default instanceof Closure) {
117-
$value->default = null;
118-
}
119-
120-
} else if ($value instanceof external_single_structure) {
121-
122-
foreach ($value->keys as $subvalue) {
123-
remove_default_closures($subvalue);
124-
}
125-
126-
} else if ($value instanceof external_multiple_structure) {
127-
remove_default_closures($value->content);
128-
}
129-
}

structure/ws_functions_41.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,3 @@ function convert_to_ts($key, $value, $boolisnumber = false, $indentation = '', $
101101
return "{}; // WARNING: Unknown structure: $key " . get_class($value);
102102
}
103103
}
104-
105-
/**
106-
* Remove all closures (anonymous functions) in the default values so the object can be serialized.
107-
*/
108-
function remove_default_closures($value) {
109-
if ($value instanceof external_warnings || $value instanceof external_files) {
110-
// Ignore these types.
111-
112-
} else if ($value instanceof external_value) {
113-
if ($value->default instanceof Closure) {
114-
$value->default = null;
115-
}
116-
117-
} else if ($value instanceof external_single_structure) {
118-
119-
foreach ($value->keys as $subvalue) {
120-
remove_default_closures($subvalue);
121-
}
122-
123-
} else if ($value instanceof external_multiple_structure) {
124-
remove_default_closures($value->content);
125-
}
126-
}

0 commit comments

Comments
 (0)