Skip to content

Commit

Permalink
Some structure improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Oct 26, 2023
1 parent fa4d381 commit dada31a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 61 deletions.
90 changes: 83 additions & 7 deletions structure/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,96 @@ function convert_key_type($key, $type, $required, $indentation) {
/**
* Print structure ready to use.
*/
function print_ws_structure($name, $structure, $useparams) {
function print_ws_structures($structures) {
foreach ($structures as $wsname => $structure) {
$type = getTSTypeFromName($wsname);

print_ws_structure($wsname, $type, $structure, true);
print_ws_structure($wsname, $type, $structure, false);
}
}

/**
* Print params or return structures.
*/
function print_ws_structure($wsname, $type, $structure, $useparams) {
if ($useparams) {
$type = implode('', array_map('ucfirst', explode('_', $name))) . 'WSParams';
$comment = "Params of $name WS.";
$comment = "Params of $wsname WS.";
$type .= 'WSParams';
$typestructure = $structure->parameters_desc;
$export = '';
} else {
$type = implode('', array_map('ucfirst', explode('_', $name))) . 'WSResponse';
$comment = "Data returned by $name WS.";
$comment = "Data returned by $wsname WS.";
$type .= 'WSResponse';
$typestructure = $structure->returns_desc;
$export = 'export ';
}

echo "
/**
* $comment
* $comment";
if (!empty($structure->description)) {
echo "
*
* WS Description: $structure->description";
}
if (isset($structure->deprecated) && $structure->deprecated) {
echo "
*
* @deprecatedonmoodle since ADDVERSIONHERE. This WS method is deprecated";
}
/* echo "
* WS Type: $structure->type
* Allowed from AJAX: " . ($structure->allowed_from_ajax ? 'yes' : 'no') . "
* Read only session: " . ($structure->readonlysession ? 'yes' : 'no') . "
* Login required: " . ($structure->loginrequired ? 'yes' : 'no');*/
echo "
*/
${export}type $type = ".convert_to_ts(null, $typestructure).";\n";
}

/**
* Returns TS Type From WS Name.
*/
export type $type = ".convert_to_ts(null, $structure).";\n";
function getTSTypeFromName($wsname) {
$type = implode('', array_map('ucfirst', explode('_', $wsname) ) );
$search = [
'/^Block/',
'/^Mod/',
'/^Enrol/',
'/^Gradereport/',
'/^CoreCalendar/',
'/^CoreBadges/',
'/^CoreBlog/',
'/^CoreCompetency/',
'/^CoreFiles/',
'/^CoreMessage/',
'/^CoreNotes/',
'/^MessageAirnotifier/',
'/^ReportInsights/',
'/^ToolLp/',
'/^ToolMobile/',
];

$replaces = [
'AddonBlock',
'AddonMod',
'AddonEnrol',
'CoreGradesGradereport',
'AddonCalendar',
'AddonBadges',
'AddonBlog',
'AddonCompetency',
'AddonPrivateFiles',
'AddonMessages',
'AddonNotes',
'AddonMessageOutputAirnotifier',
'AddonReportInsights',
'AddonCompetency',
'CoreSite',
];

return preg_replace($search, $replaces, $type);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions structure/get_all_ws_structures.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,4 @@

$structures = get_all_ws_structures();

foreach ($structures as $wsname => $structure) {

remove_default_closures($structure->parameters_desc);
print_ws_structure($wsname, $structure->parameters_desc, true);

remove_default_closures($structure->returns_desc);
print_ws_structure($wsname, $structure->returns_desc, false);
}
print_ws_structures($structures);
23 changes: 0 additions & 23 deletions structure/ws_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,3 @@ function convert_to_ts($key, $value, $boolisnumber = false, $indentation = '', $
return "{}; // WARNING: Unknown structure: $key " . get_class($value);
}
}

/**
* Remove all closures (anonymous functions) in the default values so the object can be serialized.
*/
function remove_default_closures($value) {
if ($value instanceof external_warnings || $value instanceof external_files) {
// Ignore these types.

} else if ($value instanceof external_value) {
if ($value->default instanceof Closure) {
$value->default = null;
}

} else if ($value instanceof external_single_structure) {

foreach ($value->keys as $subvalue) {
remove_default_closures($subvalue);
}

} else if ($value instanceof external_multiple_structure) {
remove_default_closures($value->content);
}
}
23 changes: 0 additions & 23 deletions structure/ws_functions_41.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,3 @@ function convert_to_ts($key, $value, $boolisnumber = false, $indentation = '', $
return "{}; // WARNING: Unknown structure: $key " . get_class($value);
}
}

/**
* Remove all closures (anonymous functions) in the default values so the object can be serialized.
*/
function remove_default_closures($value) {
if ($value instanceof external_warnings || $value instanceof external_files) {
// Ignore these types.

} else if ($value instanceof external_value) {
if ($value->default instanceof Closure) {
$value->default = null;
}

} else if ($value instanceof external_single_structure) {

foreach ($value->keys as $subvalue) {
remove_default_closures($subvalue);
}

} else if ($value instanceof external_multiple_structure) {
remove_default_closures($value->content);
}
}

0 comments on commit dada31a

Please sign in to comment.