Extended SoapServerBuilder with AbstractSoapBuilder of SoapCommon library

This commit is contained in:
Francis Besset 2011-10-09 19:35:08 +02:00
parent 3bb6d3abd4
commit cd2d8d90e8
3 changed files with 112 additions and 240 deletions

View File

@ -12,127 +12,67 @@
namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\Cache;
use BeSimple\SoapCommon\AbstractSoapBuilder;
use BeSimple\SoapCommon\Converter\TypeConverterInterface;
use BeSimple\SoapCommon\Converter\TypeConverterCollection;
/**
* SoapServerBuilder provides a fluent interface to configure and create a SoapServer instance.
*
* @author Christian Kerl
* @author Christian Kerl <christian-kerl@web.de>
*/
class SoapServerBuilder
class SoapServerBuilder extends AbstractSoapBuilder
{
public static function createEmpty()
{
return new self();
}
protected $persistence;
protected $errorReporting;
public static function createWithDefaults()
protected $handlerClass;
protected $handlerObject;
/**
* @return SoapServerBuilder
*/
static public function createWithDefaults()
{
$builder = new self();
$builder
->withSoapVersion12()
->withEncoding('UTF-8')
->withNoWsdlCache()
return parent::createWithDefaults()
->withErrorReporting(false)
->withSingleElementArrays()
;
return $builder;
}
private $options;
private $optionWsdl = null;
private $optionPersistence;
private $optionErrorReporting;
private $optionHandlerClass = null;
private $optionHandlerObject = null;
/**
* Initializes all options with the defaults used in the native SoapServer.
*/
private function __construct()
public function __construct()
{
$this->optionPersistence = SOAP_PERSISTENCE_REQUEST;
parent::__construct();
$this->persistence = SOAP_PERSISTENCE_REQUEST;
// TODO: this is not the default, but safer
$this->optionErrorReporting = false;
$this->withErrorReporting(false);
$this->options = array(
'soap_version' => SOAP_1_1,
'cache_wsdl' => Cache::getType(),
'classmap' => array(),
'typemap' => array(),
'features' => 0
);
$this->options['classmap'] = array();
$this->options['typemap'] = array();
}
public function build()
{
$this->validateOptions();
use_soap_error_handler($this->optionErrorReporting);
use_soap_error_handler($this->errorReporting);
$server = new SoapServer($this->optionWsdl, $this->options);
$server->setPersistence($this->optionPersistence);
$server = new SoapServer($this->wsdl, $this->options);
$server->setPersistence($this->persistence);
if(null !== $this->optionHandlerClass)
{
$server->setClass($this->optionHandlerClass);
}
if(null !== $this->optionHandlerObject)
{
$server->setObject($this->optionHandlerObject);
if (null !== $this->handlerClass) {
$server->setClass($this->handlerClass);
} elseif (null !== $this->handlerObject) {
$server->setObject($this->handlerObject);
}
return $server;
}
private function validateOptions()
{
if(null === $this->optionWsdl)
{
throw new \InvalidArgumentException('The WSDL has to be configured!');
}
if(null === $this->optionHandlerClass && null === $this->optionHandlerObject)
{
throw new \InvalidArgumentException('The handler has to be configured!');
}
}
public function withWsdl($wsdl)
{
$this->optionWsdl = $wsdl;
return $this;
}
public function withSoapVersion11()
{
$this->options['soap_version'] = SOAP_1_1;
return $this;
}
public function withSoapVersion12()
{
$this->options['soap_version'] = SOAP_1_2;
return $this;
}
public function withEncoding($encoding)
{
$this->options['encoding'] = $encoding;
return $this;
}
public function withActor($actor)
{
$this->options['actor'] = $actor;
@ -145,7 +85,7 @@ class SoapServerBuilder
*/
public function withHttpSession()
{
$this->optionPersistence = SOAP_PERSISTENCE_SESSION;
$this->persistence = SOAP_PERSISTENCE_SESSION;
return $this;
}
@ -157,35 +97,7 @@ class SoapServerBuilder
*/
public function withErrorReporting($enable = true)
{
$this->optionErrorReporting = $enable;
return $this;
}
public function withNoWsdlCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_NONE;
return $this;
}
public function withWsdlDiskCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_DISK;
return $this;
}
public function withWsdlMemoryCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_MEMORY;
return $this;
}
public function withWsdlDiskAndMemoryCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_DISK_MEMORY;
$this->errorReporting = $enable;
return $this;
}
@ -206,57 +118,23 @@ class SoapServerBuilder
}
/**
* Enables the SOAP_SINGLE_ELEMENT_ARRAYS feature.
*
* If enabled arrays containing only one element will be passed as arrays otherwise the single element is extracted and directly passed.
*/
public function withSingleElementArrays()
{
$this->options['features'] |= SOAP_SINGLE_ELEMENT_ARRAYS;
return $this;
}
/**
* Enables the SOAP_WAIT_ONE_WAY_CALLS feature.
*/
public function withWaitOneWayCalls()
{
$this->options['features'] |= SOAP_WAIT_ONE_WAY_CALLS;
return $this;
}
/**
* Enables the SOAP_USE_XSI_ARRAY_TYPE feature.
*/
public function withUseXsiArrayType()
{
$this->options['features'] |= SOAP_USE_XSI_ARRAY_TYPE;
return $this;
}
/**
*
*
* @param mixed $handler Can be either a class name or an object.
*
* @return SoapServerBuilder
*/
public function withHandler($handler)
{
if(is_string($handler) && class_exists($handler))
{
$this->optionHandlerClass = $handler;
$this->optionHandlerObject = null;
if (is_string($handler) && class_exists($handler)) {
$this->handlerClass = $handler;
$this->handlerObject = null;
} elseif (is_object($handler)) {
$this->handlerClass = null;
$this->handlerObject = $handler;
} else {
throw new \InvalidArgumentException('The handler has to be a class name or an object');
}
if(is_object($handler))
{
$this->optionHandlerClass = null;
$this->optionHandlerObject = $handler;
}
throw new \InvalidArgumentException('The handler has to be a class name or an object!');
return $this;
}
public function withTypeConverter(TypeConverterInterface $converter)
@ -284,10 +162,10 @@ class SoapServerBuilder
public function withTypeMapping($xmlNamespace, $xmlType, $fromXmlCallback, $toXmlCallback)
{
$this->options['typemap'][] = array(
'type_ns' => $xmlNamespace,
'type_ns' => $xmlNamespace,
'type_name' => $xmlType,
'from_xml' => $fromXmlCallback,
'to_xml' => $toXmlCallback
'to_xml' => $toXmlCallback
);
return $this;
@ -301,12 +179,9 @@ class SoapServerBuilder
*/
public function withTypemap($typemap, $merge = true)
{
if($merge)
{
if ($merge) {
$this->options['typemap'] = array_merge($this->options['typemap'], $typemap);
}
else
{
} else {
$this->options['typemap'] = $typemap;
}
@ -334,15 +209,21 @@ class SoapServerBuilder
*/
public function withClassmap($classmap, $merge = true)
{
if($merge)
{
if ($merge) {
$this->options['classmap'] = array_merge($this->options['classmap'], $classmap);
}
else
{
} else {
$this->options['classmap'] = $classmap;
}
return $this;
}
protected function validateOptions()
{
$this->validateWsdl();
if (null === $this->handlerClass && null === $this->handlerObject) {
throw new \InvalidArgumentException('The handler has to be configured!');
}
}
}

View File

@ -23,32 +23,23 @@ class SoapServerBuilderTest extends \PHPUnit_Framework_TestCase
{
public function testUnconfiguredWsdl()
{
$builder = SoapServerBuilder::createEmpty();
$builder = $this->getSoapServerBuilder();
try
{
$builder->build();
$this->fail('The SoapServer requires a WSDL file.');
}
catch(\InvalidArgumentException $e)
{
}
$this->setExpectedException('InvalidArgumentException');
$builder->build();
}
public function testUnconfiguredHandler()
{
$builder = SoapServerBuilder::createEmpty();
$builder = $this->getSoapServerBuilder();
$builder->withWsdl('my.wsdl');
try
{
$builder->build();
$this->setExpectedException('InvalidArgumentException');
$builder->build();
}
$this->fail('The SoapServer requires a handler.');
}
catch(\InvalidArgumentException $e)
{
}
public function getSoapServerBuilder()
{
return new SoapServerBuilder();
}
}

0
vendors.php Normal file → Executable file
View File