Browse files

[UPDATE] SoapHeader setters and "from schema" meta

SoapHeader setters are now generated first so the sample file will call
them first as it should be done if you need to set SoapHeaders. They are
also more efficient on namespace definition and on parameters naming.

The meta information "from schema" is introduced in each generated
Struct and Enum class in order to identify the origin of the class. It's
pretty useful when WSDL includes/imports numerous schemas.
  • Loading branch information...
1 parent 3449c6d commit 2d3eea6b38e7ce5ef17f91955e7cab0787e7ae54 @mikaelcom committed Apr 6, 2013
Showing with 85 additions and 58 deletions.
  1. +15 −4 WsdlToPhpGenerator.php
  2. +1 −12 WsdlToPhpModel.php
  3. +69 −42 WsdlToPhpService.php
View
19 WsdlToPhpGenerator.php
@@ -2054,7 +2054,7 @@ protected function manageWsdlNode($_wsdlLocation = '',$_domNode = null,$_fromWsd
/**
* Element
*/
- elseif(stripos($_domNode->nodeName,'element') !== false)
+ elseif(stripos($_domNode->nodeName,'element') !== false || stripos($_domNode->nodeName,'complextype') !== false)
$this->manageWsdlNodeElement($_wsdlLocation,$_domNode,$_fromWsdlLocation);
/**
* Documentation's
@@ -2282,6 +2282,9 @@ protected function manageWsdlNodeRestriction($_wsdlLocation = '',DOMNode $_domNo
* @uses WsdlToPhpGenerator::addRestrictionValue()
* @uses WsdlToPhpGenerator::auditInit()
* @uses WsdlToPhpGenerator::audit()
+ * @uses WsdlToPhpGenerator::getStruct()
+ * @uses WsdlToPhpGenerator::addStructMeta()
+ * @uses WsdlToPhpModel::getMetaValue()
* @uses DOMElement::getAttribute()
* @uses DOMElement::hasAttribute()
* @param string $_wsdlLocation the wsdl location
@@ -2294,7 +2297,11 @@ protected function manageWsdlNodeEnumeration($_wsdlLocation = '',DOMNode $_domNo
self::auditInit('managewsdlnode_enumeration',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
$parentNode = self::findSuitableParent($_domNode);
if($parentNode && $_domNode->hasAttribute('value'))
+ {
+ if($this->getStruct($parentNode->getAttribute('name')) && !$this->getStruct($parentNode->getAttribute('name'))->getMetaValue('from schema',false))
+ $this->addStructMeta($parentNode->getAttribute('name'),'from schema',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
$this->addRestrictionValue($parentNode->getAttribute('name'),$_domNode->getAttribute('value'));
+ }
self::audit('managewsdlnode_enumeration',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
}
/**
@@ -2303,6 +2310,7 @@ protected function manageWsdlNodeEnumeration($_wsdlLocation = '',DOMNode $_domNo
* @uses WsdlToPhpGenerator::addStructAttributeMeta()
* @uses WsdlToPhpGenerator::auditInit()
* @uses WsdlToPhpGenerator::audit()
+ * @uses WsdlToPhpGenerator::addStructMeta()
* @uses DOMElement::getAttribute()
* @param string $_wsdlLocation the wsdl location
* @param DOMNode $_domNode the node
@@ -2312,9 +2320,7 @@ protected function manageWsdlNodeEnumeration($_wsdlLocation = '',DOMNode $_domNo
protected function manageWsdlNodeElement($_wsdlLocation = '',DOMNode $_domNode,$_fromWsdlLocation = '')
{
self::auditInit('managewsdlnode_element',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
- /**
- * Nothing to do yet
- */
+ $this->addStructMeta($_domNode->getAttribute('name'),'from schema',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
self::audit('managewsdlnode_element',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
}
/**
@@ -2621,6 +2627,9 @@ protected function manageWsdlNodeHeader($_wsdlLocation = '',DOMNode $_domNode,$_
* @uses DOMElement::getAttribute()
* @uses WsdlToPhpGenerator::findSuitableParent()
* @uses WsdlToPhpGenerator::getStructAttribute()
+ * @uses WsdlToPhpGenerator::getStruct()
+ * @uses WsdlToPhpGenerator::addStructMeta()
+ * @uses WsdlToPhpModel::getMetaValue()
* @uses WsdlToPhpModel::getModelByName()
* @uses WsdlToPhpModel::getInheritance()
* @uses WsdlToPhpStruct::getIsStruct()
@@ -2663,6 +2672,8 @@ protected function manageWsdlNodeAttribute($_wsdlLocation = '',DOMNode $_domNode
$parentNode = self::findSuitableParent($_domNode);
if($parentNode)
{
+ if($this->getStruct($parentNode->getAttribute('name')) && !$this->getStruct($parentNode->getAttribute('name'))->getMetaValue('from schema',false))
+ $this->addStructMeta($parentNode->getAttribute('name'),'from schema',!empty($_wsdlLocation)?$_wsdlLocation:$_fromWsdlLocation);
$attributes = $_domNode->attributes;
$attributesLength = $attributes->length;
for($i = 0;$i < $attributesLength;$i++)
View
13 WsdlToPhpModel.php
@@ -555,18 +555,7 @@ public static function getValueWithinItsType($_value,$_knownType = null)
'double',
'decimal')))
return floatval($_value);
- elseif(is_numeric($_value) || in_array($_knownType,array(
- 'time',
- 'positiveInteger',
- 'unsignedLong',
- 'unsignedInt',
- 'short',
- 'long',
- 'int',
- 'integer',
- 'float',
- 'double',
- 'decimal')))
+ elseif(is_numeric($_value))
return intval($_value) == $_value?intval($_value):floatval($_value);
elseif(is_bool($_value) || in_array($_knownType,array(
'bool',
View
111 WsdlToPhpService.php
@@ -54,6 +54,7 @@ public function getDocSubPackages()
* @uses WsdlToPhpModel::getGenericWsdlClassName()
* @uses WsdlToPhpModel::getMetaValue()
* @uses WsdlToPhpModel::cleanString()
+ * @uses WsdlToPhpStruct::getContextualPart()
* @uses WsdlToPhpService::getFunctions()
* @uses WsdlToPhpFunction::getReturnType()
* @uses WsdlToPhpFunction::getComment()
@@ -69,12 +70,11 @@ public function getClassBody(&$_body)
{
$returnTypes = array();
$soapHeaders = array();
+ /**
+ * Gather informations
+ */
foreach($this->getFunctions() as $function)
{
- $function = $this->getFunction($function->getName());
- array_push($_body,array(
- 'comment'=>$function->getComment()));
- $function->getBody($_body);
/**
* Gather return types
*/
@@ -98,60 +98,52 @@ public function getClassBody(&$_body)
{
$soapHeaderType = str_replace('{@link ','',$soapHeaderTypes[$index]);
$soapHeaderType = str_replace('}','',$soapHeaderType);
- $soapHeaders[$soapHeaderName . '-' . $soapHeaderType . '-' . $soapHeaderNameSpace] = array(
- 'name'=>$soapHeaderName,
- 'type'=>$soapHeaderType,
- 'namespace'=>$soapHeaderNameSpace);
+ $soapHeaderKey = $soapHeaderName . '-' . $soapHeaderType;
+ if(!array_key_exists($soapHeaderKey,$soapHeaders))
+ $soapHeaders[$soapHeaderKey] = array(
+ 'name'=>$soapHeaderName,
+ 'type'=>$soapHeaderType,
+ 'namespaces'=>array(
+ $soapHeaderNameSpace));
+ elseif(!in_array($soapHeaderNameSpace,$soapHeaders[$soapHeaderKey]['namespaces']))
+ array_push($soapHeaders[$soapHeaderKey]['namespaces'],$soapHeaderNameSpace);
}
}
}
/**
- * Generates the override getResult method
- */
- if(count($returnTypes) && WsdlToPhpGenerator::getOptionGenerateWsdlClassFile())
- {
- $returnTypes = array_unique($returnTypes);
- natcasesort($returnTypes);
- /**
- * getResult() method comments
- */
- $comments = array();
- array_push($comments,'Returns the result');
- array_push($comments,'@see ' . self::getGenericWsdlClassName() . '::getResult()');
- array_push($comments,'@return ' . implode('|',$returnTypes));
- /**
- * getResult() method body
- */
- array_push($_body,array(
- 'comment'=>$comments));
- array_push($_body,'public function getResult()');
- array_push($_body,'{');
- array_push($_body,'return parent::getResult();');
- array_push($_body,'}');
- unset($comments);
- }
- /**
* Generates the SoapHeaders setter methods
*/
if(count($soapHeaders))
{
+ $whateverStruct = new WsdlToPhpStruct('whatever');
+ $soapHeaderNameUniqueMethods = array();
foreach($soapHeaders as $soapHeader)
{
$soapHeaderName = $soapHeader['name'];
$soapHeaderType = $soapHeader['type'];
- $soapHeaderNameSpace = $soapHeader['namespace'];
+ $soapHeaderNameSpaces = $soapHeader['namespaces'];
$cleanedName = $this->cleanString($soapHeaderName,false);
+ $headerParamKnown = strpos($soapHeaderType,WsdlToPhpGenerator::getPackageName() . $whateverStruct->getContextualPart()) === 0;
+ $methodName = ucfirst($cleanedName);
+ /**
+ * Ensure unique setter naming
+ */
+ if(!array_key_exists($methodName,$soapHeaderNameUniqueMethods))
+ $soapHeaderNameUniqueMethods[$methodName] = 0;
+ else
+ $soapHeaderNameUniqueMethods[$methodName]++;
+ $methodName .= $soapHeaderNameUniqueMethods[$methodName]?'_' . $soapHeaderNameUniqueMethods[$methodName]:'';
/**
* setSoapHeader() method comments
*/
$comments = array();
array_push($comments,'Sets the ' . $soapHeaderName . ' SoapHeader param');
- array_push($comments,'For more inforamtion, please read the online documentation on {@link http://www.php.net/manual/en/class.soapheader.php}');
+ array_push($comments,'For more information, please read the online documentation on {@link http://www.php.net/manual/en/class.soapheader.php}');
if(WsdlToPhpGenerator::getOptionGenerateWsdlClassFile())
array_push($comments,'@uses ' . self::getGenericWsdlClassName() . '::getSoapClient()');
array_push($comments,'@uses SoapClient::__setSoapheaders()');
- array_push($comments,'@param ' . $soapHeaderType . ' $_' . lcfirst($soapHeaderType));
- array_push($comments,'@param string $_nameSpace default = ' . $soapHeaderNameSpace);
+ array_push($comments,'@param ' . $soapHeaderType . ' $_' . lcfirst($headerParamKnown?$soapHeaderType:$cleanedName));
+ array_push($comments,'@param string $_nameSpace ' . implode(', ',$soapHeaderNameSpaces));
array_push($comments,'@param bool $_mustUnderstand');
array_push($comments,'@param string $_actor');
array_push($comments,'@return bool true|false');
@@ -160,7 +152,7 @@ public function getClassBody(&$_body)
*/
array_push($_body,array(
'comment'=>$comments));
- array_push($_body,'public function setSoapHeader' . $cleanedName . '(' . (strpos($soapHeaderType,WsdlToPhpGenerator::getPackageName()) === 0?$soapHeaderType . ' ':'') . '$_' . lcfirst($soapHeaderType) . ',$_nameSpace = ' . var_export($soapHeaderNameSpace,true) . ',$_mustUnderstand = false,$_actor = null)');
+ array_push($_body,'public function setSoapHeader' . $methodName . '(' . ($headerParamKnown?$soapHeaderType . ' ':'') . '$_' . lcfirst($headerParamKnown?$soapHeaderType:$cleanedName) . ',$_nameSpace' . (count($soapHeaderNameSpaces) > 1?'':' = ' . var_export($soapHeaderNameSpaces[0],true)) . ',$_mustUnderstand = false,$_actor = null)');
array_push($_body,'{');
array_push($_body,'$defaultHeaders = @self::getSoapClient()->__default_headers;');
array_push($_body,'if(!is_array($defaultHeaders))');
@@ -177,21 +169,56 @@ public function getClassBody(&$_body)
array_push($_body,'break;');
array_push($_body,'}');
array_push($_body,'}');
+ array_push($_body,'self::getSoapClient()->__setSoapheaders(null);');
array_push($_body,'}');
array_push($_body,'if(!empty($_actor))');
array_push($_body,'{');
- array_push($_body,'array_push($defaultHeaders,new SoapHeader($_nameSpace,\'' . $soapHeaderName . '\',$_' . lcfirst($soapHeaderType) . ',$_mustUnderstand,$_actor));');
+ array_push($_body,'array_push($defaultHeaders,new SoapHeader($_nameSpace,\'' . $soapHeaderName . '\',$_' . lcfirst($cleanedName) . ',$_mustUnderstand,$_actor));');
array_push($_body,'}');
array_push($_body,'else');
array_push($_body,'{');
- array_push($_body,'array_push($defaultHeaders,new SoapHeader($_nameSpace,\'' . $soapHeaderName . '\',$_' . lcfirst($soapHeaderType) . ',$_mustUnderstand));');
+ array_push($_body,'array_push($defaultHeaders,new SoapHeader($_nameSpace,\'' . $soapHeaderName . '\',$_' . lcfirst($cleanedName) . ',$_mustUnderstand));');
array_push($_body,'}');
- array_push($_body,'self::getSoapClient()->__setSoapheaders(null);');
array_push($_body,'return self::getSoapClient()->__setSoapheaders($defaultHeaders);');
array_push($_body,'}');
- unset($comments);
+ unset($soapHeaderName,$soapHeaderType,$soapHeaderNameSpaces,$cleanedName,$headerParamKnown,$methodName,$comments);
}
}
+ /**
+ * Generates service methods
+ */
+ foreach($this->getFunctions() as $function)
+ {
+ $function = $this->getFunction($function->getName());
+ array_push($_body,array(
+ 'comment'=>$function->getComment()));
+ $function->getBody($_body);
+ }
+ /**
+ * Generates the override getResult method if needed
+ */
+ if(count($returnTypes) && WsdlToPhpGenerator::getOptionGenerateWsdlClassFile())
+ {
+ $returnTypes = array_unique($returnTypes);
+ natcasesort($returnTypes);
+ /**
+ * getResult() method comments
+ */
+ $comments = array();
+ array_push($comments,'Returns the result');
+ array_push($comments,'@see ' . self::getGenericWsdlClassName() . '::getResult()');
+ array_push($comments,'@return ' . implode('|',$returnTypes));
+ /**
+ * getResult() method body
+ */
+ array_push($_body,array(
+ 'comment'=>$comments));
+ array_push($_body,'public function getResult()');
+ array_push($_body,'{');
+ array_push($_body,'return parent::getResult();');
+ array_push($_body,'}');
+ unset($comments);
+ }
unset($returnTypes,$soapHeaders);
}
}

0 comments on commit 2d3eea6

Please sign in to comment.