From 2a2552b14763092dbd14a9d560dae7364c59aa20 Mon Sep 17 00:00:00 2001 From: Christian Kerl Date: Fri, 7 Jan 2011 23:03:03 +0100 Subject: [PATCH] added initial implementation of WsdlFileDumper based on Zend Wsdl class --- ServiceDefinition/Dumper/FileDumper.php | 30 +++++ ServiceDefinition/Dumper/WsdlFileDumper.php | 117 ++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 ServiceDefinition/Dumper/FileDumper.php create mode 100644 ServiceDefinition/Dumper/WsdlFileDumper.php diff --git a/ServiceDefinition/Dumper/FileDumper.php b/ServiceDefinition/Dumper/FileDumper.php new file mode 100644 index 0000000..577e8c6 --- /dev/null +++ b/ServiceDefinition/Dumper/FileDumper.php @@ -0,0 +1,30 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Bundle\WebServiceBundle\ServiceDefinition\Dumper; + +use Bundle\WebServiceBundle\ServiceDefinition\Dumper\DumperInterface; +use Bundle\WebServiceBundle\Util\Assert; + +/** + * + * @author Christian Kerl + */ +abstract class FileDumper implements DumperInterface +{ + protected $file; + + public function __construct($file) + { + Assert::thatArgumentNotNull('file', $file); + + $this->file = $file; + } +} diff --git a/ServiceDefinition/Dumper/WsdlFileDumper.php b/ServiceDefinition/Dumper/WsdlFileDumper.php new file mode 100644 index 0000000..e691cbd --- /dev/null +++ b/ServiceDefinition/Dumper/WsdlFileDumper.php @@ -0,0 +1,117 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Bundle\WebServiceBundle\ServiceDefinition\Dumper; + +use Bundle\WebServiceBundle\ServiceDefinition\Method; +use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition; + +use Bundle\WebServiceBundle\Util\Assert; + +use Zend\Soap\Wsdl; + + +/** + * + * @author Christian Kerl + */ +class WsdlFileDumper extends FileDumper +{ + private $namespace; + + public function __construct($file, $namespace) + { + parent::__construct($file); + + Assert::thatArgumentNotNull('namespace', $namespace); + + $this->namespace = $namespace; + } + + public function dumpServiceDefinition(ServiceDefinition $definition) + { + Assert::thatArgumentNotNull('definition', $definition); + + $wsdl = new Wsdl($definition->getName(), $this->namespace); + + $port = $wsdl->addPortType($this->getPortTypeName($definition)); + $binding = $wsdl->addBinding($this->getBindingName($definition), $this->getPortTypeName($definition)); + + $wsdl->addSoapBinding($binding, 'document'); + $wsdl->addService($this->getServiceName($definition), $this->getPortTypeName($definition), $this->getBindingName($definition), 'http://localhost/service/'); + + foreach($definition->getMethods() as $method) + { + $requestParts = array(); + $responseParts = array(); + + foreach($method->getArguments() as $argument) + { + $requestParts[$argument->getName()] = $wsdl->getType($argument->getType()->getPhpType()); + } + + $responseParts['return'] = $wsdl->getType($method->getReturn()->getPhpType()); + + $wsdl->addMessage($this->getRequestMessageName($method), $requestParts); + $wsdl->addMessage($this->getResponseMessageName($method), $responseParts); + + $portOperation = $wsdl->addPortOperation($port, $method->getName(), $this->getRequestMessageName($method), $this->getResponseMessageName($method)); + $portOperation->setAttribute('parameterOrder', implode(' ', array_keys($requestParts))); + + $bindingInput = array( + 'parts' => implode(' ', array_keys($requestParts)), + 'use' => 'literal', + 'namespace' => $this->namespace, + 'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/', + ); + $bindingOutput = array( + 'parts' => implode(' ', array_keys($responseParts)), + 'use' => 'literal', + 'namespace' => $this->namespace, + 'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/', + ); + + $bindingOperation = $wsdl->addBindingOperation($binding, $method->getName(), $bindingInput, $bindingOutput); + $wsdl->addSoapOperation($bindingOperation, $this->getSoapOperationName($method)); + } + + $wsdl->dump($this->file); + } + + protected function getPortTypeName(ServiceDefinition $definition) + { + return $definition->getName() . 'PortType'; + } + + protected function getBindingName(ServiceDefinition $definition) + { + return $definition->getName() . 'Binding'; + } + + protected function getServiceName(ServiceDefinition $definition) + { + return $definition->getName() . 'Service'; + } + + protected function getRequestMessageName(Method $method) + { + return $method->getName() . 'Request'; + } + + protected function getResponseMessageName(Method $method) + { + return $method->getName() . 'Response'; + } + + protected function getSoapOperationName(Method $method) + { + return $this->namespace . $method->getName(); + } +}