MimeFilters are now stateless

This commit is contained in:
Petr Bechyně 2016-11-02 09:55:12 +01:00
parent 969709cae5
commit bf494a42b5
10 changed files with 31 additions and 199 deletions

View File

@ -28,41 +28,8 @@ use BeSimple\SoapCommon\SoapResponseFilter;
*/ */
class MimeFilter implements SoapRequestFilter, SoapResponseFilter class MimeFilter implements SoapRequestFilter, SoapResponseFilter
{ {
/** public function filterRequest(SoapRequest $request, $attachmentType)
* Attachment type.
*
* @var int Helper::ATTACHMENTS_TYPE_SWA | Helper::ATTACHMENTS_TYPE_MTOM
*/
protected $attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
/**
* Constructor.
*
* @param int $attachmentType Helper::ATTACHMENTS_TYPE_SWA | Helper::ATTACHMENTS_TYPE_MTOM
*/
public function __construct($attachmentType)
{ {
$this->attachmentType = $attachmentType;
}
/**
* Reset all properties to default values.
*/
public function resetFilter()
{
$this->attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
}
/**
* Modify the given request XML.
*
* @param \BeSimple\SoapCommon\SoapRequest $request SOAP request
*
* @return void
*/
public function filterRequest(SoapRequest $request)
{
// get attachments from request object
$attachmentsToSend = $request->getAttachments(); $attachmentsToSend = $request->getAttachments();
// build mime message if we have attachments // build mime message if we have attachments
@ -71,7 +38,7 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
$soapPart = new MimePart($request->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT); $soapPart = new MimePart($request->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT);
$soapVersion = $request->getVersion(); $soapVersion = $request->getVersion();
// change content type headers for MTOM with SOAP 1.1 // change content type headers for MTOM with SOAP 1.1
if ($soapVersion == SOAP_1_1 && $this->attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) { if ($soapVersion == SOAP_1_1 && $attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) {
$multipart->setHeader('Content-Type', 'type', 'application/xop+xml'); $multipart->setHeader('Content-Type', 'type', 'application/xop+xml');
$multipart->setHeader('Content-Type', 'start-info', 'text/xml'); $multipart->setHeader('Content-Type', 'start-info', 'text/xml');
$soapPart->setHeader('Content-Type', 'application/xop+xml'); $soapPart->setHeader('Content-Type', 'application/xop+xml');
@ -94,19 +61,13 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
$request->setContentType($contentType); $request->setContentType($contentType);
} }
return $request;
} }
/** public function filterResponse(SoapResponse $response, $attachmentType)
* Modify the given response XML.
*
* @param \BeSimple\SoapCommon\SoapResponse $response SOAP response
*
* @return void
*/
public function filterResponse(SoapResponse $response)
{ {
// array to store attachments $attachmentsReceived = [];
$attachmentsRecieved = array();
// check content type if it is a multipart mime message // check content type if it is a multipart mime message
$responseContentType = $response->getContentType(); $responseContentType = $response->getContentType();
@ -126,13 +87,13 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
// store attachments // store attachments
$attachments = $multipart->getParts(false); $attachments = $multipart->getParts(false);
foreach ($attachments as $cid => $attachment) { foreach ($attachments as $cid => $attachment) {
$attachmentsRecieved[$cid] = $attachment; $attachmentsReceived[$cid] = $attachment;
} }
} }
if (count($attachmentsReceived) > 0) {
// add attachments to response object $response->setAttachments($attachmentsReceived);
if (count($attachmentsRecieved) > 0) {
$response->setAttachments($attachmentsRecieved);
} }
return $response;
} }
} }

View File

@ -1,47 +0,0 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
* (c) Andreas Schamberger <mail@andreass.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapClient;
use BeSimple\SoapCommon\SoapKernel as CommonSoapKernel;
use BeSimple\SoapCommon\SoapRequest as CommonSoapRequest;
use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse;
/**
* SoapKernel for Client.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapKernel extends CommonSoapKernel
{
/**
* {@inheritDoc}
*/
public function filterRequest(CommonSoapRequest $request)
{
$request->setAttachments($this->attachments);
$this->attachments = array();
parent::filterRequest($request);
}
/**
* {@inheritDoc}
*/
public function filterResponse(CommonSoapResponse $response)
{
parent::filterResponse($response);
$this->attachments = $response->getAttachments();
}
}

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of the BeSimpleSoapClient.
*
* (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\SoapClient; namespace BeSimple\SoapClient;
use BeSimple\SoapCommon\FilterHelper; use BeSimple\SoapCommon\FilterHelper;
@ -24,21 +14,7 @@ use BeSimple\SoapCommon\SoapRequestFilter;
*/ */
class XmlMimeFilter implements SoapRequestFilter class XmlMimeFilter implements SoapRequestFilter
{ {
/** public function filterRequest(SoapRequest $request, $attachmentType)
* Reset all properties to default values.
*/
public function resetFilter()
{
}
/**
* Modify the given request XML.
*
* @param \BeSimple\SoapCommon\SoapRequest $request SOAP request
*
* @return void
*/
public function filterRequest(SoapRequest $request)
{ {
// get \DOMDocument from SOAP request // get \DOMDocument from SOAP request
$dom = $request->getContentDocument(); $dom = $request->getContentDocument();
@ -66,5 +42,6 @@ class XmlMimeFilter implements SoapRequestFilter
} }
} }
return $request;
} }
} }

View File

@ -53,13 +53,14 @@ class SoapKernel
* *
* @param SoapRequest $request Soap request * @param SoapRequest $request Soap request
* @param SoapRequestFilter[]|SoapResponseFilter[] $filters * @param SoapRequestFilter[]|SoapResponseFilter[] $filters
* @param int $attachmentType = SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64
* @return SoapRequest * @return SoapRequest
*/ */
public function filterRequest(SoapRequest $request, array $filters) public function filterRequest(SoapRequest $request, array $filters, $attachmentType)
{ {
foreach ($filters as $filter) { foreach ($filters as $filter) {
if ($filter instanceof SoapRequestFilter) { if ($filter instanceof SoapRequestFilter) {
$request = $filter->filterRequest($request); $request = $filter->filterRequest($request, $attachmentType);
} }
} }
@ -71,13 +72,14 @@ class SoapKernel
* *
* @param SoapResponse $response SOAP response * @param SoapResponse $response SOAP response
* @param SoapRequestFilter[]|SoapResponseFilter[] $filters * @param SoapRequestFilter[]|SoapResponseFilter[] $filters
* @param int $attachmentType = SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64
* @return SoapResponse * @return SoapResponse
*/ */
public function filterResponse(SoapResponse $response, array $filters) public function filterResponse(SoapResponse $response, array $filters, $attachmentType)
{ {
foreach ($filters as $filter) { foreach ($filters as $filter) {
if ($filter instanceof SoapResponseFilter) { if ($filter instanceof SoapResponseFilter) {
$response = $filter->filterResponse($response); $response = $filter->filterResponse($response, $attachmentType);
} }
} }

View File

@ -38,11 +38,11 @@ class SoapOptions
* @param string $encoding = SoapOptions::SOAP_ENCODING_UTF8 * @param string $encoding = SoapOptions::SOAP_ENCODING_UTF8
* @param SoapFeatures $features * @param SoapFeatures $features
* @param string $wsdlFile * @param string $wsdlFile
* @param string $wsdlCacheType = SoapOptions::SOAP_CACHE_TYPE_NONE|SoapOptions::SOAP_CACHE_TYPE_MEMORY|SoapOptions::SOAP_CACHE_TYPE_DISK|SoapOptions::SOAP_CACHE_TYPE_DISK_MEMORY * @param int $wsdlCacheType = SoapOptions::SOAP_CACHE_TYPE_NONE|SoapOptions::SOAP_CACHE_TYPE_MEMORY|SoapOptions::SOAP_CACHE_TYPE_DISK|SoapOptions::SOAP_CACHE_TYPE_DISK_MEMORY
* @param string $wsdlCacheDir = null * @param string|null $wsdlCacheDir = null
* @param ClassMap $classMap * @param ClassMap $classMap
* @param TypeConverterCollection $typeConverterCollection * @param TypeConverterCollection $typeConverterCollection
* @param string $attachmentType = SoapOptions::SOAP_ATTACHMENTS_OFF|SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64 * @param int|null $attachmentType = SoapOptions::SOAP_ATTACHMENTS_OFF|SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64
*/ */
public function __construct( public function __construct(
$soapVersion, $soapVersion,

View File

@ -13,8 +13,6 @@
namespace BeSimple\SoapCommon; namespace BeSimple\SoapCommon;
use BeSimple\SoapCommon\SoapRequest;
/** /**
* SOAP request filter interface. * SOAP request filter interface.
* *
@ -26,6 +24,7 @@ interface SoapRequestFilter
* Modify SOAP response. * Modify SOAP response.
* *
* @param SoapRequest $request SOAP request * @param SoapRequest $request SOAP request
* @param int $attachmentType = SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64
*/ */
public function filterRequest(SoapRequest $request); public function filterRequest(SoapRequest $request, $attachmentType);
} }

View File

@ -26,6 +26,7 @@ interface SoapResponseFilter
* Modify SOAP response. * Modify SOAP response.
* *
* @param SoapResponse $response SOAP response * @param SoapResponse $response SOAP response
* @param int $attachmentType = SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64
*/ */
public function filterResponse(SoapResponse $response); public function filterResponse(SoapResponse $response, $attachmentType);
} }

View File

@ -28,32 +28,7 @@ use BeSimple\SoapCommon\SoapResponseFilter;
*/ */
class MimeFilter implements SoapRequestFilter, SoapResponseFilter class MimeFilter implements SoapRequestFilter, SoapResponseFilter
{ {
/** public function filterRequest(SoapRequest $request, $attachmentType)
* Attachment type.
*
* @var int Helper::ATTACHMENTS_TYPE_SWA | Helper::ATTACHMENTS_TYPE_MTOM
*/
protected $attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
/**
* Constructor.
*
* @param int $attachmentType Helper::ATTACHMENTS_TYPE_SWA | Helper::ATTACHMENTS_TYPE_MTOM
*/
public function __construct($attachmentType)
{
$this->attachmentType = $attachmentType;
}
/**
* Reset all properties to default values.
*/
public function resetFilter()
{
$this->attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
}
public function filterRequest(SoapRequest $request)
{ {
$attachmentsReceived = []; $attachmentsReceived = [];
@ -86,7 +61,7 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
return $request; return $request;
} }
public function filterResponse(SoapResponse $response) public function filterResponse(SoapResponse $response, $attachmentType)
{ {
$attachmentsToSend = $response->getAttachments(); $attachmentsToSend = $response->getAttachments();
if (count($attachmentsToSend) > 0) { if (count($attachmentsToSend) > 0) {
@ -94,7 +69,7 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
$soapPart = new MimePart($response->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT); $soapPart = new MimePart($response->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT);
$soapVersion = $response->getVersion(); $soapVersion = $response->getVersion();
// change content type headers for MTOM with SOAP 1.1 // change content type headers for MTOM with SOAP 1.1
if ($soapVersion == SOAP_1_1 && $this->attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) { if ($soapVersion == SOAP_1_1 && $attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) {
$multipart->setHeader('Content-Type', 'type', 'application/xop+xml'); $multipart->setHeader('Content-Type', 'type', 'application/xop+xml');
$multipart->setHeader('Content-Type', 'start-info', 'text/xml'); $multipart->setHeader('Content-Type', 'start-info', 'text/xml');
$soapPart->setHeader('Content-Type', 'application/xop+xml'); $soapPart->setHeader('Content-Type', 'application/xop+xml');

View File

@ -1,36 +0,0 @@
<?php
namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\SoapRequest;
/**
* SoapKernel for Server.
*
* @todo-critical: kill this shit
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapServerKernel extends SoapKernel
{
/**
* {@inheritDoc}
*/
public function filterRequest(SoapRequest $request, array $filters)
{
parent::filterRequest($request, $filters);
// attachments are now gone from here
}
/**
* {@inheritDoc}
*/
public function filterResponse(SoapResponse $response)
{
$response->setAttachments($this->attachments);
$this->attachments = array();
parent::filterResponse($response);
}
}

View File

@ -100,7 +100,7 @@ class SoapServer extends \SoapServer
private function handleSoapRequest(SoapRequest $soapRequest) private function handleSoapRequest(SoapRequest $soapRequest)
{ {
if ($this->soapOptions->hasAttachments()) { if ($this->soapOptions->hasAttachments()) {
$soapRequest = $this->soapKernel->filterRequest($soapRequest, $this->getFilters()); $soapRequest = $this->soapKernel->filterRequest($soapRequest, $this->getFilters(), $this->soapOptions->getAttachmentType());
} }
ob_start(); ob_start();
@ -120,7 +120,7 @@ class SoapServer extends \SoapServer
); );
if ($this->soapOptions->hasAttachments()) { if ($this->soapOptions->hasAttachments()) {
$this->soapKernel->filterResponse($soapResponse, $this->getFilters()); $soapResponse = $this->soapKernel->filterResponse($soapResponse, $this->getFilters(), $this->soapOptions->getAttachmentType());
} }
return $soapResponse; return $soapResponse;