From 6f7fdef0f500cd4bb540affd3bc1482243f337c1 Mon Sep 17 00:00:00 2001 From: emkael Date: Wed, 24 Feb 2016 23:18:07 +0100 Subject: * Prado 3.3.0 --- lib/prado/framework/3rdParty/WsdlGen/Wsdl.php | 286 +++++++++++++++++++ .../framework/3rdParty/WsdlGen/WsdlGenerator.php | 317 +++++++++++++++++++++ .../framework/3rdParty/WsdlGen/WsdlMessage.php | 80 ++++++ .../framework/3rdParty/WsdlGen/WsdlOperation.php | 135 +++++++++ 4 files changed, 818 insertions(+) create mode 100644 lib/prado/framework/3rdParty/WsdlGen/Wsdl.php create mode 100644 lib/prado/framework/3rdParty/WsdlGen/WsdlGenerator.php create mode 100644 lib/prado/framework/3rdParty/WsdlGen/WsdlMessage.php create mode 100644 lib/prado/framework/3rdParty/WsdlGen/WsdlOperation.php (limited to 'lib/prado/framework/3rdParty/WsdlGen') diff --git a/lib/prado/framework/3rdParty/WsdlGen/Wsdl.php b/lib/prado/framework/3rdParty/WsdlGen/Wsdl.php new file mode 100644 index 0000000..7344812 --- /dev/null +++ b/lib/prado/framework/3rdParty/WsdlGen/Wsdl.php @@ -0,0 +1,286 @@ + + * @author Wei Zhuo + * @package System.Web.Services.SOAP + */ + +/** + * Contains the dom object used to build up the wsdl. The + * operations generated by the generator are stored in here until the getWsdl() + * method is called which builds and returns the generated XML string. + * @author Marcus Nyeholt + * @author Wei Zhuo + * @version $Revision$ + */ +class Wsdl +{ + /** + * The name of the service (usually the classname) + * @var string + */ + private $serviceName; + + /** + * The URI to find the service at. If empty, the current + * uri will be used (minus any query string) + */ + private $serviceUri; + + /** + * The complex types declarations + * @var ArrayObject + */ + private $types; + + + /** + * A collection of SOAP operations + * @var array + */ + private $operations=array(); + + /** + * Wsdl DOMDocument that's generated. + */ + private $wsdl = null; + + /** + * The definitions created for the WSDL + */ + private $definitions = null; + + /** + * The target namespace variable? + */ + private $targetNamespace =''; + + /** + * The binding style (default at the moment) + */ + private $bindingStyle = 'rpc'; + + /** + * The binding uri + */ + private $bindingTransport = 'http://schemas.xmlsoap.org/soap/http'; + + private $_encoding=''; + + private static $_primitiveTypes = array('string', 'int', 'float', 'boolean', 'date', 'time', 'dateTime'); + + /** + * Creates a new Wsdl thing + * @param string $name the name of the service. + * @param string $serviceUri The URI of the service that handles this WSDL + * @param string character encoding + */ + public function __construct($name, $serviceUri='', $encoding='') + { + $this->_encoding = $encoding; + $this->serviceName = $name; + $protocol=(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!=='off'))?'https://':'http://'; + if ($serviceUri === '') $serviceUri = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; + $this->serviceUri = str_replace('&', '&', $serviceUri); + $this->types = new ArrayObject(); + $this->targetNamespace = 'urn:'.$name.'wsdl'; + } + + public function getWsdl() + { + $this->buildWsdl(); + return $this->wsdl; + } + + /** + * Generates the WSDL file into the $this->wsdl variable + */ + protected function buildWsdl() + { + $encoding = $this->_encoding==='' ? '' : 'encoding="'.$this->_encoding.'"'; + + $xml = ' + '; + + $dom = new DOMDocument(); + $dom->loadXml($xml); + $this->definitions = $dom->documentElement; + + $this->addTypes($dom); + + $this->addMessages($dom); + $this->addPortTypes($dom); + $this->addBindings($dom); + $this->addService($dom); + + $this->wsdl = $dom->saveXML(); + } + + /** + * Adds complexType definitions to the document + * @param DomDocument $dom The document to add to + */ + public function addTypes(DomDocument $dom) + { + if (!count($this->types)) return; + $types = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:types'); + $schema = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema'); + $schema->setAttribute('targetNamespace', $this->targetNamespace); + foreach($this->types as $type => $elements) + { + $complexType = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType'); + $complexType->setAttribute('name', $type); + if(substr($type, strlen($type) - 5, 5) == 'Array') // if it's an array + { + $sequence = $dom->createElement("xsd:sequence"); + + $singularType = substr($type, 0, strlen($type) - 5); + $e = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element'); + $e->setAttribute('name', $singularType); + $e->setAttribute('type', sprintf('tns:%s',$singularType)); + $e->setAttribute('minOccurs','0'); + $e->setAttribute('maxOccurs','unbounded'); + $sequence->appendChild($e); + $complexType->appendChild($sequence); + } + else + { + $all = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:all'); + foreach($elements as $elem) + { + $e = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element'); + $e->setAttribute('name', $elem['name']); + $e->setAttribute('type', $elem['type']); + if($elem['minOc']!==false) + $e->setAttribute('minOccurs',$elem['minOc']); + if($elem['maxOc']!==false) + $e->setAttribute('maxOccurs',$elem['maxOc']); + if($elem['nil']!==false) + $e->setAttribute('nillable',$elem['nil']); + $all->appendChild($e); + } + $complexType->appendChild($all); + } + $schema->appendChild($complexType); + $types->appendChild($schema); + } + + $this->definitions->appendChild($types); + } + + /** + * @return string prefix 'xsd:' for primitive array types, otherwise, 'tns:' + */ + protected function getArrayTypePrefix($type) + { + $elementType = substr($type, 0, strlen($type) - 5); + return in_array($elementType, self::$_primitiveTypes) ? 'xsd:' : 'tns:'; + } + + /** + * Add messages for the service + * @param DomDocument $dom The document to add to + */ + protected function addMessages(DomDocument $dom) + { + foreach ($this->operations as $operation) { + $operation->setMessageElements($this->definitions, $dom); + } + } + + /** + * Add the port types for the service + * @param DomDocument $dom The document to add to + */ + protected function addPortTypes(DOMDocument $dom) + { + $portType = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:portType'); + $portType->setAttribute('name', $this->serviceName.'PortType'); + + $this->definitions->appendChild($portType); + foreach ($this->operations as $operation) { + $portOperation = $operation->getPortOperation($dom); + $portType->appendChild($portOperation); + } + } + + /** + * Add the bindings for the service + * @param DomDocument $dom The document to add to + */ + protected function addBindings(DOMDocument $dom) + { + $binding = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:binding'); + $binding->setAttribute('name', $this->serviceName.'Binding'); + $binding->setAttribute('type', 'tns:'.$this->serviceName.'PortType'); + + $soapBinding = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/soap/', 'soap:binding'); + $soapBinding->setAttribute('style', $this->bindingStyle); + $soapBinding->setAttribute('transport', $this->bindingTransport); + $binding->appendChild($soapBinding); + + $this->definitions->appendChild($binding); + + foreach ($this->operations as $operation) { + $bindingOperation = $operation->getBindingOperation($dom, $this->targetNamespace, $this->bindingStyle); + $binding->appendChild($bindingOperation); + } + } + + /** + * Add the service definition + * @param DomDocument $dom The document to add to + */ + protected function addService(DomDocument $dom) + { + $service = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:service'); + $service->setAttribute('name', $this->serviceName.'Service'); + + $port = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:port'); + $port->setAttribute('name', $this->serviceName.'Port'); + $port->setAttribute('binding', 'tns:'.$this->serviceName.'Binding'); + + $soapAddress = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/soap/', 'soap:address'); + $soapAddress->setAttribute('location', $this->serviceUri); + $port->appendChild($soapAddress); + + $service->appendChild($port); + + $this->definitions->appendChild($service); + } + + /** + * Adds an operation to have port types and bindings output + * @param WsdlOperation $operation The operation to add + */ + public function addOperation(WsdlOperation $operation) + { + $this->operations[] = $operation; + } + + /** + * Adds complexTypes to the wsdl + * @param string $type Name of the type + * @param Array $elements Elements of the type (each one is an associative array('name','type')) + */ + public function addComplexType($type, $elements) + { + $this->types[$type] = $elements; + } +} diff --git a/lib/prado/framework/3rdParty/WsdlGen/WsdlGenerator.php b/lib/prado/framework/3rdParty/WsdlGen/WsdlGenerator.php new file mode 100644 index 0000000..e4624ef --- /dev/null +++ b/lib/prado/framework/3rdParty/WsdlGen/WsdlGenerator.php @@ -0,0 +1,317 @@ + + * @package System.Web.Services.SOAP + */ + +require_once(dirname(__FILE__).'/Wsdl.php'); +require_once(dirname(__FILE__).'/WsdlMessage.php'); +require_once(dirname(__FILE__).'/WsdlOperation.php'); + +/** + * Generator for the wsdl. + * Special thanks to Cristian Losada for implementing the Complex Types section of the WSDL. + * @author Marcus Nyeholt + * @author Cristian Losada + * @version $Revision$ + */ +class WsdlGenerator +{ + /** + * The instance. + * var WsdlGenerator + */ + private static $instance; + + /** + * The name of this service (the classname) + * @var string + */ + private $serviceName = ''; + + /** + * The complex types to use in the wsdl + * @var Array + */ + private $types = array(); + + /** + * The operations available in this wsdl + * @var ArrayObject + */ + private $operations; + + /** + * The wsdl object. + * @var object + */ + private $wsdlDocument; + + /** + * The actual wsdl string + * @var string + */ + private $wsdl = ''; + + /** + * The singleton instance for the generator + */ + public static function getInstance() + { + if (is_null(self::$instance)) { + self::$instance = new WsdlGenerator(); + } + return self::$instance; + } + + /** + * Get the Wsdl generated + * @return string The Wsdl for this wsdl + */ + public function getWsdl() + { + return $this->wsdl; + } + + /** + * Generates WSDL for a passed in class, and saves it in the current object. The + * WSDL can then be retrieved by calling + * @param string $className The name of the class to generate for + * @param string $serviceUri The URI of the service that handles this WSDL + * @param string $encoding character encoding. + * @return void + */ + public function generateWsdl($className, $serviceUri='',$encoding='') + { + $this->wsdlDocument = new Wsdl($className, $serviceUri, $encoding); + + $classReflect = new ReflectionClass($className); + $methods = $classReflect->getMethods(); + + foreach ($methods as $method) { + // Only process public methods + if ($method->isPublic()) { + $this->processMethod($method); + } + } + + foreach($this->types as $type => $elements) { + $this->wsdlDocument->addComplexType($type, $elements); + } + + $this->wsdl = $this->wsdlDocument->getWsdl(); + } + + /** + * Static method that generates and outputs the generated wsdl + * @param string $className The name of the class to export + * @param string $serviceUri The URI of the service that handles this WSDL + * @param string $encoding character encoding. + */ + public static function generate($className, $serviceUri='', $encoding='') + { + $generator = WsdlGenerator::getInstance(); + $generator->generateWsdl($className, $serviceUri,$encoding); + //header('Content-type: text/xml'); + return $generator->getWsdl(); + //exit(); + + } + + /** + * Process a method found in the passed in class. + * @param ReflectionMethod $method The method to process + */ + protected function processMethod(ReflectionMethod $method) + { + $comment = $method->getDocComment(); + if (strpos($comment, '@soapmethod') === false) { + return; + } + $comment = preg_replace("/(^[\\s]*\\/\\*\\*) + |(^[\\s]\\*\\/) + |(^[\\s]*\\*?\\s) + |(^[\\s]*) + |(^[\\t]*)/ixm", "", $comment); + + $comment = str_replace("\r", "", $comment); + $comment = preg_replace("/([\\t])+/", "\t", $comment); + $commentLines = explode("\n", $comment); + + $methodDoc = ''; + $params = array(); + $return = array(); + $gotDesc = false; + $gotParams = false; + + foreach ($commentLines as $line) { + if ($line == '') continue; + if ($line{0} == '@') { + $gotDesc = true; + if (preg_match('/^@param\s+([\w\[\]()]+)\s+\$([\w()]+)\s*(.*)/i', $line, $match)) { + $param = array(); + $param['type'] = $this->convertType($match[1]); + $param['name'] = $match[2]; + $param['desc'] = $match[3]; + $params[] = $param; + } + else if (preg_match('/^@return\s+([\w\[\]()]+)\s*(.*)/i', $line, $match)) { + $gotParams = true; + $return['type'] = $this->convertType($match[1]); + $return['desc'] = $match[2]; + $return['name'] = 'return'; + } + } + else { + if (!$gotDesc) { + $methodDoc .= trim($line); + } + else if (!$gotParams) { + $params[count($params)-1]['desc'] .= trim($line); + } + else { + if ($line == '*/') continue; + $return['desc'] .= trim($line); + } + } + } + + $methodName = $method->getName(); + $operation = new WsdlOperation($methodName, $methodDoc); + + $operation->setInputMessage(new WsdlMessage($methodName.'Request', $params)); + $operation->setOutputMessage(new WsdlMessage($methodName.'Response', array($return))); + + $this->wsdlDocument->addOperation($operation); + + } + + /** + * Converts from a PHP type into a WSDL type. This is borrowed from + * Cerebral Cortex (let me know and I'll remove asap). + * + * TODO: date and dateTime + * @param string $type The php type to convert + * @return string The XSD type. + */ + private function convertType($type) + { + switch ($type) { + case 'string': + case 'str': + return 'xsd:string'; + break; + case 'int': + case 'integer': + return 'xsd:int'; + break; + case 'float': + case 'double': + return 'xsd:float'; + break; + case 'boolean': + case 'bool': + return 'xsd:boolean'; + break; + case 'date': + return 'xsd:date'; + break; + case 'time': + return 'xsd:time'; + break; + case 'dateTime': + return 'xsd:dateTime'; + break; + case 'array': + return 'soap-enc:Array'; + break; + case 'object': + return 'xsd:struct'; + break; + case 'mixed': + return 'xsd:anyType'; + break; + case 'void': + return ''; + default: + if(strpos($type, '[]')) // if it is an array + { + $className = substr($type, 0, strlen($type) - 2); + $type = $className . 'Array'; + $this->types[$type] = ''; + $this->convertType($className); + } + else + { + if(!isset($this->types[$type])) + $this->extractClassProperties($type); + } + return 'tns:' . $type; + } + } + + /** + * Extract the type and the name of all properties of the $className class and saves it in the $types array + * This method extract properties from PHPDoc formatted comments for variables. Unfortunately the reflectionproperty + * class doesn't have a getDocComment method to extract comments about it, so we have to extract the information + * about the variables manually. Thanks heaps to Cristian Losada for implementing this. + * @param string $className The name of the class + */ + private function extractClassProperties($className) + { + /** + * modified by Qiang Xue, Jan. 2, 2007 + * Using Reflection's DocComment to obtain property definitions + * DocComment is available since PHP 5.1 + */ + $reflection = new ReflectionClass($className); + $properties = $reflection->getProperties(); + foreach($properties as $property) + { + $comment = $property->getDocComment(); + if(strpos($comment, '@soapproperty') !== false) + { + if(preg_match('/@var\s+([\w\.]+(\[\s*\])?)\s*?\$(.*)$/mi',$comment,$matches)) + { + // support nillable, minOccurs, maxOccurs attributes + $nillable=$minOccurs=$maxOccurs=false; + if(preg_match('/{(.+)}/',$matches[3],$attr)) + { + $matches[3]=str_replace($attr[0],'',$matches[3]); + if(preg_match_all('/((\w+)\s*=\s*(\w+))/mi',$attr[1],$attr)) + { + foreach($attr[2] as $id=>$prop) + { + if(strcasecmp($prop,'nillable')===0) + $nillable=$attr[3][$id] ? 'true' : 'false'; + elseif(strcasecmp($prop,'minOccurs')===0) + $minOccurs=(int)$attr[3][$id]; + elseif(strcasecmp($prop,'maxOccurs')===0) + $maxOccurs=(int)$attr[3][$id]; + } + } + } + + $param = array(); + $param['type'] = $this->convertType($matches[1]); + $param['name'] = trim($matches[3]); + $param['nil'] = $nillable; + $param['minOc'] = $minOccurs; + $param['maxOc'] = $maxOccurs; + $this->types[$className][] = $param; + + } + } + } + } +} diff --git a/lib/prado/framework/3rdParty/WsdlGen/WsdlMessage.php b/lib/prado/framework/3rdParty/WsdlGen/WsdlMessage.php new file mode 100644 index 0000000..3597c97 --- /dev/null +++ b/lib/prado/framework/3rdParty/WsdlGen/WsdlMessage.php @@ -0,0 +1,80 @@ + + * @author Wei Zhuo + * @package System.Web.Services.SOAP + */ + +/** + * Represents a WSDL message. This is bound to the portTypes + * for this service + * @author Marcus Nyeholt + * @author Wei Zhuo + * @version $Revision$ + */ +class WsdlMessage +{ + /** + * The name of this message + * @var string + */ + private $name; + + /** + * Represents the parameters for this message + * @var array + */ + private $parts; + + /** + * Creates a new message + * @param string $messageName The name of the message + * @param string $parts The parts of this message + */ + public function __construct($messageName, $parts) + { + $this->name = $messageName; + $this->parts = $parts; + + } + + /** + * Gets the name of this message + * @return string The name + */ + public function getName() + { + return $this->name; + } + + /** + * Return the message as a DOM element + * @param DOMDocument $wsdl The wsdl document the messages will be children of + */ + public function getMessageElement(DOMDocument $dom) + { + $message = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:message'); + $message->setAttribute('name', $this->name); + + foreach ($this->parts as $part) { + if (isset($part['name'])) { + $partElement = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:part'); + $partElement->setAttribute('name', $part['name']); + $partElement->setAttribute('type', $part['type']); + $message->appendChild($partElement); + } + } + + return $message; + } +} diff --git a/lib/prado/framework/3rdParty/WsdlGen/WsdlOperation.php b/lib/prado/framework/3rdParty/WsdlGen/WsdlOperation.php new file mode 100644 index 0000000..91adbda --- /dev/null +++ b/lib/prado/framework/3rdParty/WsdlGen/WsdlOperation.php @@ -0,0 +1,135 @@ + + * @author Wei Zhuo + * @package System.Web.Services.SOAP + */ + +/** + * Represents a WSDL Operation. This is exported for the portTypes and bindings + * section of the soap service + * @author Marcus Nyeholt + * @author Wei Zhuo + * @version $Revision$ + */ +class WsdlOperation +{ + /** + * The name of the operation + */ + private $operationName; + + /** + * Documentation for the operation + */ + private $documentation; + + /** + * The input wsdl message + */ + private $inputMessage; + + /** + * The output wsdl message + */ + private $outputMessage; + + public function __construct($name, $doc='') + { + $this->operationName = $name; + $this->documentation = $doc; + } + + public function setInputMessage(WsdlMessage $msg) + { + $this->inputMessage = $msg; + } + + public function setOutputMessage(WsdlMessage $msg) + { + $this->outputMessage = $msg; + } + + /** + * Sets the message elements for this operation into the wsdl document + * @param DOMElement $wsdl The parent domelement for the messages + * @param DomDocument $dom The dom document to create the messages as children of + */ + public function setMessageElements(DOMElement $wsdl, DOMDocument $dom) + { + + $input = $this->inputMessage->getMessageElement($dom); + $output = $this->outputMessage->getMessageElement($dom); + + $wsdl->appendChild($input); + $wsdl->appendChild($output); + } + + /** + * Get the port operations for this operation + * @param DomDocument $dom The dom document to create the messages as children of + * @return DomElement The dom element representing this port. + */ + public function getPortOperation(DomDocument $dom) + { + $operation = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:operation'); + $operation->setAttribute('name', $this->operationName); + + $documentation = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:documentation', htmlentities($this->documentation)); + $input = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:input'); + $input->setAttribute('message', 'tns:'.$this->inputMessage->getName()); + $output = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:output'); + $output->setAttribute('message', 'tns:'.$this->outputMessage->getName()); + + $operation->appendChild($documentation); + $operation->appendChild($input); + $operation->appendChild($output); + + return $operation; + } + + /** + * Build the binding operations. + * TODO: Still quite incomplete with all the things being stuck in, I don't understand + * a lot of it, and it's mostly copied from the output of nusoap's wsdl output. + * @param DomDocument $dom The dom document to create the binding as children of + * @param string $namespace The namespace this binding is in. + * @return DomElement The dom element representing this binding. + */ + public function getBindingOperation(DomDocument $dom, $namespace, $style='rpc') + { + $operation = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:operation'); + $operation->setAttribute('name', $this->operationName); + + $soapOperation = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/soap/', 'soap:operation'); + $method = $this->operationName; + $soapOperation->setAttribute('soapAction', $namespace.'#'.$method); + $soapOperation->setAttribute('style', $style); + + $input = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:input'); + $output = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/', 'wsdl:output'); + + $soapBody = $dom->createElementNS('http://schemas.xmlsoap.org/wsdl/soap/', 'soap:body'); + $soapBody->setAttribute('use', 'encoded'); + $soapBody->setAttribute('namespace', $namespace); + $soapBody->setAttribute('encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/'); + $input->appendChild($soapBody); + $output->appendChild(clone $soapBody); + + $operation->appendChild($soapOperation); + $operation->appendChild($input); + $operation->appendChild($output); + + return $operation; + } +} -- cgit v1.2.3