web serviceMDL-12886 refactor servers into object + add Zend_soap and Zend_xmlrpc server

This commit is contained in:
jerome 2009-02-11 06:57:30 +00:00
parent 625b51d4e0
commit 06e7fadc43
79 changed files with 10475 additions and 211 deletions

View file

@ -0,0 +1,127 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
require_once 'Zend/Soap/Wsdl/Parser.php';
/**
* Zend_Soap_Wsdl_CodeGenerator
*
* @category Zend
* @package Zend_Soap
*/
class Zend_Soap_Wsdl_CodeGenerator {
/**
* @var string WSDL Filename/URI
*/
private static $filename = null;
/**
* @var string PHP Code for output
*/
private static $php_code;
/**
* @var object Zend_Soap_Wsdl_Parser Result
*/
private static $wsdl;
/**
* Constructor
*
* @param string $wsdl Filename, URI or XML for the WSDL
* @param string $output Output file name, default: null
*/
public static function parse($wsdl, $output = null)
{
self::$wsdl = Zend_Soap_Wsdl_Parser::parse($wsdl);
self::$php_code = self::generatePhp();
if (!is_null($output) && is_writable($output)) {
file_put_contents($output);
}
return self::$php_code;
}
/**
* Generate the output PHP
*
* @return string
*/
private function generatePhp()
{
$php_code = '<?php' . "\n";
if (isset(self::$wsdl->documentation)) {
$docs = self::$wsdl->documentation;
$docs = explode("\n", $docs);
$php_code .= "/**\n";
foreach ($docs as $line) {
$php_code .= ' * ' .trim($line). PHP_EOL;
}
$php_code .= " */\n\n";
}
if (!isset(self::$wsdl->name)) {
$classname = 'SoapService';
} else {
$classname = self::$wsdl->name;
}
$php_code .= "class {$classname} {\n";
foreach (self::$wsdl->operations as $name => $io) {
if (isset($io['documentation'])) {
$php_code .= "\n\t/**\n";
$docs = $io['documentation'];
$docs = explode("\n", $docs);
foreach ($docs as $line) {
$php_code .= "\t * " .trim($line). PHP_EOL;
}
$php_code .= "\t */\n";
}
$php_code .= "\n\tpublic function {$name} (";
if (isset($io['input'])) {
$arg_names = array();
foreach ($io['input'] as $arg) {
$arg_names[] = $arg['name'];
}
$php_code .= '$' .implode(', $', $arg_names);
}
$php_code .= ')';
$php_code .= "\n\t{";
$php_code .= "\n\t\t\n";
if (isset($io['output'])) {
$php_code .= "\t\treturn \${$io['output']['name']};\n";
}
$php_code .= "\t}\n";
}
$php_code .= "\n}";
$php_code .= PHP_EOL. "\$server = new SoapServer;" .PHP_EOL;
$php_code .= "\$server->setClass($classname);";
$php_code .= "\n?>";
return $php_code;
}
}

View file

@ -0,0 +1,25 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @subpackage Wsdl
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
require_once "Zend/Exception.php";
class Zend_Soap_Wsdl_Exception extends Zend_Exception { }

View file

@ -0,0 +1,173 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
require_once 'Zend/Soap/Wsdl/Parser/Result.php';
/**
* Zend_Soap_Wsdl_Parser
*
* @category Zend
* @package Zend_Soap
*/
class Zend_Soap_Wsdl_Parser {
/**
* @var SimpleXML object for the WSDL document being parsed
*/
private static $xml;
/**
* Parse a WSDL document into a generic object
*
* @param string|file $wsdl The WSDL document or a filename for the WSDL document to parse
* @return Zend_Soap_Wsdl_Parser_Result The contents of the WSDL file
*/
public static function parse($wsdl)
{
if (strpos($wsdl, '<') === false) {
$wsdl_result = new Zend_Soap_Wsdl_Parser_Result($wsdl);
$wsdl = file_get_contents($wsdl);
} else {
$tmp = tempnam(ini_get('upload_tmp_dir'), 'ZF_Temp_');
file_put_contents($tmp, $wsdl);
$wsdl_result = new Zend_Soap_Wsdl_Parser_Result($tmp);
}
self::$xml = simplexml_load_string($wsdl);
/* This is done so that we have a known prefix to the WSDL elements
for XPath queries */
self::$xml['xmlns:zfwsdl'] = 'http://schemas.xmlsoap.org/wsdl/';
self::$xml = simplexml_load_string(self::$xml->asXML());
if (isset(self::$xml->documentation)) {
$wsdl_result->documentation = trim(self::$xml->documentation);
}
if (!isset(self::$xml['name'])) {
$wsdl_result->name = null;
} else {
$wsdl_result->name = (string) self::$xml['name'];
}
foreach (self::$xml->binding->operation as $operation) {
$name = (string) $operation['name'];
$wsdl_result->operations[$name] = array();
$wsdl_result->operations[$name]['input'] = self::getOperationInputs($name);
$wsdl_result->operations[$name]['output'] = self::getOperationOutput($name);
$wsdl_result->operations[$name]['documentation'] = self::getDocs($name);
}
$wsdl_result->portType = (string) self::$xml->portType['name'];
$wsdl_result->binding = (string) self::$xml->binding['name'];
$wsdl_result->service['name'] = (string) self::$xml->service['name'];
$wsdl_result->service['address'] = (string) self::$xml->service->port->children('http://schemas.xmlsoap.org/wsdl/soap/')->attributes();
$wsdl_result->targetNamespace = (string) self::$xml['targetNamespace'];
return $wsdl_result;
}
/**
* Get Function arguments
*
* @param string $operation_name Name of the <operation> element to find
* @return string
*/
private static function getOperationInputs($operation_name)
{
$operation = self::$xml->xpath('/zfwsdl:definitions[1]/zfwsdl:portType/zfwsdl:operation[@name="' .$operation_name. '"]');
if ($operation == null) {
return '';
}
if (isset($operation[0]->input)) {
$input_message_name = $operation[0]->input['message'];
$input_message_name = explode(':', $input_message_name);
$input_message_name = $input_message_name[1];
$input_message = self::$xml->xpath('/zfwsdl:definitions[1]/zfwsdl:message[@name="' .$input_message_name. '"]');
}
if ($input_message != null) {
foreach ($input_message[0]->part as $part) {
$args[] = array(
'name' => (string) $part['name'],
'type' => (string) $part['type'],
);
}
if (isset($args) && is_array($args)) {
return $args;
} else {
return null;
}
} else {
return null;
}
}
/**
* Get Function return variable
*
* @param string $operation_name Name of the <operation> element to find
* @return string|false Returns variable name if found, or false
*/
private static function getOperationOutput($operation_name)
{
$operation = self::$xml->xpath('/zfwsdl:definitions[1]/zfwsdl:portType/zfwsdl:operation[@name="' .$operation_name. '"]');
if (isset($operation[0]->output)) {
$output_message_name = $operation[0]->output['message'];
$output_message_name = explode(':', $output_message_name);
$output_message_name = $output_message_name[1];
$output_message = self::$xml->xpath('/zfwsdl:definitions[1]/zfwsdl:message[@name="' .$output_message_name. '"]');
}
if ($output_message != null) {
return array(
'name' => (string) $output_message[0]->part['name'],
'type' => (string) $output_message[0]->part['type']
);
} else {
return null;
}
}
/**
* Get Function Documentation
*
* @param string $operation_name Name of the <operation> element to find
* @return string
*/
private static function getDocs($operation_name)
{
$portType = self::$xml->xpath('//zfwsdl:operation[@name="' .$operation_name. '"]/zfwsdl:documentation');
if (isset($portType) && is_array($portType) && (sizeof($portType) >= 1)) {
return trim((string) $portType[0]);
} else {
return null;
}
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* Zend_Soap_Wsdl_Parser_Result
*
* @category Zend
* @package Zend_Soap
*/
class Zend_Soap_Wsdl_Parser_Result {
public $wsdl_file = '';
public $name;
public $documentation;
public $operations;
public $portType;
public $binding;
public $service;
public $targetNamespace;
public function __construct($wsdl)
{
$this->wsdl_file = $wsdl;
}
}

View file

@ -0,0 +1,27 @@
<?php
abstract class Zend_Soap_Wsdl_Strategy_Abstract implements Zend_Soap_Wsdl_Strategy_Interface
{
protected $_context;
/**
* Set the Zend_Soap_Wsdl Context object this strategy resides in.
*
* @param Zend_Soap_Wsdl $context
* @return void
*/
public function setContext(Zend_Soap_Wsdl $context)
{
$this->_context = $context;
}
/**
* Return the current Zend_Soap_Wsdl context object
*
* @return Zend_Soap_Wsdl
*/
public function getContext()
{
return $this->_context;
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @subpackage Wsdl
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
class Zend_Soap_Wsdl_Strategy_AnyType implements Zend_Soap_Wsdl_Strategy_Interface
{
/**
* Not needed in this strategy.
*
* @param Zend_Soap_Wsdl $context
*/
public function setContext(Zend_Soap_Wsdl $context)
{
}
/**
* Returns xsd:anyType regardless of the input.
*
* @param string $type
* @return string
*/
public function addComplexType($type)
{
return 'xsd:anyType';
}
}

View file

@ -0,0 +1,125 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @subpackage Wsdl
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
require_once "Zend/Soap/Wsdl/Strategy/DefaultComplexType.php";
class Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex extends Zend_Soap_Wsdl_Strategy_DefaultComplexType
{
/**
* Add an ArrayOfType based on the xsd:complexType syntax if type[] is detected in return value doc comment.
*
* @param string $type
* @return string tns:xsd-type
*/
public function addComplexType($type)
{
$nestingLevel = $this->_getNestedCount($type);
if($nestingLevel > 1) {
require_once "Zend/Soap/Wsdl/Exception.php";
throw new Zend_Soap_Wsdl_Exception(
"ArrayOfTypeComplex cannot return nested ArrayOfObject deeper than ".
"one level. Use array object properties to return deep nested data.
");
}
$singularType = $this->_getSingularPhpType($type);
if(!class_exists($singularType)) {
require_once "Zend/Soap/Wsdl/Exception.php";
throw new Zend_Soap_Wsdl_Exception(sprintf(
"Cannot add a complex type %s that is not an object or where ".
"class could not be found in 'DefaultComplexType' strategy.", $type
));
}
if($nestingLevel == 1) {
// The following blocks define the Array of Object structure
$xsdComplexTypeName = $this->_addArrayOfComplexType($singularType, $type);
} else {
$xsdComplexTypeName = $singularType;
}
// The array for the objects has been created, now build the object definition:
if(!in_array($singularType, $this->getContext()->getTypes())) {
parent::addComplexType($singularType);
}
return "tns:".$xsdComplexTypeName;
}
protected function _addArrayOfComplexType($singularType, $type)
{
$dom = $this->getContext()->toDomDocument();
$xsdComplexTypeName = $this->_getXsdComplexTypeName($singularType);
if(!in_array($xsdComplexTypeName, $this->getContext()->getTypes())) {
$complexType = $dom->createElement('xsd:complexType');
$complexType->setAttribute('name', $xsdComplexTypeName);
$complexContent = $dom->createElement("xsd:complexContent");
$complexType->appendChild($complexContent);
$xsdRestriction = $dom->createElement("xsd:restriction");
$xsdRestriction->setAttribute('base', 'soap-enc:Array');
$complexContent->appendChild($xsdRestriction);
$xsdAttribute = $dom->createElement("xsd:attribute");
$xsdAttribute->setAttribute("ref", "soap-enc:arrayType");
$xsdAttribute->setAttribute("wsdl:arrayType", sprintf("tns:%s[]", $singularType));
$xsdRestriction->appendChild($xsdAttribute);
$this->getContext()->getSchema()->appendChild($complexType);
$this->getContext()->addType($xsdComplexTypeName);
}
return $xsdComplexTypeName;
}
protected function _getXsdComplexTypeName($type)
{
return sprintf('ArrayOf%s', $type);
}
/**
* From a nested defintion with type[], get the singular PHP Type
*
* @param string $type
* @return string
*/
protected function _getSingularPhpType($type)
{
return str_replace("[]", "", $type);
}
/**
* Return the array nesting level based on the type name
*
* @param string $type
* @return integer
*/
protected function _getNestedCount($type)
{
return substr_count($type, "[]");
}
}

View file

@ -0,0 +1,148 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strategy_Abstract
{
/**
* Add an unbounded ArrayOfType based on the xsd:sequence syntax if type[] is detected in return value doc comment.
*
* @param string $type
* @return string tns:xsd-type
*/
public function addComplexType($type)
{
$nestedCounter = $this->_getNestedCount($type);
if($nestedCounter > 0) {
$singularType = $this->_getSingularType($type);
for($i = 1; $i <= $nestedCounter; $i++) {
$complexTypeName = $this->_getTypeNameBasedOnNestingLevel($singularType, $i);
$childTypeName = $this->_getTypeNameBasedOnNestingLevel($singularType, $i-1);
$this->_addElementFromWsdlAndChildTypes($complexTypeName, $childTypeName);
}
// adding the PHP type which is resolved to a nested XSD type. therefore add only once.
$this->getContext()->addType($complexTypeName);
return "tns:$complexTypeName";
} else {
require_once "Zend/Soap/Wsdl/Exception.php";
throw new Zend_Soap_Wsdl_Exception(sprintf(
'ArrayOfTypeSequence Strategy does not allow for complex types that are not in @return type[] syntax. "%s" type was specified.', $type
));
}
}
/**
* Return the ArrayOf or simple type name based on the singular xsdtype and the nesting level
*
* @param string $singularType
* @param int $level
* @return string
*/
protected function _getTypeNameBasedOnNestingLevel($singularType, $level)
{
if($level == 0) {
// This is not an Array anymore, return the xsd simple type
return $singularType;
} else {
$prefix = str_repeat("ArrayOf", $level);
$xsdType = $this->_getStrippedXsdType($singularType);
$arrayType = $prefix.$xsdType;
return $arrayType;
}
}
/**
* Strip the xsd: from a singularType and Format it nice for ArrayOf<Type> naming
*
* @param string $singularType
* @return string
*/
protected function _getStrippedXsdType($singularType)
{
return ucfirst(substr(strtolower($singularType), 4));
}
/**
* From a nested defintion with type[], get the singular xsd:type
*
* @throws Zend_Soap_Wsdl_Exception When no xsd:simpletype can be detected.
* @param string $type
* @return string
*/
protected function _getSingularType($type)
{
$singulartype = $this->getContext()->getType(str_replace("[]", "", $type));
if(substr($singulartype, 0, 4) != "xsd:") {
require_once "Zend/Soap/Wsdl/Exception.php";
throw new Zend_Soap_Wsdl_Exception(sprintf(
'ArrayOfTypeSequence Strategy works only with arrays of simple types like int, string, boolean, not with "%s".'.
'You may use Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex for more complex types.', $type
));
}
return $singulartype;
}
/**
* Return the array nesting level based on the type name
*
* @param string $type
* @return integer
*/
protected function _getNestedCount($type)
{
return substr_count($type, "[]");
}
/**
* Append the complex type definition to the WSDL via the context access
*
* @param string $arrayType
* @param string $childTypeName
* @return void
*/
protected function _addElementFromWsdlAndChildTypes($arrayType, $childTypeName)
{
if (!in_array($arrayType, $this->getContext()->getTypes())) {
$dom = $this->getContext()->toDomDocument();
$complexType = $dom->createElement('xsd:complexType');
$complexType->setAttribute('name', $arrayType);
$sequence = $dom->createElement('xsd:sequence');
$element = $dom->createElement('xsd:element');
$element->setAttribute('name', 'item');
$element->setAttribute('type', $childTypeName);
$element->setAttribute('minOccurs', 0);
$element->setAttribute('maxOccurs', 'unbounded');
$sequence->appendChild($element);
$complexType->appendChild($sequence);
$this->getContext()->getSchema()->appendChild($complexType);
$this->getContext()->addType($arrayType);
}
}
}

View file

@ -0,0 +1,69 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @subpackage Wsdl
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
class Zend_Soap_Wsdl_Strategy_DefaultComplexType extends Zend_Soap_Wsdl_Strategy_Abstract
{
/**
* Add a complex type by recursivly using all the class properties fetched via Reflection.
*
* @param string $type Name of the class to be specified
* @return string XSD Type for the given PHP type
*/
public function addComplexType($type)
{
if(!class_exists($type)) {
require_once "Zend/Soap/Wsdl/Exception.php";
throw new Zend_Soap_Wsdl_Exception(sprintf(
"Cannot add a complex type %s that is not an object or where ".
"class could not be found in 'DefaultComplexType' strategy.", $type
));
}
$dom = $this->getContext()->toDomDocument();
$class = new ReflectionClass($type);
$complexType = $dom->createElement('xsd:complexType');
$complexType->setAttribute('name', $type);
$all = $dom->createElement('xsd:all');
foreach ($class->getProperties() as $property) {
if (preg_match_all('/@var\s+([^\s]+)/m', $property->getDocComment(), $matches)) {
/**
* @todo check if 'xsd:element' must be used here (it may not be compatible with using 'complexType'
* node for describing other classes used as attribute types for current class
*/
$element = $dom->createElement('xsd:element');
$element->setAttribute('name', $property->getName());
$element->setAttribute('type', $this->getContext()->getType(trim($matches[1][0])));
$all->appendChild($element);
}
}
$complexType->appendChild($all);
$this->getContext()->getSchema()->appendChild($complexType);
$this->getContext()->addType($type);
return "tns:$type";
}
}

View file

@ -0,0 +1,35 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Soap
* @subpackage Wsdl
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
interface Zend_Soap_Wsdl_Strategy_Interface
{
public function setContext(Zend_Soap_Wsdl $context);
/**
* Create a complex type based on a strategy
*
* @param string $type
* @return string XSD type
*/
public function addComplexType($type);
}