SoapResponse is a product of SoapResponseFactory, small refactorings

This commit is contained in:
Petr Bechyně 2016-11-02 16:08:21 +01:00
parent bf494a42b5
commit 8d033f9afc
7 changed files with 89 additions and 146 deletions

View File

@ -18,6 +18,7 @@ namespace BeSimple\SoapCommon;
class ClassMap class ClassMap
{ {
protected $classMap; protected $classMap;
protected $inverseClassMap;
public function __construct(array $classMap = []) public function __construct(array $classMap = [])
{ {
@ -61,6 +62,7 @@ class ClassMap
} }
$this->classMap[$type] = $className; $this->classMap[$type] = $className;
$this->inverseClassMap[$className] = $type;
} }
/** /**
@ -72,6 +74,20 @@ class ClassMap
return isset($this->classmap[$type]); return isset($this->classmap[$type]);
} }
public function getByClassName($className)
{
if (!$this->hasByClassName($className)) {
throw new \InvalidArgumentException(sprintf('The className "%s" was not found in %s', $className, __CLASS__));
}
return $this->inverseClassMap[$className];
}
public function hasByClassName($className)
{
return isset($this->inverseClassMap[$className]);
}
public function addClassMap(ClassMap $classMap) public function addClassMap(ClassMap $classMap)
{ {
foreach ($classMap->getAll() as $type => $className) { foreach ($classMap->getAll() as $type => $className) {

View File

@ -2,8 +2,6 @@
namespace BeSimple\SoapCommon; namespace BeSimple\SoapCommon;
use BeSimple\SoapCommon\Mime\Part as MimePart;
/** /**
* SoapKernel provides methods to pre- and post-process SoapRequests and SoapResponses using * SoapKernel provides methods to pre- and post-process SoapRequests and SoapResponses using
* chains of SoapRequestFilter and SoapResponseFilter objects (roughly following * chains of SoapRequestFilter and SoapResponseFilter objects (roughly following
@ -14,40 +12,6 @@ use BeSimple\SoapCommon\Mime\Part as MimePart;
*/ */
class SoapKernel class SoapKernel
{ {
/**
* Add attachment.
*
* @param \BeSimple\SoapCommon\Mime\Part $attachment New attachment
*
* @return void
*/
public function addAttachment(MimePart $attachment)
{
$contentId = trim($attachment->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;
}
/** /**
* Applies all registered SoapRequestFilter to the given SoapRequest. * Applies all registered SoapRequestFilter to the given SoapRequest.
* *

View File

@ -1,47 +0,0 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (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;
use BeSimple\SoapCommon\Classmap as BaseClassmap;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class Classmap extends BaseClassmap
{
protected $classmapInversed = array();
/**
* {@inheritdoc}
*/
public function add($type, $classname)
{
parent::add($type, $classname);
$this->classmapInversed[$classname] = $type;
}
public function getByClassname($classname)
{
if (!$this->hasByClassname($classname)) {
throw new \InvalidArgumentException(sprintf('The classname "%s" was not found in %s', $classname, __CLASS__));
}
return $this->classmapInversed[$classname];
}
public function hasByClassname($classname)
{
return isset($this->classmapInversed[$classname]);
}
}

View File

@ -1,54 +1,11 @@
<?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\SoapServer; namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse; use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse;
use BeSimple\SoapCommon\SoapMessage;
/**
* SoapResponse class for SoapClient. Provides factory function for response object.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapResponse extends CommonSoapResponse class SoapResponse extends CommonSoapResponse
{ {
/**
* Factory function for SoapResponse.
*
* @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
* @param array $attachments SOAP attachments
*
* @return SoapResponse
*/
public static function create($content, $location, $action, $version, $attachments = [])
{
$response = new SoapResponse();
$response->setContent($content);
$response->setLocation($location);
$response->setAction($action);
$response->setVersion($version);
$contentType = SoapMessage::getContentTypeForVersion($version);
$response->setContentType($contentType);
return $response;
}
/**
* Send SOAP response to client.
*/
public function getResponseContent() public function getResponseContent()
{ {
// set Content-Type header // set Content-Type header

View File

@ -0,0 +1,42 @@
<?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\SoapServer;
use BeSimple\SoapCommon\SoapMessage;
class SoapResponseFactory
{
/**
* Factory function for SoapResponse.
*
* @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
* @param array $attachments SOAP attachments
*
* @return SoapResponse
*/
public static function create($content, $location, $action, $version, $attachments = [])
{
$response = new SoapResponse();
$response->setContent($content);
$response->setLocation($location);
$response->setAction($action);
$response->setVersion($version);
$contentType = SoapMessage::getContentTypeForVersion($version);
$response->setContentType($contentType);
return $response;
}
}

View File

@ -33,7 +33,6 @@ class SoapServer extends \SoapServer
const SOAP_SERVER_REQUEST_FAILED = false; const SOAP_SERVER_REQUEST_FAILED = false;
protected $soapVersion; protected $soapVersion;
protected $soapKernel;
protected $soapServerOptions; protected $soapServerOptions;
protected $soapOptions; protected $soapOptions;
@ -48,8 +47,6 @@ class SoapServer extends \SoapServer
if ($soapOptions->hasAttachments()) { if ($soapOptions->hasAttachments()) {
$soapOptions = $this->configureTypeConverters($soapOptions); $soapOptions = $this->configureTypeConverters($soapOptions);
} }
$this->soapKernel = new SoapKernel();
$this->soapVersion = $soapOptions->getSoapVersion(); $this->soapVersion = $soapOptions->getSoapVersion();
$this->soapServerOptions = $soapServerOptions; $this->soapServerOptions = $soapServerOptions;
$this->soapOptions = $soapOptions; $this->soapOptions = $soapOptions;
@ -66,9 +63,30 @@ class SoapServer extends \SoapServer
* @param string $requestUrl * @param string $requestUrl
* @param string $soapAction * @param string $soapAction
* @param string $requestContent = null * @param string $requestContent = null
* @return string * @return string|false
*/ */
public function handle($requestUrl, $soapAction, $requestContent = null) public function handle($requestUrl, $soapAction, $requestContent = null)
{
try {
return $this->getSoapResponse($requestUrl, $soapAction, $requestContent)->getResponseContent();
} catch (\SoapFault $fault) {
$this->fault($fault->faultcode, $fault->faultstring);
return self::SOAP_SERVER_REQUEST_FAILED;
}
}
/**
* Custom handle method to be able to modify the SOAP messages.
*
* @param string $requestUrl
* @param string $soapAction
* @param string $requestContent = null
* @return SoapResponse
*/
public function getSoapResponse($requestUrl, $soapAction, $requestContent = null)
{ {
$soapRequest = SoapRequestFactory::create( $soapRequest = SoapRequestFactory::create(
$requestUrl, $requestUrl,
@ -76,16 +94,9 @@ class SoapServer extends \SoapServer
$this->soapVersion, $this->soapVersion,
$requestContent $requestContent
); );
try {
$soapResponse = $this->handleSoapRequest($soapRequest); $soapResponse = $this->handleSoapRequest($soapRequest);
} catch (\SoapFault $fault) {
$this->fault($fault->faultcode, $fault->faultstring);
return self::SOAP_SERVER_REQUEST_FAILED; return $soapResponse;
}
return $soapResponse->getResponseContent();
} }
/** /**
@ -99,8 +110,9 @@ class SoapServer extends \SoapServer
*/ */
private function handleSoapRequest(SoapRequest $soapRequest) private function handleSoapRequest(SoapRequest $soapRequest)
{ {
$soapKernel = new SoapKernel();
if ($this->soapOptions->hasAttachments()) { if ($this->soapOptions->hasAttachments()) {
$soapRequest = $this->soapKernel->filterRequest($soapRequest, $this->getFilters(), $this->soapOptions->getAttachmentType()); $soapRequest = $soapKernel->filterRequest($soapRequest, $this->getFilters(), $this->soapOptions->getAttachmentType());
} }
ob_start(); ob_start();
@ -111,8 +123,7 @@ class SoapServer extends \SoapServer
header_remove('Content-Length'); header_remove('Content-Length');
header_remove('Content-Type'); header_remove('Content-Type');
// wrap response data in SoapResponse object $soapResponse = SoapResponseFactory::create(
$soapResponse = SoapResponse::create(
$response, $response,
$soapRequest->getLocation(), $soapRequest->getLocation(),
$soapRequest->getAction(), $soapRequest->getAction(),
@ -120,7 +131,7 @@ class SoapServer extends \SoapServer
); );
if ($this->soapOptions->hasAttachments()) { if ($this->soapOptions->hasAttachments()) {
$soapResponse = $this->soapKernel->filterResponse($soapResponse, $this->getFilters(), $this->soapOptions->getAttachmentType()); $soapResponse = $soapKernel->filterResponse($soapResponse, $this->getFilters(), $this->soapOptions->getAttachmentType());
} }
return $soapResponse; return $soapResponse;
@ -145,10 +156,10 @@ class SoapServer extends \SoapServer
{ {
$filters = []; $filters = [];
if ($this->soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) { if ($this->soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {
$filters[] = new MimeFilter($this->soapOptions->getAttachmentType()); $filters[] = new MimeFilter();
} }
if ($this->soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) { if ($this->soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) {
$filters[] = new XmlMimeFilter($this->soapOptions->getAttachmentType()); $filters[] = new XmlMimeFilter();
} }
return $filters; return $filters;