BeSimpleSoap/src/BeSimple/SoapCommon/SoapKernel.php

136 lines
3.6 KiB
PHP
Raw Normal View History

<?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;
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
* the chain-of-responsibility pattern).
2011-12-17 16:25:49 +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)
*/
private $requestFilters = array();
2011-12-17 16:25:49 +01:00
/**
* Response filters.
*
* @var array(SoapResponseFilter)
*/
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)
{
2012-01-03 18:48:40 +01:00
$contentId = trim($attachment->getHeader('Content-ID'), '<>');
2012-01-03 16:38:50 +01:00
$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-12-17 16:25:49 +01:00
* Registers the given object either as filter for SoapRequests or as filter for SoapResponses
* 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
* ResponseFilter1->ResponseFilter2.
2011-12-17 16:25:49 +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
*/
public function registerFilter($filter)
2011-12-17 16:25:49 +01:00
{
if ($filter instanceof SoapRequestFilter) {
array_unshift($this->requestFilters, $filter);
}
2011-12-17 16:25:49 +01:00
if ($filter instanceof SoapResponseFilter) {
array_push($this->responseFilters, $filter);
}
}
2011-12-17 16:25:49 +01:00
/**
2011-12-17 16:25:49 +01:00
* Applies all registered SoapRequestFilter to the given SoapRequest.
*
* @param SoapRequest $request Soap request
*/
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) {
$filter->filterRequest($request);
}
}
2011-12-17 16:25:49 +01:00
/**
2011-12-17 16:25:49 +01:00
* Applies all registered SoapResponseFilter to the given SoapResponse.
*
* @param SoapResponse $response SOAP response
*/
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);
}
2012-01-03 16:38:50 +01:00
$this->attachments = $response->getAttachments();
}
}