mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
web service MDL-12886
* Remove description array => all these information are now into the phpdoc. Remove all call/reference to moodleexternal.php * Adapt our own REST server to these changes * Remove Zend REST server as it's going to be deprecated in Zend Framework 1.8 * Remove our own SOAP server as we use the Zend SOAP server
This commit is contained in:
parent
60cd5b5a34
commit
40f024c9f3
17 changed files with 457 additions and 835 deletions
|
@ -46,13 +46,13 @@ final class webservice_lib {
|
|||
continue;
|
||||
}
|
||||
$path = $directorypath . '/' . $file;
|
||||
///browse the subfolder
|
||||
///browse the subfolder
|
||||
if( is_dir($path) ) {
|
||||
require_once($path."/lib.php");
|
||||
$classname = $file."_server";
|
||||
$protocols[] = new $classname;
|
||||
require_once($path."/lib.php");
|
||||
$classname = $file."_server";
|
||||
$protocols[] = new $classname;
|
||||
}
|
||||
///retrieve api.php file
|
||||
///retrieve api.php file
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
|
@ -106,11 +106,11 @@ final class webservice_lib {
|
|||
continue;
|
||||
}
|
||||
$path = $directorypath . '/' . $file;
|
||||
///browse the subfolder
|
||||
///browse the subfolder
|
||||
if( is_dir($path) ) {
|
||||
webservice_lib::setListApiFiles($files, $path);
|
||||
webservice_lib::setListApiFiles($files, $path);
|
||||
}
|
||||
///retrieve api.php file
|
||||
///retrieve api.php file
|
||||
else if ($file == "external.php") {
|
||||
$files[] = $path;
|
||||
}
|
||||
|
@ -122,6 +122,278 @@ final class webservice_lib {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate description array from the phpdoc
|
||||
* TODO: works but it need serious refactoring
|
||||
* @param <type> $file
|
||||
* @param <type> $class
|
||||
* @return <type>
|
||||
*/
|
||||
public static function generate_webservice_description($file, $class){
|
||||
require_once($file);
|
||||
require_once "Zend/Loader.php";
|
||||
Zend_Loader::registerAutoload();
|
||||
$reflection = Zend_Server_Reflection::reflectClass($class);
|
||||
$description = array();
|
||||
|
||||
foreach($reflection->getMethods() as $method){
|
||||
|
||||
if ($method->getName()!="get_function_webservice_description"
|
||||
&& $method->getName()!="get_descriptions" ) {
|
||||
$docBlock = $method->getDocComment();
|
||||
|
||||
|
||||
//retrieve the return and add it into the description if not array|object
|
||||
preg_match_all('/@return\s+(\w+)\s+((?:\$)?\w+)/', $docBlock, $returnmatches);
|
||||
|
||||
//retrieve the subparam and subreturn
|
||||
preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches);
|
||||
|
||||
for($i=0;$i<sizeof($matches[1]);$i++){
|
||||
// var_dump(strpos("optional", $matches[4][$i]));
|
||||
switch ($matches[1][$i]) {
|
||||
case "subparam":
|
||||
if (strpos($matches[4][$i], "optional")!==false) {
|
||||
$descriptiontype = "optional";
|
||||
} else {
|
||||
$descriptiontype = "params" ;
|
||||
}
|
||||
break;
|
||||
case "subreturn":
|
||||
$descriptiontype = "return";
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($description)) {
|
||||
$description[$method->getName()] = array();
|
||||
}
|
||||
|
||||
if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) {
|
||||
$description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]);
|
||||
}
|
||||
|
||||
if ($matches[1][$i] == "subparam" || $matches[1][$i] == "subreturn") {
|
||||
|
||||
|
||||
///algorythm parts
|
||||
///1. We compare the string to the description array
|
||||
/// When we find a attribut that is not in the description, we retrieve all the rest of the string
|
||||
///2. We create the missing part of the description array, starting from the end of the rest of the string
|
||||
///3. We add the missing part to the description array
|
||||
|
||||
///Part 1.
|
||||
|
||||
|
||||
///construct the description array
|
||||
if (strpos($matches[3][$i], "->")===false && strpos($matches[3][$i], ":")===false) {
|
||||
//no separator
|
||||
$otherparam = $matches[3][$i];
|
||||
}
|
||||
else if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) {
|
||||
$separator = ":";
|
||||
$separatorsize=1;
|
||||
|
||||
} else {
|
||||
$separator = "->";
|
||||
$separatorsize=2;
|
||||
}
|
||||
|
||||
$param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1);
|
||||
|
||||
$otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize);
|
||||
$parsingdesc = $description[$method->getName()];
|
||||
|
||||
if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){
|
||||
$parsingdesc = $parsingdesc[$descriptiontype];
|
||||
}
|
||||
$descriptionpath=array();
|
||||
|
||||
$creationfinished = false;
|
||||
unset($type);
|
||||
|
||||
while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) {
|
||||
if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) {
|
||||
$type = $separator;
|
||||
|
||||
$separator = ":";
|
||||
$separatorsize=1;
|
||||
} else {
|
||||
$type = $separator;
|
||||
$separator = "->";
|
||||
$separatorsize=2;
|
||||
}
|
||||
|
||||
$param = substr($otherparam,0,strpos($otherparam, $separator));
|
||||
|
||||
$otherparam = substr($otherparam,strpos($otherparam, $separator)+$separatorsize);
|
||||
|
||||
|
||||
if ($type==":") {
|
||||
if (!array_key_exists('multiple:'.$param, $parsingdesc)){
|
||||
|
||||
$desctoadd = webservice_lib::create_end_of_description(":".$param.$separator.$otherparam, $matches[2][$i]);
|
||||
|
||||
if(empty($descriptionpath) ) {
|
||||
if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
|
||||
$desctoadd = array($descriptiontype => $desctoadd);
|
||||
}
|
||||
$paramtoadd = $descriptiontype;
|
||||
} else {
|
||||
$paramtoadd = 'multiple:'.$param;
|
||||
}
|
||||
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
|
||||
$creationfinished = true;
|
||||
} else {
|
||||
if(empty($descriptionpath)) {
|
||||
$descriptionpath[] = $descriptiontype;
|
||||
}
|
||||
$descriptionpath[] = 'multiple:'.$param;
|
||||
$parsingdesc = $parsingdesc['multiple:'.$param];
|
||||
}
|
||||
} else {
|
||||
if (!array_key_exists($param, $parsingdesc)){
|
||||
|
||||
$desctoadd = webservice_lib::create_end_of_description("->".$param.$separator.$otherparam, $matches[2][$i]);
|
||||
|
||||
if(empty($descriptionpath)) {
|
||||
|
||||
if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
|
||||
$desctoadd = array($descriptiontype => $desctoadd);
|
||||
}
|
||||
$paramtoadd = $descriptiontype;
|
||||
|
||||
} else {
|
||||
$paramtoadd = $param;
|
||||
}
|
||||
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
|
||||
|
||||
$creationfinished = true;
|
||||
} else {
|
||||
if(empty($descriptionpath)) {
|
||||
$descriptionpath[] = $descriptiontype;
|
||||
}
|
||||
$descriptionpath[] = $param;
|
||||
$parsingdesc = $parsingdesc[$param];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!$creationfinished) {
|
||||
|
||||
if (!empty($type) && $type==":") {
|
||||
|
||||
$desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]);
|
||||
|
||||
if(empty($descriptionpath)) {
|
||||
if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
|
||||
$desctoadd = array($descriptiontype => $desctoadd);
|
||||
}
|
||||
$paramtoadd = $descriptiontype;
|
||||
} else {
|
||||
$paramtoadd = 'multiple:'.$param;
|
||||
}
|
||||
|
||||
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
|
||||
|
||||
} else {
|
||||
$desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]);
|
||||
if(empty($descriptionpath)) {
|
||||
|
||||
if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
|
||||
$desctoadd = array($descriptiontype => $desctoadd);
|
||||
}
|
||||
$paramtoadd = $descriptiontype;
|
||||
|
||||
} else {
|
||||
$paramtoadd = $param;
|
||||
|
||||
}
|
||||
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: works but it needs refactoring
|
||||
* @param <type> $param
|
||||
* @param <type> $desctoadd
|
||||
* @param <type> $descriptionlevel
|
||||
* @param <type> $descriptionpath
|
||||
* @param <type> $level
|
||||
*/
|
||||
public static function add_end_of_description($param, $desctoadd, &$descriptionlevel, $descriptionpath, $level= 0){
|
||||
if (sizeof($descriptionpath)==0 || sizeof($descriptionpath)==$level+1) {
|
||||
|
||||
if (is_array($descriptionlevel) && !empty($descriptionlevel)) {
|
||||
foreach($desctoadd as $key=>$value) {
|
||||
if ($key!="params" && $key!="optional" && $key!="return") { //TODO
|
||||
$descriptionlevel[$param][$key] = $value;
|
||||
} else {
|
||||
$descriptionlevel[$param] = $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$descriptionlevel = $desctoadd;
|
||||
}
|
||||
} else {
|
||||
webservice_lib::add_end_of_description($param, $desctoadd, &$descriptionlevel[$descriptionpath[$level]], $descriptionpath, $level+1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TODO: works but it needs refactoring
|
||||
* @param <type> $stringtoadd
|
||||
* @param <type> $type
|
||||
* @return <type>
|
||||
*/
|
||||
public static function create_end_of_description($stringtoadd, $type) {
|
||||
|
||||
if (strrpos($stringtoadd, "->")===false || (strrpos($stringtoadd, ":")!==false && (strrpos($stringtoadd, ":") > strrpos($stringtoadd, "->")))) {
|
||||
$separator = ":";
|
||||
$separatorsize=1;
|
||||
} else {
|
||||
$separator = "->";
|
||||
$separatorsize=2;
|
||||
}
|
||||
|
||||
$param = substr($stringtoadd,strrpos($stringtoadd, $separator)+$separatorsize);
|
||||
$result = array( $param => $type);
|
||||
|
||||
$otherparam = substr($stringtoadd,0,strlen($stringtoadd)-strlen($param)-$separatorsize);
|
||||
|
||||
while(strrpos($otherparam, ":")!==false || strrpos($otherparam, "->")!==false) {
|
||||
if (strrpos($otherparam, "->")===false || (strrpos($otherparam, ":")!==false && (strrpos($otherparam, ":") > strrpos($otherparam, "->")))) {
|
||||
$separator = ":";
|
||||
$separatorsize=1;
|
||||
} else {
|
||||
$separator = "->";
|
||||
$separatorsize=2;
|
||||
}
|
||||
$param = substr($otherparam,strrpos($otherparam, $separator)+$separatorsize);
|
||||
$otherparam = substr($otherparam,0,strrpos($otherparam, $separator));
|
||||
|
||||
if ($separator==":") {
|
||||
$result = array('multiple:'.$param => $result);
|
||||
} else {
|
||||
$result = array($param => $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -189,7 +461,7 @@ final class wsuser_form extends moodleform {
|
|||
global $DB;
|
||||
$this->username = $this->_customdata['username'];
|
||||
$mform =& $this->_form;
|
||||
|
||||
|
||||
$strrequired = get_string('required');
|
||||
|
||||
$mform->addElement('hidden', 'username', $this->username);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue