171 lines
5.2 KiB
PHP
Raw Normal View History

2011-10-09 19:41:56 +02:00
<?php
/*
* This file is part of the BeSimpleSoapServer.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapServer;
2018-04-06 10:58:45 +02:00
use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Converter\MtomTypeConverter;
use BeSimple\SoapCommon\Converter\SwaTypeConverter;
2011-10-09 19:41:56 +02:00
/**
* Extended SoapServer that allows adding filters for SwA, MTOM, ... .
*
* @author Andreas Schamberger <mail@andreass.net>
2011-10-09 19:41:56 +02:00
* @author Christian Kerl <christian-kerl@web.de>
*/
class SoapServer extends \SoapServer
{
/**
2018-04-06 10:58:45 +02:00
* Soap version.
*
2018-04-06 10:58:45 +02:00
* @var int
*/
2018-04-06 10:58:45 +02:00
protected $soapVersion = SOAP_1_1;
2018-04-06 10:58:45 +02:00
/**
* Soap kernel.
*
* @var \BeSimple\SoapServer\SoapKernel
*/
protected $soapKernel = null;
2011-10-09 19:41:56 +02:00
/**
2018-04-06 10:58:45 +02:00
* Constructor.
*
2018-04-06 10:58:45 +02:00
* @param string $wsdl WSDL file
* @param array(string=>mixed) $options Options array
*/
2018-04-06 10:58:45 +02:00
public function __construct($wsdl, array $options = array())
{
2018-04-06 10:58:45 +02:00
// store SOAP version
if (isset($options['soap_version'])) {
$this->soapVersion = $options['soap_version'];
}
// create soap kernel instance
$this->soapKernel = new SoapKernel();
// set up type converter and mime filter
$this->configureMime($options);
// we want the exceptions option to be set
$options['exceptions'] = true;
parent::__construct($wsdl, $options);
}
/**
* Custom handle method to be able to modify the SOAP messages.
*
2018-04-06 10:58:45 +02:00
* @param string $request Request string
*/
2018-04-06 10:58:45 +02:00
public function handle($request = null)
{
2018-04-06 10:58:45 +02:00
// wrap request data in SoapRequest object
$soapRequest = SoapRequest::create($request, $this->soapVersion);
2018-04-06 10:58:45 +02:00
// handle actual SOAP request
try {
2018-04-06 10:58:45 +02:00
$soapResponse = $this->handle2($soapRequest);
} catch (\SoapFault $fault) {
2018-04-06 10:58:45 +02:00
// issue an error to the client
$this->fault($fault->faultcode, $fault->faultstring);
}
2018-04-06 10:58:45 +02:00
// send SOAP response to client
$soapResponse->send();
}
/**
* Runs the currently registered request filters on the request, calls the
* necessary functions (through the parent's class handle()) and runs the
* response filters.
*
* @param SoapRequest $soapRequest SOAP request object
*
* @return SoapResponse
*/
2018-04-06 10:58:45 +02:00
public function handle2(SoapRequest $soapRequest)
{
2018-04-06 10:58:45 +02:00
// run SoapKernel on SoapRequest
$this->soapKernel->filterRequest($soapRequest);
2018-04-06 10:58:45 +02:00
// call parent \SoapServer->handle() and buffer output
ob_start();
parent::handle($soapRequest->getContent());
2018-04-06 10:58:45 +02:00
$response = ob_get_clean();
// Remove headers added by SoapServer::handle() method
header_remove('Content-Length');
header_remove('Content-Type');
2018-04-06 10:58:45 +02:00
// wrap response data in SoapResponse object
$soapResponse = SoapResponse::create(
$response,
$soapRequest->getLocation(),
$soapRequest->getAction(),
2018-04-06 10:58:45 +02:00
$soapRequest->getVersion()
);
2018-04-06 10:58:45 +02:00
// run SoapKernel on SoapResponse
$this->soapKernel->filterResponse($soapResponse);
return $soapResponse;
}
2018-04-06 10:58:45 +02:00
/**
* Get SoapKernel instance.
*
* @return \BeSimple\SoapServer\SoapKernel
*/
public function getSoapKernel()
{
2018-04-06 10:58:45 +02:00
return $this->soapKernel;
}
/**
2018-04-06 10:58:45 +02:00
* Configure filter and type converter for SwA/MTOM.
*
* @param array &$options SOAP constructor options array.
*
2018-04-06 10:58:45 +02:00
* @return void
*/
2018-04-06 10:58:45 +02:00
private function configureMime(array &$options)
2011-10-09 19:41:56 +02:00
{
2018-04-06 10:58:45 +02:00
if (isset($options['attachment_type']) && Helper::ATTACHMENTS_TYPE_BASE64 !== $options['attachment_type']) {
// register mime filter in SoapKernel
$mimeFilter = new MimeFilter($options['attachment_type']);
$this->soapKernel->registerFilter($mimeFilter);
// configure type converter
if (Helper::ATTACHMENTS_TYPE_SWA === $options['attachment_type']) {
$converter = new SwaTypeConverter();
$converter->setKernel($this->soapKernel);
} elseif (Helper::ATTACHMENTS_TYPE_MTOM === $options['attachment_type']) {
$xmlMimeFilter = new XmlMimeFilter($options['attachment_type']);
$this->soapKernel->registerFilter($xmlMimeFilter);
$converter = new MtomTypeConverter();
$converter->setKernel($this->soapKernel);
}
2018-04-06 10:58:45 +02:00
// configure typemap
if (!isset($options['typemap'])) {
$options['typemap'] = array();
2016-11-08 18:31:28 +01:00
}
2018-04-06 10:58:45 +02:00
$options['typemap'][] = array(
'type_name' => $converter->getTypeName(),
'type_ns' => $converter->getTypeNamespace(),
'from_xml' => function($input) use ($converter) {
return $converter->convertXmlToPhp($input);
},
'to_xml' => function($input) use ($converter) {
return $converter->convertPhpToXml($input);
},
);
2016-11-08 18:31:28 +01:00
}
}
}