Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

126 řádky
6.4 KiB

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