added support for 'typemap' and 'classmap' SoapServer option
This commit is contained in:
parent
4cc5950a66
commit
60795fbcbb
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of the WebServiceBundle.
|
||||
*
|
||||
* (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 Bundle\WebServiceBundle\Converter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Kerl <christian-kerl@web.de>
|
||||
*/
|
||||
use Bundle\WebServiceBundle\SoapKernel;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
class ConverterRepository
|
||||
{
|
||||
private $typeConverters = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function addTypeConverter(TypeConverterInterface $converter)
|
||||
{
|
||||
$this->typeConverters[] = $converter;
|
||||
}
|
||||
|
||||
public function toSoapServerTypemap(SoapKernel $kernel)
|
||||
{
|
||||
$result = array();
|
||||
|
||||
foreach($this->typeConverters as $typeConverter)
|
||||
{
|
||||
$result[] = array(
|
||||
'type_name' => $typeConverter->getTypeName(),
|
||||
'type_ns' => $typeConverter->getTypeNamespace(),
|
||||
'from_xml' => function($input) use ($kernel, $typeConverter) {
|
||||
return $typeConverter->convertXmlToPhp($kernel->getRequest(), $input);
|
||||
},
|
||||
'to_xml' => function($input) use ($kernel, $typeConverter) {
|
||||
return $typeConverter->convertPhpToXml($kernel->getResponse(), $input);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function registerTypeConverterServices(ContainerInterface $container)
|
||||
{
|
||||
foreach($container->findTaggedServiceIds('webservice.converter') as $id => $attributes)
|
||||
{
|
||||
$this->addTypeConverter($container->get($id));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of the WebServiceBundle.
|
||||
*
|
||||
* (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 Bundle\WebServiceBundle\Converter;
|
||||
|
||||
use Bundle\WebServiceBundle\Soap\SoapRequest;
|
||||
|
||||
use Bundle\WebServiceBundle\Soap\SoapResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Kerl <christian-kerl@web.de>
|
||||
*/
|
||||
interface TypeConverterInterface
|
||||
{
|
||||
function getTypeNamespace();
|
||||
|
||||
function getTypeName();
|
||||
|
||||
function convertXmlToPhp(SoapRequest $request, $data);
|
||||
|
||||
function convertPhpToXml(SoapResponse $response, $data);
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of the WebServiceBundle.
|
||||
*
|
||||
* (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 Bundle\WebServiceBundle\Converter;
|
||||
|
||||
use Bundle\WebServiceBundle\Soap\SoapRequest;
|
||||
|
||||
use Bundle\WebServiceBundle\Soap\SoapResponse;
|
||||
|
||||
use Bundle\WebServiceBundle\Util\String;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Kerl <christian-kerl@web.de>
|
||||
*/
|
||||
class XopIncludeTypeConverter implements TypeConverterInterface
|
||||
{
|
||||
public function getTypeNamespace()
|
||||
{
|
||||
return 'http://www.w3.org/2001/XMLSchema';
|
||||
}
|
||||
|
||||
public function getTypeName()
|
||||
{
|
||||
return 'base64Binary';
|
||||
}
|
||||
|
||||
public function convertXmlToPhp(SoapRequest $request, $data)
|
||||
{
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadXML($data);
|
||||
|
||||
$includes = $doc->getElementsByTagNameNS('http://www.w3.org/2004/08/xop/include', 'Include');
|
||||
$include = $includes->item(0);
|
||||
|
||||
$ref = $include->getAttribute('href');
|
||||
|
||||
if(String::startsWith($ref, 'cid:'))
|
||||
{
|
||||
$cid = urldecode(substr($ref, 4));
|
||||
|
||||
return $request->getSoapAttachments()->get($cid)->getContent();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function convertPhpToXml(SoapResponse $response, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
}
|
|
@ -23,7 +23,7 @@ class WebServiceExtension extends Extension
|
|||
{
|
||||
public function configLoad(array $config, ContainerBuilder $configuration)
|
||||
{
|
||||
if(!$configuration->hasDefinition('webservice_http_kernel'))
|
||||
if(!$configuration->hasDefinition('webservice.kernel'))
|
||||
{
|
||||
$loader = new XmlFileLoader($configuration, __DIR__ . '/../Resources/config');
|
||||
$loader->load('services.xml');
|
||||
|
@ -55,6 +55,7 @@ class WebServiceExtension extends Extension
|
|||
|
||||
$configuration->setParameter('webservice.definition.name', $config['name']);
|
||||
$configuration->setParameter('webservice.definition.resource', isset($config['resource']) ? $config['resource'] : null);
|
||||
$configuration->setParameter('webservice.definition.wsdl', isset($config['wsdl']) ? $config['wsdl'] : null);
|
||||
}
|
||||
|
||||
protected function registerServiceBindingConfig(array $config, ContainerBuilder $configuration)
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
<parameter key="webservice.definition.class">Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition</parameter>
|
||||
<parameter key="webservice.definition.loader.class">Bundle\WebServiceBundle\ServiceDefinition\Loader\XmlFileLoader</parameter>
|
||||
<parameter key="webservice.definition.dumper.class">Bundle\WebServiceBundle\ServiceDefinition\Dumper\Wsdl11DocumentLiteralWrappedFileDumper</parameter>
|
||||
<parameter key="webservice.definition.dumper.class">Bundle\WebServiceBundle\Tests\StaticFileDumper</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
|
@ -23,6 +23,7 @@
|
|||
|
||||
<service id="webservice.kernel" class="Bundle\WebServiceBundle\SoapKernel">
|
||||
<argument type="service" id="webservice.binder" />
|
||||
<argument type="service" id="webservice.converter.repository" />
|
||||
<argument type="service" id="symfony_http_kernel" />
|
||||
</service>
|
||||
|
||||
|
@ -38,6 +39,12 @@
|
|||
<service id="webservice.binder.request" class="%webservice.binder.request.class%" />
|
||||
<service id="webservice.binder.response" class="%webservice.binder.response.class%" />
|
||||
|
||||
<service id="webservice.converter.repository" class="Bundle\WebServiceBundle\Converter\ConverterRepository">
|
||||
<call method="registerTypeConverterServices">
|
||||
<argument type="service" id="service_container" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="webservice.definition" class="%webservice.definition.class%" shared="true">
|
||||
<argument type="string">%webservice.definition.name%</argument>
|
||||
</service>
|
||||
|
@ -45,7 +52,7 @@
|
|||
<argument type="string">%webservice.definition.resource%</argument>
|
||||
</service>
|
||||
<service id="webservice.definition.dumper" class="%webservice.definition.dumper.class%">
|
||||
<argument type="string"></argument>
|
||||
<argument type="string">%webservice.definition.wsdl%</argument>
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
namespace Bundle\WebServiceBundle\ServiceBinding;
|
||||
|
||||
use Bundle\WebServiceBundle\ServiceDefinition\Type;
|
||||
|
||||
use Bundle\WebServiceBundle\Soap\SoapHeader;
|
||||
|
||||
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
|
||||
|
@ -62,6 +64,39 @@ class ServiceBinder
|
|||
return $this->definitionDumper->dumpServiceDefinition($this->definition);
|
||||
}
|
||||
|
||||
public function getSoapServerClassmap()
|
||||
{
|
||||
$result = array();
|
||||
|
||||
foreach($this->definition->getHeaders() as $header)
|
||||
{
|
||||
$this->addSoapServerClassmapEntry($result, $header->getType());
|
||||
}
|
||||
|
||||
foreach($this->definition->getMethods() as $method)
|
||||
{
|
||||
foreach($method->getArguments() as $arg)
|
||||
{
|
||||
$this->addSoapServerClassmapEntry($result, $arg->getType());
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function addSoapServerClassmapEntry(&$classmap, Type $type)
|
||||
{
|
||||
list($namespace, $xmlType) = $this->parsePackedQName($type->getXmlType());
|
||||
$phpType = $type->getPhpType();
|
||||
|
||||
if(isset($classmap[$xmlType]) && $classmap[$xmlType] != $phpType)
|
||||
{
|
||||
// log warning
|
||||
}
|
||||
|
||||
$classmap[$xmlType] = $phpType;
|
||||
}
|
||||
|
||||
public function isServiceHeader($name)
|
||||
{
|
||||
return $this->definition->getHeaders()->has($name);
|
||||
|
@ -99,11 +134,18 @@ class ServiceBinder
|
|||
|
||||
protected function createSoapHeader(Header $headerDefinition, $data)
|
||||
{
|
||||
if(!preg_match('/^\{(.+)\}(.+)$/', $headerDefinition->getType()->getXmlType(), $matches))
|
||||
list($namespace, $name) = $this->parsePackedQName($headerDefinition->getType()->getXmlType());
|
||||
|
||||
return new SoapHeader($namespace, $name, $data);
|
||||
}
|
||||
|
||||
private function parsePackedQName($qname)
|
||||
{
|
||||
if(!preg_match('/^\{(.+)\}(.+)$/', $qname, $matches))
|
||||
{
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
return new SoapHeader($matches[1], $matches[2], $data);
|
||||
return array($matches[1], $matches[2]);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
namespace Bundle\WebServiceBundle;
|
||||
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
|
@ -21,6 +22,8 @@ use Bundle\WebServiceBundle\Soap\SoapHeader;
|
|||
|
||||
use Bundle\WebServiceBundle\ServiceBinding\ServiceBinder;
|
||||
|
||||
use Bundle\WebServiceBundle\Converter\ConverterRepository;
|
||||
|
||||
use Bundle\WebServiceBundle\Util\String;
|
||||
|
||||
/**
|
||||
|
@ -58,11 +61,18 @@ class SoapKernel implements HttpKernelInterface
|
|||
*/
|
||||
protected $kernel;
|
||||
|
||||
public function __construct(ServiceBinder $serviceBinder, HttpKernelInterface $kernel)
|
||||
public function __construct(ServiceBinder $serviceBinder, ConverterRepository $converterRepository, HttpKernelInterface $kernel)
|
||||
{
|
||||
$this->serviceBinder = $serviceBinder;
|
||||
|
||||
$this->soapServer = new \SoapServer($this->serviceBinder->getSerializedServiceDefinition());
|
||||
$this->soapServer = new \SoapServer(
|
||||
$this->serviceBinder->getSerializedServiceDefinition(),
|
||||
array(
|
||||
'classmap' => $this->serviceBinder->getSoapServerClassmap(),
|
||||
'typemap' => $converterRepository->toSoapServerTypemap($this),
|
||||
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
|
||||
)
|
||||
);
|
||||
$this->soapServer->setObject($this);
|
||||
|
||||
$this->kernel = $kernel;
|
||||
|
@ -73,7 +83,12 @@ class SoapKernel implements HttpKernelInterface
|
|||
return $this->soapRequest;
|
||||
}
|
||||
|
||||
public function handle(Request $request = null, $type = self::MASTER_REQUEST, $raw = false)
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->soapResponse;
|
||||
}
|
||||
|
||||
public function handle(Request $request = null, $type = self::MASTER_REQUEST, $catch = true)
|
||||
{
|
||||
$this->soapRequest = $this->checkRequest($request);
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
namespace Bundle\WebServiceBundle\Tests;
|
||||
|
||||
use Bundle\WebServiceBundle\Converter\ConverterRepository;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Bundle\WebServiceBundle\SoapKernel;
|
||||
|
@ -46,12 +48,14 @@ class SoapKernelTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$serviceBinder = new ServiceBinder($serviceDefinition, $serviceDefinitionLoader, $serviceDefinitionDumper, $requestMessageBinder, $responseMessageBinder);
|
||||
|
||||
$converterRepository = new ConverterRepository();
|
||||
|
||||
$httpKernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
|
||||
$httpKernel->expects($this->any())
|
||||
->method('handle')
|
||||
->will($this->returnValue(new SoapResponse(200)));
|
||||
|
||||
$this->soapKernel = new SoapKernel($serviceBinder, $httpKernel);
|
||||
$this->soapKernel = new SoapKernel($serviceBinder, $converterRepository, $httpKernel);
|
||||
}
|
||||
|
||||
public function testHandle()
|
||||
|
|
Loading…
Reference in New Issue