
be.selckin.ws.util.java2php.PhpWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-php-soap-codegen Show documentation
Show all versions of cxf-php-soap-codegen Show documentation
php soap api code generator based on cxf jax-ws models
The newest version!
package be.selckin.ws.util.java2php;
import be.selckin.ws.util.java2php.php.Argument;
import be.selckin.ws.util.java2php.php.Operation;
import be.selckin.ws.util.java2php.php.PhpClass;
import be.selckin.ws.util.java2php.php.PhpEnum;
import be.selckin.ws.util.java2php.php.PhpProperty;
import be.selckin.ws.util.java2php.php.PhpService;
import be.selckin.ws.util.java2php.php.PhpType;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class PhpWriter {
public void write(Php php, EndPoint endPoint) throws IOException {
for (PhpService service : endPoint.getServices()) {
writeService(php, service, endPoint.getTypes());
}
}
private void writeService(Php php, PhpService service, List phpTypes) throws IOException {
php.appendLine("");
php.appendLine("");
php.appendLine("// Service: " + service.getQName());
php.startNamespace(service.getNamespace());
php.startClass(service.getName());
php.appendLine("");
php.appendField("client", "null");
php.appendLine("");
php.startFunction("__construct", Arrays.asList(new Argument("url"), new Argument("extraConfig")));
php.appendLine("$this->client = new \\SoapClient($url, array_merge(array(");
php.appendLine("'style' => SOAP_DOCUMENT,");
php.appendLine("'features' => SOAP_SINGLE_ELEMENT_ARRAYS,");
php.appendLine("'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,");
php.appendLine("'exceptions' => TRUE,");
php.appendLine("'classmap' => array(");
php.indentMore();
writeClassMap(php, phpTypes);
php.indentLess();
php.appendLine("),");
php.appendLine("), $extraConfig));");
php.endFunction();
for (Operation operation : service.getOperations()) {
List arguments = operation.getArguments();
php.appendLine("");
php.appendPhpDocReturn(operation.getTypeHint());
php.startFunction(operation.getName(), arguments);
StringBuilder call = new StringBuilder("$this->client->" + operation.getName() + "(");
Iterator it = arguments.iterator();
if (it.hasNext()) {
call.append("array(");
String name = it.next().getName();
call.append("\"").append(name).append("\" => ").append("$").append(name);
while (it.hasNext()) {
call.append(", ");
name = it.next().getName();
call.append("\"").append(name).append(" => ").append("$").append(name);
}
call.append(")");
}
call.append(");");
php.appendLine("return " + call.toString());
php.endFunction();
}
php.endClass();
php.endNamespace();
}
private void writeClassMap(Php php, List phpTypes) throws IOException {
for (PhpClass phpClass : Iterables.filter(phpTypes, PhpClass.class)) {
php.appendLine(String.format("\"%s\" => \"%s\",", phpClass.getQName().getLocalPart(), phpClass.getAbsolutePhpName().replace("\\", "\\\\")));
}
}
public void write(Php code, Set types) throws IOException {
for (Entry> entry : groupNs(types).asMap().entrySet()) {
code.startNamespace(entry.getKey());
{
for (PhpType phpType : entry.getValue()) {
code.appendLine("//");
code.appendLine("// XML Schema item " + phpType.getQName().toString());
code.appendLine("//");
if (phpType instanceof PhpClass) {
write(code, (PhpClass) phpType);
} else if (phpType instanceof PhpEnum) {
write(code, (PhpEnum) phpType);
} else
throw new RuntimeException("Can't handle: " + phpType);
}
}
code.endNamespace();
}
}
private void write(Php code, PhpEnum anEnum) throws IOException {
code.startInterface(anEnum.getName());
{
for (String value : anEnum.getValues()) {
code.appendConst(value, value);
}
}
code.endInterface();
}
private void write(Php code, PhpClass phpClass) throws IOException {
code.startClass(phpClass.getName());
{
for (PhpProperty phpProperty : phpClass.getProperties()) {
for (String comment : phpProperty.getComments()) {
code.appendLine("//" + comment);
}
code.appendPhpDocVar(phpProperty);
code.appendField(phpProperty.getName(), phpProperty.getInitialValue());
}
code.appendLine("");
List required = phpClass.getRequiredProperties();
if (!required.isEmpty()) {
List args = new ArrayList<>();
for (PhpProperty property : required) {
args.add(new Argument(property.getTypeHint(), property.getName()));
}
code.appendPhpDocVars(required);
code.startFunction("__construct", args);
{
for (PhpProperty property : required) {
code.appendLine("$this->" + property.getSetterName() + "($" + property.getName() + ");");
}
}
code.endFunction();
}
code.appendLine("");
for (PhpProperty phpProperty : phpClass.getProperties()) {
code.appendPhpDocReturn(phpProperty.getTypeHint());
code.startFunction(phpProperty.getGetterName(), Collections.emptyList());
code.appendLine("return $this->" + phpProperty.getName() + ";");
code.endFunction();
code.appendPhpDocParam("value", phpProperty);
if (phpProperty.getExplicitNamespace() == null) {
code.startFunction(phpProperty.getSetterName(), Arrays.asList(new Argument(phpProperty.getTypeHint(), "value")));
code.appendLine("$this->" + phpProperty.getName() + " = $value;");
code.endFunction();
} else {
code.startFunction(phpProperty.getSetterName(), Arrays.asList(new Argument(phpProperty.getTypeHint(), "value")));
code.appendLine("$this->" + phpProperty.getName() + " = array_map(function($item) {");
code.appendLine(" if ($item instanceof \\SoapVar) {");
code.appendLine(" return $item;");
code.appendLine(" } else {");
code.appendLine(" return new \\SoapVar($item, SOAP_ENC_OBJECT, \"" + phpProperty.getName() + "\", \"" + phpProperty.getExplicitNamespace() + "\", null, \"" + phpProperty.getExplicitNamespace() + "\");");
code.appendLine(" }");
code.appendLine("}, $value);");
code.endFunction();
}
}
}
code.endClass();
}
private TreeMultimap groupNs(Set types) {
TreeMultimap perNamespace = TreeMultimap.create(Ordering.natural(), new Comparator() {
@Override
public int compare(PhpType o1, PhpType o2) {
return ComparisonChain.start().compare(o1.getName(), o2.getName()).result();
}
});
for (PhpType type : types) {
perNamespace.put(type.getNamespace(), type);
}
return perNamespace;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy