SoapDiscovery.class.php 源碼如下: ...
SoapDiscovery.class.php 源碼如下:
============================================================
1 <?php 2 3 /** 4 * Copyright (c) 2005, Braulio Jos?Solano Rojas 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without modification, are 8 * permitted provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, this list of 11 * conditions and the following disclaimer. 12 * Redistributions in binary form must reproduce the above copyright notice, this list of 13 * conditions and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may 16 * be used to endorse or promote products derived from this software without specific 17 * prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 20 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * 34 * @version $Id$ 35 * @copyright 2005 36 */ 37 38 /** 39 * SoapDiscovery Class that provides Web Service Definition Language (WSDL). 40 * 41 * @package SoapDiscovery 42 * @author Braulio Jos?Solano Rojas 43 * @copyright Copyright (c) 2005 Braulio Jos?Solano Rojas 44 * @version $Id$ 45 * @access public 46 **/ 47 class SoapDiscovery { 48 private $class_name = ''; 49 private $service_name = ''; 50 51 /** 52 * SoapDiscovery::__construct() SoapDiscovery class Constructor. 53 * 54 * @param string $class_name 55 * @param string $service_name 56 **/ 57 public function __construct($class_name = '', $service_name = '') { 58 $this->class_name = $class_name; 59 $this->service_name = $service_name; 60 } 61 62 /** 63 * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable. 64 * 65 * @return string 66 **/ 67 public function getWSDL() { 68 if (empty($this->service_name)) { 69 throw new Exception('No service name.'); 70 } 71 $headerWSDL = "<?xml version=\"1.0\" ?>\n"; 72 $headerWSDL.= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n"; 73 $headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n"; 74 75 if (empty($this->class_name)) { 76 throw new Exception('No class name.'); 77 } 78 79 $class = new ReflectionClass($this->class_name); 80 81 if (!$class->isInstantiable()) { 82 throw new Exception('Class is not instantiable.'); 83 } 84 85 $methods = $class->getMethods(); 86 87 $portTypeWSDL = '<portType name="'.$this->service_name.'Port">'; 88 $bindingWSDL = '<binding name="'.$this->service_name.'Binding" type="tns:'.$this->service_name."Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n"; 89 $serviceWSDL = '<service name="'.$this->service_name."\">\n<documentation />\n<port name=\"".$this->service_name.'Port" binding="tns:'.$this->service_name."Binding\"><soap:address location=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."\" />\n</port>\n</service>\n"; 90 $messageWSDL = ''; 91 foreach ($methods as $method) { 92 if ($method->isPublic() && !$method->isConstructor()) { 93 $portTypeWSDL.= '<operation name="'.$method->getName()."\">\n".'<input message="tns:'.$method->getName()."Request\" />\n<output message=\"tns:".$method->getName()."Response\" />\n</operation>\n"; 94 $bindingWSDL.= '<operation name="'.$method->getName()."\">\n".'<soap:operation soapAction="urn:'.$this->service_name.'#'.$this->class_name.'#'.$method->getName()."\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n"; 95 $messageWSDL.= '<message name="'.$method->getName()."Request\">\n"; 96 $parameters = $method->getParameters(); 97 foreach ($parameters as $parameter) { 98 $messageWSDL.= '<part name="'.$parameter->getName()."\" type=\"xsd:string\" />\n"; 99 } 100 $messageWSDL.= "</message>\n"; 101 $messageWSDL.= '<message name="'.$method->getName()."Response\">\n"; 102 $messageWSDL.= '<part name="'.$method->getName()."\" type=\"xsd:string\" />\n"; 103 $messageWSDL.= "</message>\n"; 104 } 105 } 106 $portTypeWSDL.= "</portType>\n"; 107 $bindingWSDL.= "</binding>\n"; 108 //return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'); 109 110 $fso = fopen($this->class_name . ".wsdl", "w"); 111 fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>')); 112 } 113 114 /** 115 * SoapDiscovery::getDiscovery() Returns discovery of WSDL. 116 * 117 * @return string 118 **/ 119 public function getDiscovery() { 120 return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."?wsdl\" />\n</disco:discovery>"; 121 } 122 } 123 124 ?>