BeSimpleSoap/ServiceDefinition/Dumper/WsdlDumper.php

163 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Dumper;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Type;
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use BeSimple\SoapBundle\Util\Assert;
use BeSimple\SoapBundle\Util\QName;
/**
* @author Christian Kerl <christian-kerl@web.de>
*/
class WsdlDumper implements DumperInterface
{
private $loader;
private $wsdl;
2011-02-03 01:04:12 +01:00
private $definition;
2011-07-14 17:45:03 +02:00
public function __construct(AnnotationComplexTypeLoader $loader)
{
$this->loader = $loader;
}
public function dumpServiceDefinition(ServiceDefinition $definition, array $options = array())
{
Assert::thatArgumentNotNull('definition', $definition);
$options = array_merge(array('endpoint' => '', 'stylesheet' => null), $options);
$this->definition = $definition;
$this->wsdl = new Wsdl($definition->getName(), $definition->getNamespace(), new WsdlTypeStrategy($this->loader, $definition));
$port = $this->wsdl->addPortType($this->getPortTypeName());
$binding = $this->wsdl->addBinding($this->getBindingName(), $this->qualify($this->getPortTypeName()));
2011-07-14 17:45:03 +02:00
$this->wsdl->addSoapBinding($binding, 'rpc');
$this->wsdl->addService($this->getServiceName(), $this->getPortName(), $this->qualify($this->getBindingName()), $options['endpoint']);
foreach ($definition->getMethods() as $method) {
$requestHeaderParts =
$requestParts =
$responseParts = array();
foreach ($method->getHeaders() as $header) {
$requestHeaderParts[$header->getName()] = $this->wsdl->getType($header->getType()->getPhpType());
}
foreach ($method->getArguments() as $argument) {
$requestParts[$argument->getName()] = $this->wsdl->getType($argument->getType()->getPhpType());
}
if ($method->getReturn() !== null) {
$responseParts['return'] = $this->wsdl->getType($method->getReturn()->getPhpType());
}
if (!empty($requestHeaderParts)) {
$this->wsdl->addMessage($this->getRequestHeaderMessageName($method), $requestHeaderParts);
}
$this->wsdl->addMessage($this->getRequestMessageName($method), $requestParts);
$this->wsdl->addMessage($this->getResponseMessageName($method), $responseParts);
$portOperation = $this->wsdl->addPortOperation(
$port,
$method->getName(),
$this->qualify($this->getRequestMessageName($method)),
$this->qualify($this->getResponseMessageName($method))
);
$portOperation->setAttribute('parameterOrder', implode(' ', array_keys($requestParts)));
$baseBinding = array(
'use' => 'literal',
'namespace' => $definition->getNamespace(),
'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/',
);
$bindingOperation = $this->wsdl->addBindingOperation(
$binding,
$method->getName(),
array_merge(array('parts' => implode(' ', array_keys($requestParts))), $baseBinding),
array_merge(array('parts' => implode(' ', array_keys($responseParts))), $baseBinding)
);
$bindingOperation = $this->wsdl->addBindingOperationHeader(
$bindingOperation,
array_keys($requestHeaderParts),
array_merge(array('message' => $this->qualify($this->getRequestHeaderMessageName($method))), $baseBinding)
);
$this->wsdl->addSoapOperation($bindingOperation, $this->getSoapOperationName($method));
}
2011-02-03 01:04:12 +01:00
$this->definition = null;
$dom = $this->wsdl->toDomDocument();
$dom->formatOutput = true;
if (null !== $options['stylesheet']) {
$stylesheet = $dom->createProcessingInstruction('xml-stylesheet', sprintf('type="text/xsl" href="%s"', $options['stylesheet']));
$dom->insertBefore($stylesheet, $dom->documentElement);
}
2011-07-14 17:45:03 +02:00
2011-07-17 19:41:06 +02:00
return $this->wsdl->toXml();
}
protected function qualify($name, $namespace = null)
{
if($namespace === null) {
$namespace = $this->definition->getNamespace();
}
return $this->wsdl->toDomDocument()->lookupPrefix($namespace).':'.$name;
}
protected function getPortName()
{
return $this->definition->getName().'Port';
}
2011-07-14 17:45:03 +02:00
2011-02-03 01:04:12 +01:00
protected function getPortTypeName()
{
return $this->definition->getName().'PortType';
}
2011-02-03 01:04:12 +01:00
protected function getBindingName()
{
return $this->definition->getName().'Binding';
}
2011-02-03 01:04:12 +01:00
protected function getServiceName()
{
return $this->definition->getName().'Service';
}
protected function getRequestHeaderMessageName(Method $method)
{
return $method->getName().'Header';
}
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->definition->getNamespace().$method->getName();
}
}