Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Soap/Wsdl.php
Go to the documentation of this file.
00001 <?php
00025 require_once "Zend/Soap/Wsdl/Strategy/Interface.php";
00026 
00030 require_once "Zend/Soap/Wsdl/Strategy/Abstract.php";
00031 
00038 class Zend_Soap_Wsdl
00039 {
00043     private $_dom;
00044 
00048     private $_wsdl;
00049 
00053     private $_uri;
00054 
00058     private $_schema = null;
00059 
00065     private $_includedTypes = array();
00066 
00070     protected $_strategy = null;
00071 
00072 
00080     public function __construct($name, $uri, $strategy = true)
00081     {
00082         if ($uri instanceof Zend_Uri_Http) {
00083             $uri = $uri->getUri();
00084         }
00085         $this->_uri = $uri;
00086 
00091         $wsdl = "<?xml version='1.0' ?>
00092                 <definitions name='$name' targetNamespace='$uri'
00093                     xmlns='http://schemas.xmlsoap.org/wsdl/'
00094                     xmlns:tns='$uri'
00095                     xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
00096                     xmlns:xsd='http://www.w3.org/2001/XMLSchema'
00097                     xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/'
00098                     xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'></definitions>";
00099         $this->_dom = new DOMDocument();
00100         if (!$this->_dom->loadXML($wsdl)) {
00101             require_once 'Zend/Server/Exception.php';
00102             throw new Zend_Server_Exception('Unable to create DomDocument');
00103         } else {
00104             $this->_wsdl = $this->_dom->documentElement;
00105         }
00106 
00107         $this->setComplexTypeStrategy($strategy);
00108     }
00109 
00116     public function setUri($uri)
00117     {
00118         if ($uri instanceof Zend_Uri_Http) {
00119             $uri = $uri->getUri();
00120         }
00121         $oldUri = $this->_uri;
00122         $this->_uri = $uri;
00123 
00124         if($this->_dom !== null) {
00125             // @todo: This is the worst hack ever, but its needed due to design and non BC issues of WSDL generation
00126             $xml = $this->_dom->saveXML();
00127             $xml = str_replace($oldUri, $uri, $xml);
00128             $this->_dom = new DOMDocument();
00129             $this->_dom->loadXML($xml);
00130         }
00131 
00132         return $this;
00133     }
00134 
00142     public function setComplexTypeStrategy($strategy)
00143     {
00144         if($strategy === true) {
00145             require_once "Zend/Soap/Wsdl/Strategy/DefaultComplexType.php";
00146             $strategy = new Zend_Soap_Wsdl_Strategy_DefaultComplexType();
00147         } else if($strategy === false) {
00148             require_once "Zend/Soap/Wsdl/Strategy/AnyType.php";
00149             $strategy = new Zend_Soap_Wsdl_Strategy_AnyType();
00150         } else if(is_string($strategy)) {
00151             if(class_exists($strategy)) {
00152                 $strategy = new $strategy();
00153             } else {
00154                 require_once "Zend/Soap/Wsdl/Exception.php";
00155                 throw new Zend_Soap_Wsdl_Exception(
00156                     sprintf("Strategy with name '%s does not exist.", $strategy
00157                 ));
00158             }
00159         }
00160 
00161         if(!($strategy instanceof Zend_Soap_Wsdl_Strategy_Interface)) {
00162             require_once "Zend/Soap/Wsdl/Exception.php";
00163             throw new Zend_Soap_Wsdl_Exception("Set a strategy that is not of type 'Zend_Soap_Wsdl_Strategy_Interface'");
00164         }
00165         $this->_strategy = $strategy;
00166         return $this;
00167     }
00168 
00174     public function getComplexTypeStrategy()
00175     {
00176         return $this->_strategy;
00177     }
00178 
00189     public function addMessage($name, $parts)
00190     {
00191         $message = $this->_dom->createElement('message');
00192 
00193         $message->setAttribute('name', $name);
00194 
00195         if (sizeof($parts) > 0) {
00196             foreach ($parts as $name => $type) {
00197                 $part = $this->_dom->createElement('part');
00198                 $part->setAttribute('name', $name);
00199                 if (is_array($type)) {
00200                     foreach ($type as $key => $value) {
00201                         $part->setAttribute($key, $value);
00202                     }
00203                 } else {
00204                     $part->setAttribute('type', $type);
00205                 }
00206                 $message->appendChild($part);
00207             }
00208         }
00209 
00210         $this->_wsdl->appendChild($message);
00211 
00212         return $message;
00213     }
00214 
00221     public function addPortType($name)
00222     {
00223         $portType = $this->_dom->createElement('portType');
00224         $portType->setAttribute('name', $name);
00225         $this->_wsdl->appendChild($portType);
00226 
00227         return $portType;
00228     }
00229 
00240     public function addPortOperation($portType, $name, $input = false, $output = false, $fault = false)
00241     {
00242         $operation = $this->_dom->createElement('operation');
00243         $operation->setAttribute('name', $name);
00244 
00245         if (is_string($input) && (strlen(trim($input)) >= 1)) {
00246             $node = $this->_dom->createElement('input');
00247             $node->setAttribute('message', $input);
00248             $operation->appendChild($node);
00249         }
00250         if (is_string($output) && (strlen(trim($output)) >= 1)) {
00251             $node= $this->_dom->createElement('output');
00252             $node->setAttribute('message', $output);
00253             $operation->appendChild($node);
00254         }
00255         if (is_string($fault) && (strlen(trim($fault)) >= 1)) {
00256             $node = $this->_dom->createElement('fault');
00257             $node->setAttribute('message', $fault);
00258             $operation->appendChild($node);
00259         }
00260 
00261         $portType->appendChild($operation);
00262 
00263         return $operation;
00264     }
00265 
00273     public function addBinding($name, $portType)
00274     {
00275         $binding = $this->_dom->createElement('binding');
00276         $binding->setAttribute('name', $name);
00277         $binding->setAttribute('type', $portType);
00278 
00279         $this->_wsdl->appendChild($binding);
00280 
00281         return $binding;
00282     }
00283 
00293     public function addBindingOperation($binding, $name, $input = false, $output = false, $fault = false)
00294     {
00295         $operation = $this->_dom->createElement('operation');
00296         $operation->setAttribute('name', $name);
00297 
00298         if (is_array($input)) {
00299             $node = $this->_dom->createElement('input');
00300             $soap_node = $this->_dom->createElement('soap:body');
00301             foreach ($input as $name => $value) {
00302                 $soap_node->setAttribute($name, $value);
00303             }
00304             $node->appendChild($soap_node);
00305             $operation->appendChild($node);
00306         }
00307 
00308         if (is_array($output)) {
00309             $node = $this->_dom->createElement('output');
00310             $soap_node = $this->_dom->createElement('soap:body');
00311             foreach ($output as $name => $value) {
00312                 $soap_node->setAttribute($name, $value);
00313             }
00314             $node->appendChild($soap_node);
00315             $operation->appendChild($node);
00316         }
00317 
00318         if (is_array($fault)) {
00319             $node = $this->_dom->createElement('fault');
00320             if (isset($fault['name'])) {
00321                 $node->setAttribute('name', $fault['name']);
00322             }
00323             $soap_node = $this->_dom->createElement('soap:body');
00324             foreach ($output as $name => $value) {
00325                 $soap_node->setAttribute($name, $value);
00326             }
00327             $node->appendChild($soap_node);
00328             $operation->appendChild($node);
00329         }
00330 
00331         $binding->appendChild($operation);
00332 
00333         return $operation;
00334     }
00335 
00344     public function addSoapBinding($binding, $style = 'document', $transport = 'http://schemas.xmlsoap.org/soap/http')
00345     {
00346         $soap_binding = $this->_dom->createElement('soap:binding');
00347         $soap_binding->setAttribute('style', $style);
00348         $soap_binding->setAttribute('transport', $transport);
00349 
00350         $binding->appendChild($soap_binding);
00351 
00352         return $soap_binding;
00353     }
00354 
00362     public function addSoapOperation($binding, $soap_action)
00363     {
00364         if ($soap_action instanceof Zend_Uri_Http) {
00365             $soap_action = $soap_action->getUri();
00366         }
00367         $soap_operation = $this->_dom->createElement('soap:operation');
00368         $soap_operation->setAttribute('soapAction', $soap_action);
00369 
00370         $binding->insertBefore($soap_operation, $binding->firstChild);
00371 
00372         return $soap_operation;
00373     }
00374 
00384     public function addService($name, $port_name, $binding, $location)
00385     {
00386         if ($location instanceof Zend_Uri_Http) {
00387             $location = $location->getUri();
00388         }
00389         $service = $this->_dom->createElement('service');
00390         $service->setAttribute('name', $name);
00391 
00392         $port = $this->_dom->createElement('port');
00393         $port->setAttribute('name', $port_name);
00394         $port->setAttribute('binding', $binding);
00395 
00396         $soap_address = $this->_dom->createElement('soap:address');
00397         $soap_address->setAttribute('location', $location);
00398 
00399         $port->appendChild($soap_address);
00400         $service->appendChild($port);
00401 
00402         $this->_wsdl->appendChild($service);
00403 
00404         return $service;
00405     }
00406 
00418     public function addDocumentation($input_node, $documentation)
00419     {
00420         if ($input_node === $this) {
00421             $node = $this->_dom->documentElement;
00422         } else {
00423             $node = $input_node;
00424         }
00425 
00426         $doc = $this->_dom->createElement('documentation');
00427         $doc_cdata = $this->_dom->createTextNode($documentation);
00428         $doc->appendChild($doc_cdata);
00429 
00430         if($node->hasChildNodes()) {
00431             $node->insertBefore($doc, $node->firstChild);
00432         } else {
00433             $node->appendChild($doc);
00434         }
00435 
00436         return $doc;
00437     }
00438 
00444     public function addTypes($types)
00445     {
00446         if ($types instanceof DomDocument) {
00447             $dom = $this->_dom->importNode($types->documentElement);
00448             $this->_wsdl->appendChild($types->documentElement);
00449         } elseif ($types instanceof DomNode || $types instanceof DomElement || $types instanceof DomDocumentFragment ) {
00450             $dom = $this->_dom->importNode($types);
00451             $this->_wsdl->appendChild($dom);
00452         }
00453     }
00454 
00461     public function addType($type)
00462     {
00463         if(!in_array($type, $this->_includedTypes)) {
00464             $this->_includedTypes[] = $type;
00465         }
00466         return $this;
00467     }
00468 
00474     public function getTypes()
00475     {
00476         return $this->_includedTypes;
00477     }
00478 
00484     public function getSchema()
00485     {
00486         if($this->_schema == null) {
00487             $this->addSchemaTypeSection();
00488         }
00489 
00490         return $this->_schema;
00491     }
00492 
00498     public function toXML()
00499     {
00500            return $this->_dom->saveXML();
00501     }
00502 
00508     public function toDomDocument()
00509     {
00510         return $this->_dom;
00511     }
00512 
00518     public function dump($filename = false)
00519     {
00520         if (!$filename) {
00521             echo $this->toXML();
00522             return true;
00523         } else {
00524             return file_put_contents($filename, $this->toXML());
00525         }
00526     }
00527 
00534     public function getType($type)
00535     {
00536         switch (strtolower($type)) {
00537             case 'string':
00538             case 'str':
00539                 return 'xsd:string';
00540                 break;
00541             case 'int':
00542             case 'integer':
00543                 return 'xsd:int';
00544                 break;
00545             case 'float':
00546             case 'double':
00547                 return 'xsd:float';
00548                 break;
00549             case 'boolean':
00550             case 'bool':
00551                 return 'xsd:boolean';
00552                 break;
00553             case 'array':
00554                 return 'soap-enc:Array';
00555                 break;
00556             case 'object':
00557                 return 'xsd:struct';
00558                 break;
00559             case 'mixed':
00560                 return 'xsd:anyType';
00561                 break;
00562             case 'void':
00563                 return '';
00564             default:
00565                 // delegate retrieval of complex type to current strategy
00566                 return $this->addComplexType($type);
00567             }
00568     }
00569 
00575     public function addSchemaTypeSection()
00576     {
00577         if ($this->_schema === null) {
00578             $this->_schema = $this->_dom->createElement('xsd:schema');
00579             $this->_schema->setAttribute('targetNamespace', $this->_uri);
00580             $types = $this->_dom->createElement('types');
00581             $types->appendChild($this->_schema);
00582             $this->_wsdl->appendChild($types);
00583         }
00584         return $this;
00585     }
00586 
00593     public function addComplexType($type)
00594     {
00595         if (in_array($type, $this->getTypes())) {
00596             return "tns:$type";
00597         }
00598         $this->addSchemaTypeSection();
00599 
00600         $strategy = $this->getComplexTypeStrategy();
00601         $strategy->setContext($this);
00602         // delegates the detection of a complex type to the current strategy
00603         return $strategy->addComplexType($type);
00604     }
00605 
00612     private function _parseElement($element)
00613     {
00614         if (!is_array($element)) {
00615             require_once "Zend/Soap/Wsdl/Exception.php";
00616             throw new Zend_Soap_Wsdl_Exception("The 'element' parameter needs to be an associative array.");
00617         }
00618 
00619         $elementXml = $this->_dom->createElement('xsd:element');
00620         foreach ($element as $key => $value) {
00621             if (in_array($key, array('sequence', 'all', 'choice'))) {
00622                 if (is_array($value)) {
00623                     $complexType = $this->_dom->createElement('xsd:complexType');
00624                     if (count($value) > 0) {
00625                         $container = $this->_dom->createElement('xsd:' . $key);
00626                         foreach ($value as $subelement) {
00627                             $subelementXml = $this->_parseElement($subelement);
00628                             $container->appendChild($subelementXml);
00629                         }
00630                         $complexType->appendChild($container);
00631                     }
00632                     $elementXml->appendChild($complexType);
00633                 }
00634             } else {
00635                 $elementXml->setAttribute($key, $value);
00636             }
00637         }
00638         return $elementXml;
00639     }
00640 
00659     public function addElement($element)
00660     {
00661         $schema = $this->getSchema();
00662         $elementXml = $this->_parseElement($element);
00663         $schema->appendChild($elementXml);
00664         return 'tns:' . $element['name'];
00665     }
00666 }
 All Data Structures Namespaces Files Functions Variables Enumerations