From 6ac3493bc223768f666b487e02eca03d21241ec2 Mon Sep 17 00:00:00 2001 From: Christian Kerl Date: Mon, 21 Nov 2011 20:21:24 +0100 Subject: [PATCH 1/2] added basic soap processing classes discussed in #2 --- src/BeSimple/SoapCommon/SoapKernel.php | 82 ++++++++++++++++ src/BeSimple/SoapCommon/SoapMessage.php | 97 +++++++++++++++++++ src/BeSimple/SoapCommon/SoapRequest.php | 24 +++++ src/BeSimple/SoapCommon/SoapRequestFilter.php | 24 +++++ src/BeSimple/SoapCommon/SoapResponse.php | 24 +++++ .../SoapCommon/SoapResponseFilter.php | 24 +++++ 6 files changed, 275 insertions(+) create mode 100644 src/BeSimple/SoapCommon/SoapKernel.php create mode 100644 src/BeSimple/SoapCommon/SoapMessage.php create mode 100644 src/BeSimple/SoapCommon/SoapRequest.php create mode 100644 src/BeSimple/SoapCommon/SoapRequestFilter.php create mode 100644 src/BeSimple/SoapCommon/SoapResponse.php create mode 100644 src/BeSimple/SoapCommon/SoapResponseFilter.php diff --git a/src/BeSimple/SoapCommon/SoapKernel.php b/src/BeSimple/SoapCommon/SoapKernel.php new file mode 100644 index 0000000..74c6c7c --- /dev/null +++ b/src/BeSimple/SoapCommon/SoapKernel.php @@ -0,0 +1,82 @@ + + * (c) Francis Besset + * (c) Andreas Schamberger + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon; + +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 + * chains of SoapRequestFilter and SoapResponseFilter objects (roughly following + * the chain-of-responsibility pattern). + * + * @author Christian Kerl + */ +class SoapKernel +{ + private $requestFilters = array(); + private $responseFilters = array(); + + /** + * 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 + * before outer filters. This means the order is as follows: RequestFilter2->RequestFilter1 and + * ResponseFilter1->ResponseFilter2. + * + * TODO: add priority mechanism to ensure correct order of filters + * + * @param SoapRequestFilter|SoapResponseFilter $filter + */ + public function registerFilter($filter) + { + if($filter instanceof SoapRequestFilter) + { + array_unshift($this->requestFilters, $filter); + } + + if($filter instanceof SoapResponseFilter) + { + array_push($this->responseFilters, $filter); + } + } + + /** + * Applies all registered SoapRequestFilter to the given SoapRequest. + * + * @param SoapRequest $request + */ + public function filterRequest(SoapRequest $request) + { + foreach($this->requestFilters as $filter) + { + $filter->filterRequest($request); + } + } + + /** + * Applies all registered SoapResponseFilter to the given SoapResponse. + * + * @param SoapResponse $response + */ + public function filterResponse(SoapResponse $response) + { + foreach($this->responseFilters as $filter) + { + $filter->filterRequest($response); + } + } +} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/SoapMessage.php b/src/BeSimple/SoapCommon/SoapMessage.php new file mode 100644 index 0000000..a795139 --- /dev/null +++ b/src/BeSimple/SoapCommon/SoapMessage.php @@ -0,0 +1,97 @@ + + * (c) Francis Besset + * (c) Andreas Schamberger + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon; + +/** + * @author Christian Kerl + */ +abstract class SoapMessage +{ + const CONTENT_TYPE_HEADER = 'CONTENT_TYPE'; + const ACTION_HEADER = 'HTTP_SOAPACTION'; + + static protected $versionToContentTypeMap = array( + SOAP_1_1 => 'text/xml; charset=%s', + SOAP_1_2 => 'application/soap+xml; charset=%s' + ); + + static public function getContentTypeForVersion($version, $encoding = 'utf-8') + { + if(!in_array($soapVersion, array(SOAP_1_1, SOAP_1_2))) + { + throw new \InvalidArgumentException("The 'version' argument has to be either 'SOAP_1_1' or 'SOAP_1_2'!"); + } + + return sprintf(self::$versionToContentTypeMap[$version], $encoding); + } + + protected $contentType; + protected $content; + + protected $contentDomDocument = null; + + protected $version; + protected $action; + + public function getContentType() + { + return $this->contentType; + } + + public function setContentType($contentType) + { + $this->contentType = $contentType; + } + + public function getContent() + { + return $this->content; + } + + public function setContent($content) + { + $this->content = $content; + } + + public function getContentDocument() + { + if(null === $this->contentDomDocument) + { + $this->contentDomDocument = new \DOMDocument(); + $this->contentDomDocument->loadXML($this->content); + } + + return $this->contentDomDocument; + } + + public function getVersion() + { + return $this->version; + } + + public function setVersion($version) + { + $this->version = $version; + } + + public function getAction() + { + return $this->action; + } + + public function setAction($action) + { + $this->action = $action; + } +} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/SoapRequest.php b/src/BeSimple/SoapCommon/SoapRequest.php new file mode 100644 index 0000000..24120c4 --- /dev/null +++ b/src/BeSimple/SoapCommon/SoapRequest.php @@ -0,0 +1,24 @@ + + * (c) Francis Besset + * (c) Andreas Schamberger + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon; + +use BeSimple\SoapCommon\SoapMessage; + +/** + * @author Christian Kerl + */ +class SoapRequest extends SoapMessage +{ + +} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/SoapRequestFilter.php b/src/BeSimple/SoapCommon/SoapRequestFilter.php new file mode 100644 index 0000000..8b200ee --- /dev/null +++ b/src/BeSimple/SoapCommon/SoapRequestFilter.php @@ -0,0 +1,24 @@ + + * (c) Francis Besset + * (c) Andreas Schamberger + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon; + +use BeSimple\SoapCommon\SoapRequest; + +/** + * @author Christian Kerl + */ +interface SoapRequestFilter +{ + public function filterRequest(SoapRequest $request); +} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/SoapResponse.php b/src/BeSimple/SoapCommon/SoapResponse.php new file mode 100644 index 0000000..7141917 --- /dev/null +++ b/src/BeSimple/SoapCommon/SoapResponse.php @@ -0,0 +1,24 @@ + + * (c) Francis Besset + * (c) Andreas Schamberger + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon; + +use BeSimple\SoapCommon\SoapMessage; + +/** + * @author Christian Kerl + */ +class SoapResponse extends SoapMessage +{ + +} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/SoapResponseFilter.php b/src/BeSimple/SoapCommon/SoapResponseFilter.php new file mode 100644 index 0000000..ebc4c50 --- /dev/null +++ b/src/BeSimple/SoapCommon/SoapResponseFilter.php @@ -0,0 +1,24 @@ + + * (c) Francis Besset + * (c) Andreas Schamberger + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon; + +use BeSimple\SoapCommon\SoapResponse; + +/** + * @author Christian Kerl + */ +interface SoapResponseFilter +{ + public function filterResponse(SoapResponse $response); +} \ No newline at end of file From c09a163b90c799c46afe697115d67045983b8d0d Mon Sep 17 00:00:00 2001 From: Christian Kerl Date: Mon, 21 Nov 2011 20:28:31 +0100 Subject: [PATCH 2/2] fixed method call in SoapKernel --- src/BeSimple/SoapCommon/SoapKernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BeSimple/SoapCommon/SoapKernel.php b/src/BeSimple/SoapCommon/SoapKernel.php index 74c6c7c..5a8f650 100644 --- a/src/BeSimple/SoapCommon/SoapKernel.php +++ b/src/BeSimple/SoapCommon/SoapKernel.php @@ -76,7 +76,7 @@ class SoapKernel { foreach($this->responseFilters as $filter) { - $filter->filterRequest($response); + $filter->filterResponse($response); } } } \ No newline at end of file