Class CWsdlGenerator
CWsdlGenerator generates the WSDL for a given service class.
The WSDL generation is based on the doc comments found in the service class file. In particular, it recognizes the '@soap' tag in the comment and extracts API method and type definitions.
In a service class, a remote invokable method must be a public method with a doc comment block containing the '@soap' tag. In the doc comment, the type and name of every input parameter and the type of the return value should be declared using the standard phpdoc format.
CWsdlGenerator recognizes the following primitive types (case-sensitive) in the parameter and return type declarations:- str/string: maps to xsd:string;
- int/integer: maps to xsd:int;
- float/double: maps to xsd:float;
- bool/boolean: maps to xsd:boolean;
- date: maps to xsd:date;
- time: maps to xsd:time;
- datetime: maps to xsd:dateTime;
- array: maps to xsd:string;
- object: maps to xsd:struct;
- mixed: maps to xsd:anyType.
If a type is not a primitive type, it is considered as a class type, and CWsdlGenerator will look for its property declarations. Only public properties are considered, and they each must be associated with a doc comment block containg the '@soap' tag. The doc comment block should declare the type of the property.
CWsdlGenerator recognizes the array type with the following format:
typeName[]: maps to tns:typeNameArray
The following is an example declaring a remote invokable method:
/ ** * A foo method. * @param string name of something * @param string value of something * @return string[] some array * @soap * / public function foo($name,$value) {...}
And the following is an example declaring a class with remote accessible properties:
class Foo { / ** * @var string name of foo {nillable=1, minOccurs=0, maxOccurs=2} * @soap * / public $name; / ** * @var Member[] members of foo * @soap * / public $members; }
In the above, the 'members' property is an array of 'Member' objects. Since 'Member' is not a primitive type, CWsdlGenerator will look further to find the definition of 'Member'.
Optionally, extra attributes (nillable, minOccurs, maxOccurs) can be defined for each property by enclosing definitions into curly brackets and separated by comma like so:
{[attribute1 = value1][, attribute2 = value2], ...}
where the attribute can be one of following:- nillable = [0|1|true|false]
- minOccurs = n; where n>=0
- maxOccurs = n; where [n>=0|unbounded]
- all - (default) allows any sorting order of child nodes
- sequence - all child nodes in WSDL XML file will be expected in predefined order
- choice - supplied can be either of the child elements
In the following example, class Foo will create a XML node <xsd:Foo><xsd:sequence> ... </xsd:sequence></xsd:Foo> with children attributes expected in pre-defined order.
/ * * @soap-indicator sequence * / class Foo { ... }
For more on soap indicators, see See http://www.w3schools.com/schema/schema_complex_indicators.asp.
Since the variability of WSDL definitions is virtually unlimited, a special doc comment tag '@soap-wsdl' can be used in order to inject any custom XML string into generated WSDL file. If such a block of the code is found in class's comment block, then it will be used instead of parsing and generating standard attributes within the class. This gives virtually unlimited flexibility in defining data structures of any complexity. Following is an example of defining custom piece of WSDL XML node:
/ * * @soap-wsdl <xsd:sequence> * @soap-wsdl <xsd:element minOccurs="1" maxOccurs="1" nillable="false" name="name" type="xsd:string"/> * @soap-wsdl <xsd:choice minOccurs="1" maxOccurs="1" nillable="false"> * @soap-wsdl <xsd:element minOccurs="1" maxOccurs="1" nillable="false" name="age" type="xsd:integer"/> * @soap-wsdl <xsd:element minOccurs="1" maxOccurs="1" nillable="false" name="date_of_birth" type="xsd:date"/> * @soap-wsdl </xsd:choice> * @soap-wsdl </xsd:sequence> * / class User { / ** * @var string User name {minOccurs=1, maxOccurs=1} * @soap * / public $name; / ** * @var integer User age {nillable=0, minOccurs=1, maxOccurs=1} * @example 35 * @soap * / public $age; / ** * @var date User's birthday {nillable=0, minOccurs=1, maxOccurs=1} * @example 1980-05-27 * @soap * / public $date_of_birth; }
In the example above, WSDL generator would inject under XML node <xsd:User> the code block defined by @soap-wsdl lines.
By inserting into SOAP URL link the parameter "?makedoc", WSDL generator will
output human-friendly overview of all complex data types rather than XML WSDL
file. Each complex type is described in a separate HTML table and recognizes
also the '@example' PHPDoc tag. See CWsdlGenerator::buildHtmlDocs()
.
- CComponent
- CWsdlGenerator
Copyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.0
Located at x2engine/framework/web/services/CWsdlGenerator.php
public
string
|
#
generateWsdl( string $className, string $serviceUrl, string $encoding = 'UTF-8' )
Generates the WSDL for the given class. |
protected
|
|
protected
|
|
protected
|
|
protected
|
#
injectDom(
Import custom XML source node into WSDL document under specified target node |
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
public
|
#
buildHtmlDocs( boolean $return = false )
Generate human friendly HTML documentation for complex data types. This method can be invoked either by inserting URL parameter "&makedoc" into URL link, e.g. "http://www.mydomain.com/soap/create?makedoc", or simply by calling from another script with argument $return=true. |
string |
STYLE_RPC
|
'rpc' |
|
string |
STYLE_DOCUMENT
|
'document' |
|
string |
USE_ENCODED
|
'encoded' |
|
string |
USE_LITERAL
|
'literal' |
public
string
|
$namespace |
|
#
the namespace to be used in the generated WSDL. If not set, it defaults to the name of the class that WSDL is generated upon. |
public
string
|
$serviceName |
|
#
the name of the generated WSDL. If not set, it defaults to "urn:{$className}wsdl". |
public
array
|
$operationBodyStyle | array(
'use' => self::USE_ENCODED,
'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/',
) |
#
soap:body operation style options |
public
array
|
$bindingStyle |
|
#
soap:operation style |
public
string
|
$bindingTransport | 'http://schemas.xmlsoap.org/soap/http' |
#
soap:operation transport |
protected static
array
|
$typeMap | array(
'string'=>'xsd:string',
'str'=>'xsd:string',
'int'=>'xsd:int',
'integer'=>'xsd:integer',
'float'=>'xsd:float',
'double'=>'xsd:float',
'bool'=>'xsd:boolean',
'boolean'=>'xsd:boolean',
'date'=>'xsd:date',
'time'=>'xsd:time',
'datetime'=>'xsd:dateTime',
'array'=>'soap-enc:Array',
'object'=>'xsd:struct',
'mixed'=>'xsd:anyType',
) |
|
protected
array
|
$operations |
|
#
List of recognized SOAP operations that will become remotely available. All methods with declared @soap parameter will be included here in the format operation1 => description1, operation2 => description2, .. |
protected
array
|
$types |
|
#
List of complex types used by operations. If an SOAP operation defines complex input or output type, all objects are included here containing all sub-parameters. For instance, if an SOAP operation "createUser" requires complex input object "User", then the object "User" will be included here with declared subparameters such as "firstname", "lastname", etc.. |
protected
array
|
$elements |
|
|
protected
array
|
$messages |
|
#
Map of request and response types for all operations. |