SoapDiscovery.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = '',$path = './') {
  55. $this->class_name = $class_name;
  56. $this->service_name = $service_name;
  57. $this->path = $path;
  58. }
  59. /**
  60. * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
  61. *
  62. * @return string
  63. * */
  64. public function getWSDL() {
  65. header("Content-type: text/html; charset=utf-8");
  66. if (empty($this->service_name)) {
  67. throw new Exception('No service name.');
  68. }
  69. $headerWSDL = "<?xml version=\"1.0\" ?>\n";
  70. $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";
  71. $headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";
  72. if (empty($this->class_name)) {
  73. throw new Exception('No class name.');
  74. }
  75. $class = new ReflectionClass($this->class_name);
  76. if (!$class->isInstantiable()) {
  77. throw new Exception('Class is not instantiable.');
  78. }
  79. $methods = $class->getMethods();
  80. $portTypeWSDL = '<portType name="' . $this->service_name . 'Port">';
  81. $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";
  82. $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";
  83. $messageWSDL = '';
  84. foreach ($methods as $method) {
  85. if ($method->isPublic() && !$method->isConstructor()) {
  86. $portTypeWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<input message="tns:' . $method->getName() . "Request\" />\n<output message=\"tns:" . $method->getName() . "Response\" />\n</operation>\n";
  87. $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";
  88. $messageWSDL.= '<message name="' . $method->getName() . "Request\">\n";
  89. $parameters = $method->getParameters();
  90. foreach ($parameters as $parameter) {
  91. $messageWSDL.= '<part name="' . $parameter->getName() . "\" type=\"xsd:string\" />\n";
  92. }
  93. $messageWSDL.= "</message>\n";
  94. $messageWSDL.= '<message name="' . $method->getName() . "Response\">\n";
  95. $messageWSDL.= '<part name="' . $method->getName() . "\" type=\"xsd:string\" />\n";
  96. $messageWSDL.= "</message>\n";
  97. }
  98. }
  99. $portTypeWSDL.= "</portType>\n";
  100. $bindingWSDL.= "</binding>\n";
  101. //return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
  102. //生成wsdl文件,将上面的return注释
  103. $this->wsdl_name = preg_replace('/(\\\.*\\\)/','',$this->class_name);
  104. $fso = fopen($this->path.$this->wsdl_name . ".wsdl", "w");
  105. fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
  106. }
  107. /**
  108. * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
  109. *
  110. * @return string
  111. * */
  112. public function getDiscovery() {
  113. 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>";
  114. }
  115. }
  116. ?>