2011-11-21 20:21:24 +01:00
|
|
|
<?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\SoapCommon;
|
|
|
|
|
2012-01-03 16:38:50 +01:00
|
|
|
use BeSimple\SoapCommon\Mime\Part as MimePart;
|
|
|
|
|
2011-11-21 20:21:24 +01:00
|
|
|
use BeSimple\SoapCommon\SoapRequest;
|
|
|
|
use BeSimple\SoapCommon\SoapResponse;
|
|
|
|
|
|
|
|
use BeSimple\SoapCommon\SoapRequestFilter;
|
|
|
|
use BeSimple\SoapCommon\SoapResponseFilter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SoapKernel provides methods to pre- and post-process SoapRequests and SoapResponses using
|
2011-12-17 16:25:49 +01:00
|
|
|
* chains of SoapRequestFilter and SoapResponseFilter objects (roughly following
|
2011-11-21 20:21:24 +01:00
|
|
|
* the chain-of-responsibility pattern).
|
2011-12-17 16:25:49 +01:00
|
|
|
*
|
2011-11-21 20:21:24 +01:00
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
|
|
|
*/
|
|
|
|
class SoapKernel
|
|
|
|
{
|
2012-01-03 16:38:50 +01:00
|
|
|
/**
|
|
|
|
* Mime attachments.
|
|
|
|
*
|
|
|
|
* @var array(\BeSimple\SoapCommon\Mime\Part)
|
|
|
|
*/
|
|
|
|
protected $attachments = array();
|
|
|
|
|
2011-12-17 16:25:49 +01:00
|
|
|
/**
|
|
|
|
* Request filters.
|
|
|
|
*
|
|
|
|
* @var array(SoapRequestFilter)
|
|
|
|
*/
|
2011-11-21 20:21:24 +01:00
|
|
|
private $requestFilters = array();
|
2011-12-17 16:25:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Response filters.
|
|
|
|
*
|
|
|
|
* @var array(SoapResponseFilter)
|
|
|
|
*/
|
2011-11-21 20:21:24 +01:00
|
|
|
private $responseFilters = array();
|
2011-12-17 16:25:49 +01:00
|
|
|
|
2012-01-03 16:38:50 +01:00
|
|
|
/**
|
|
|
|
* Add attachment.
|
|
|
|
*
|
|
|
|
* @param \BeSimple\SoapCommon\Mime\Part $attachment New attachment
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addAttachment(MimePart $attachment)
|
|
|
|
{
|
|
|
|
$contentId = trim($part->getHeader('Content-ID'), '<>');
|
|
|
|
|
|
|
|
$this->attachments[$contentId] = $attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get attachment and remove from array.
|
|
|
|
*
|
|
|
|
* @param string $contentId Content ID of attachment
|
|
|
|
*
|
|
|
|
* @return \BeSimple\SoapCommon\Mime\Part|null
|
|
|
|
*/
|
|
|
|
public function getAttachment($contentId)
|
|
|
|
{
|
|
|
|
if (isset($this->attachments[$contentId])) {
|
|
|
|
$part = $this->attachments[$contentId];
|
|
|
|
unset($this->attachments[$contentId]);
|
|
|
|
|
|
|
|
return $part;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:21:24 +01:00
|
|
|
/**
|
2011-12-17 16:25:49 +01:00
|
|
|
* Registers the given object either as filter for SoapRequests or as filter for SoapResponses
|
2011-11-21 20:21:24 +01:00
|
|
|
* or as filter for both depending on the implemented interfaces. Inner filters have to be registered
|
2011-12-17 16:25:49 +01:00
|
|
|
* before outer filters. This means the order is as follows: RequestFilter2->RequestFilter1 and
|
2011-11-21 20:21:24 +01:00
|
|
|
* ResponseFilter1->ResponseFilter2.
|
2011-12-17 16:25:49 +01:00
|
|
|
*
|
2011-11-21 20:21:24 +01:00
|
|
|
* TODO: add priority mechanism to ensure correct order of filters
|
2011-12-17 16:25:49 +01:00
|
|
|
*
|
|
|
|
* @param SoapRequestFilter|SoapResponseFilter $filter Filter to register
|
2011-11-21 20:21:24 +01:00
|
|
|
*/
|
|
|
|
public function registerFilter($filter)
|
2011-12-17 16:25:49 +01:00
|
|
|
{
|
|
|
|
if ($filter instanceof SoapRequestFilter) {
|
2011-11-21 20:21:24 +01:00
|
|
|
array_unshift($this->requestFilters, $filter);
|
|
|
|
}
|
2011-12-17 16:25:49 +01:00
|
|
|
|
|
|
|
if ($filter instanceof SoapResponseFilter) {
|
2011-11-21 20:21:24 +01:00
|
|
|
array_push($this->responseFilters, $filter);
|
|
|
|
}
|
|
|
|
}
|
2011-12-17 16:25:49 +01:00
|
|
|
|
2011-11-21 20:21:24 +01:00
|
|
|
/**
|
2011-12-17 16:25:49 +01:00
|
|
|
* Applies all registered SoapRequestFilter to the given SoapRequest.
|
|
|
|
*
|
|
|
|
* @param SoapRequest $request Soap request
|
2011-11-21 20:21:24 +01:00
|
|
|
*/
|
|
|
|
public function filterRequest(SoapRequest $request)
|
|
|
|
{
|
2012-01-03 16:38:50 +01:00
|
|
|
$request->setAttachments($this->attachments);
|
|
|
|
$this->attachments = array();
|
|
|
|
|
2011-12-17 16:25:49 +01:00
|
|
|
foreach ($this->requestFilters as $filter) {
|
2011-11-21 20:21:24 +01:00
|
|
|
$filter->filterRequest($request);
|
|
|
|
}
|
|
|
|
}
|
2011-12-17 16:25:49 +01:00
|
|
|
|
2011-11-21 20:21:24 +01:00
|
|
|
/**
|
2011-12-17 16:25:49 +01:00
|
|
|
* Applies all registered SoapResponseFilter to the given SoapResponse.
|
|
|
|
*
|
|
|
|
* @param SoapResponse $response SOAP response
|
2011-11-21 20:21:24 +01:00
|
|
|
*/
|
|
|
|
public function filterResponse(SoapResponse $response)
|
|
|
|
{
|
2011-12-17 16:25:49 +01:00
|
|
|
foreach ($this->responseFilters as $filter) {
|
2011-11-21 20:28:31 +01:00
|
|
|
$filter->filterResponse($response);
|
2011-11-21 20:21:24 +01:00
|
|
|
}
|
2012-01-03 16:38:50 +01:00
|
|
|
|
|
|
|
$this->attachments = $response->getAttachments();
|
2011-11-21 20:21:24 +01:00
|
|
|
}
|
|
|
|
}
|