From 8d033f9afc69e558c946bd141b0c028e4b76e156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Bechyn=C4=9B?= Date: Wed, 2 Nov 2016 16:08:21 +0100 Subject: [PATCH] SoapResponse is a product of SoapResponseFactory, small refactorings --- src/BeSimple/SoapClient/MimeFilter.php | 2 +- src/BeSimple/SoapCommon/ClassMap.php | 16 ++++++ src/BeSimple/SoapCommon/SoapKernel.php | 36 -------------- src/BeSimple/SoapServer/Classmap.php | 47 ------------------ src/BeSimple/SoapServer/SoapResponse.php | 43 ---------------- .../SoapServer/SoapResponseFactory.php | 42 ++++++++++++++++ src/BeSimple/SoapServer/SoapServer.php | 49 ++++++++++++------- 7 files changed, 89 insertions(+), 146 deletions(-) delete mode 100644 src/BeSimple/SoapServer/Classmap.php create mode 100644 src/BeSimple/SoapServer/SoapResponseFactory.php diff --git a/src/BeSimple/SoapClient/MimeFilter.php b/src/BeSimple/SoapClient/MimeFilter.php index e06c04f..e28e706 100644 --- a/src/BeSimple/SoapClient/MimeFilter.php +++ b/src/BeSimple/SoapClient/MimeFilter.php @@ -94,6 +94,6 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter $response->setAttachments($attachmentsReceived); } - return $response; + return $response; } } diff --git a/src/BeSimple/SoapCommon/ClassMap.php b/src/BeSimple/SoapCommon/ClassMap.php index f2ec016..be1c259 100644 --- a/src/BeSimple/SoapCommon/ClassMap.php +++ b/src/BeSimple/SoapCommon/ClassMap.php @@ -18,6 +18,7 @@ namespace BeSimple\SoapCommon; class ClassMap { protected $classMap; + protected $inverseClassMap; public function __construct(array $classMap = []) { @@ -61,6 +62,7 @@ class ClassMap } $this->classMap[$type] = $className; + $this->inverseClassMap[$className] = $type; } /** @@ -72,6 +74,20 @@ class ClassMap 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) { foreach ($classMap->getAll() as $type => $className) { diff --git a/src/BeSimple/SoapCommon/SoapKernel.php b/src/BeSimple/SoapCommon/SoapKernel.php index d968a4b..005ae47 100644 --- a/src/BeSimple/SoapCommon/SoapKernel.php +++ b/src/BeSimple/SoapCommon/SoapKernel.php @@ -2,8 +2,6 @@ namespace BeSimple\SoapCommon; -use BeSimple\SoapCommon\Mime\Part as MimePart; - /** * SoapKernel provides methods to pre- and post-process SoapRequests and SoapResponses using * chains of SoapRequestFilter and SoapResponseFilter objects (roughly following @@ -14,40 +12,6 @@ use BeSimple\SoapCommon\Mime\Part as MimePart; */ 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. * diff --git a/src/BeSimple/SoapServer/Classmap.php b/src/BeSimple/SoapServer/Classmap.php deleted file mode 100644 index aa1e3e9..0000000 --- a/src/BeSimple/SoapServer/Classmap.php +++ /dev/null @@ -1,47 +0,0 @@ - - * (c) Francis Besset - * - * 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 - */ -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]); - } -} diff --git a/src/BeSimple/SoapServer/SoapResponse.php b/src/BeSimple/SoapServer/SoapResponse.php index 2006abc..51224ea 100644 --- a/src/BeSimple/SoapServer/SoapResponse.php +++ b/src/BeSimple/SoapServer/SoapResponse.php @@ -1,54 +1,11 @@ - * (c) Francis Besset - * - * 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\SoapResponse as CommonSoapResponse; -use BeSimple\SoapCommon\SoapMessage; -/** - * SoapResponse class for SoapClient. Provides factory function for response object. - * - * @author Andreas Schamberger - */ 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() { // set Content-Type header diff --git a/src/BeSimple/SoapServer/SoapResponseFactory.php b/src/BeSimple/SoapServer/SoapResponseFactory.php new file mode 100644 index 0000000..99d4c2c --- /dev/null +++ b/src/BeSimple/SoapServer/SoapResponseFactory.php @@ -0,0 +1,42 @@ + + * (c) Francis Besset + * + * 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; + } +} diff --git a/src/BeSimple/SoapServer/SoapServer.php b/src/BeSimple/SoapServer/SoapServer.php index ab04ab1..5a3f8ac 100644 --- a/src/BeSimple/SoapServer/SoapServer.php +++ b/src/BeSimple/SoapServer/SoapServer.php @@ -33,7 +33,6 @@ class SoapServer extends \SoapServer const SOAP_SERVER_REQUEST_FAILED = false; protected $soapVersion; - protected $soapKernel; protected $soapServerOptions; protected $soapOptions; @@ -48,8 +47,6 @@ class SoapServer extends \SoapServer if ($soapOptions->hasAttachments()) { $soapOptions = $this->configureTypeConverters($soapOptions); } - - $this->soapKernel = new SoapKernel(); $this->soapVersion = $soapOptions->getSoapVersion(); $this->soapServerOptions = $soapServerOptions; $this->soapOptions = $soapOptions; @@ -66,9 +63,30 @@ class SoapServer extends \SoapServer * @param string $requestUrl * @param string $soapAction * @param string $requestContent = null - * @return string + * @return string|false */ 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( $requestUrl, @@ -76,16 +94,9 @@ class SoapServer extends \SoapServer $this->soapVersion, $requestContent ); + $soapResponse = $this->handleSoapRequest($soapRequest); - try { - $soapResponse = $this->handleSoapRequest($soapRequest); - } catch (\SoapFault $fault) { - $this->fault($fault->faultcode, $fault->faultstring); - - return self::SOAP_SERVER_REQUEST_FAILED; - } - - return $soapResponse->getResponseContent(); + return $soapResponse; } /** @@ -99,8 +110,9 @@ class SoapServer extends \SoapServer */ private function handleSoapRequest(SoapRequest $soapRequest) { + $soapKernel = new SoapKernel(); 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(); @@ -111,8 +123,7 @@ class SoapServer extends \SoapServer header_remove('Content-Length'); header_remove('Content-Type'); - // wrap response data in SoapResponse object - $soapResponse = SoapResponse::create( + $soapResponse = SoapResponseFactory::create( $response, $soapRequest->getLocation(), $soapRequest->getAction(), @@ -120,7 +131,7 @@ class SoapServer extends \SoapServer ); 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; @@ -145,10 +156,10 @@ class SoapServer extends \SoapServer { $filters = []; 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) { - $filters[] = new XmlMimeFilter($this->soapOptions->getAttachmentType()); + $filters[] = new XmlMimeFilter(); } return $filters;